1 // Created by: PLOTNIKOV Eugeny & CHABROVSKY Dmitry
2 // Copyright (c) 1998-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
5 // The content of this file is subject to the Open CASCADE Technology Public
6 // License Version 6.5 (the "License"). You may not use the content of this file
7 // except in compliance with the License. Please obtain a copy of the License
8 // at http://www.opencascade.org and read it completely before using this file.
10 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
11 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
13 // The Original Code and all software distributed under the License is
14 // distributed on an "AS IS" basis, without warranty of any kind, and the
15 // Initial Developer hereby disclaims all such warranties, including without
16 // limitation, any warranties of merchantability, fitness for a particular
17 // purpose or non-infringement. Please see the License for the specific terms
18 // and conditions governing the rights and limitations under the License.
21 #include <W32_Allocator.hxx>
22 #include <W95_Allocator.hxx>
23 #include <WNT_Allocator.hxx>
27 #pragma comment( lib, "gdi32.lib" )
28 #pragma comment( lib, "user32.lib" )
30 ////////////////////////////////////////////////////////////////////////////////
31 // I N I T I A L I Z A T I O N //
32 ////////////////////////////////////////////////////////////////////////////////
33 #define DEF_BLOCK_SIZE ( 2 * s_dwPageSize )
35 static DWORD s_dwPageSize;
37 PW32_Allocator ( *W32_GetAllocator ) ( int, PW32_Allocator );
38 double W32_TextFactor;
40 static PW32_Allocator W95_GetAllocator ( int anID, PW32_Allocator head ) {
42 return new W95_Allocator ( anID, head );
44 } // end W95_GetAllocator
46 static PW32_Allocator WNT_GetAllocator ( int anID, PW32_Allocator head ) {
48 return new WNT_Allocator ( anID, head );
50 } // end WNT_GetAllocator
52 class _initAllocator {
60 _initAllocator :: _initAllocator () {
65 GetSystemInfo ( &si );
67 s_dwPageSize = si.dwPageSize;
69 os.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
72 if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) {
74 W32_GetAllocator = &W95_GetAllocator;
79 GetEnvironmentVariable ( TEXT( "CSF_WNT_FORCE_WIN95" ), NULL, 0 );
81 if ( GetLastError () != ERROR_ENVVAR_NOT_FOUND ) goto doWin95;
83 W32_GetAllocator = &WNT_GetAllocator;
90 static _initAllocator s_InitAllocator;
91 ////////////////////////////////////////////////////////////////////////////////
92 // W 3 2 _ A L L O C A T O R S T U F F //
93 ////////////////////////////////////////////////////////////////////////////////
94 W32_Allocator :: W32_Allocator ( int anID, PW32_Allocator head ) :
95 myID ( anID ), myFlags ( W32F_EMPTY ), myEnd ( NULL ),
99 HDC hdc = GetDC ( NULL );
101 GetObject ( hdc, sizeof ( LOGFONT ), &lf );
103 ReleaseDC ( NULL, hdc );
105 lf.lfOutPrecision |= OUT_TT_ONLY_PRECIS;
107 if ( head != NULL ) {
109 while ( head -> myNext != NULL ) head = head -> myNext;
111 head -> myNext = this;
115 myStart = MakeBlock ( DEF_BLOCK_SIZE );
117 myTextFont = CreateFontIndirect ( &lf );
120 myTextVScale = 1.0 / W32_TextFactor;
122 myScaleX = myScaleY = 1.0;
124 myPivot.x = myMove.x =
125 myPivot.y = myMove.y = 0;
127 myFlags = ( W32F_EMPTY | W32F_POUTL | W32F_DFONT );
128 myPrimitive = zzNone;
131 myMarkerPointColor = RGB( 255, 255, 255 );
135 W32_Allocator :: ~W32_Allocator () {
137 KillBlocks ( myStart );
139 if ( myFlags & W32F_DFONT ) DeleteFont( myTextFont );
143 PW32_Block W32_Allocator :: MakeBlock ( int aBlockSize ) {
147 aBlockSize = ( s_dwPageSize / aBlockSize + 1 ) * s_dwPageSize;
149 retVal = ( PW32_Block )VirtualAlloc (
150 NULL, aBlockSize, MEM_RESERVE | MEM_COMMIT,
154 if ( retVal == NULL ) RaiseException ( STATUS_NO_MEMORY, 0, 0, NULL );
156 if ( myEnd != NULL ) myEnd -> next = retVal;
160 retVal -> next = NULL;
161 retVal -> size = aBlockSize / sizeof ( int ) - sizeof ( W32_Block ) / sizeof ( int );
166 } // end W32_Allocator :: MakeBlock
168 PW32_Block W32_Allocator :: KillBlock ( PW32_Block aVictim ) {
170 PW32_Block next = aVictim -> next;
172 ReleaseBlock ( aVictim );
174 VirtualFree ( ( LPVOID )aVictim, 0, MEM_RELEASE );
178 } // end W32_Allocator :: KillBlock
180 void W32_Allocator :: KillBlocks ( PW32_Block aBegin ) {
182 while ( ( aBegin = KillBlock ( aBegin ) ) != NULL );
184 } // end W32_Allocator :: KillBlocks
186 void W32_Allocator :: ClearBlocks ( void ) {
188 if ( myStart -> next != NULL ) KillBlocks ( myStart -> next );
190 ReleaseBlock ( myStart );
192 myStart -> next = NULL;
196 myFlags &= ~W32F_DRAWN;
197 myFlags |= W32F_EMPTY;
199 } // end W32_Allocator :: ClearBlocks
201 void W32_Allocator :: ReleaseBlock ( PW32_Block pb ) {
203 for ( int i = 0; i < pb -> free; i += pb -> data[ i ] )
205 if ( pb -> data[ i + 1 ] != __W32_DATA ) {
207 W32_Note* pNote = ( W32_Note* )&( pb -> data[ i + 1 ] );
208 pNote -> ~W32_Note ();
212 } // end W32_Allocator :: ReleaseBlock
214 BOOL W32_Allocator :: FreeSpace ( PW32_Block aBlock, int aQuerySize ) {
216 return ( aBlock -> size >= ( aBlock -> free + aQuerySize + 1 ) );
218 } // end W32_Allocator :: FreeSpace
220 PW32_Block W32_Allocator :: ReserveData ( unsigned int iSize ) {
222 if ( !FreeSpace ( myEnd, iSize ) ) return NULL;
226 } // end W32_Allocator :: ReserveData
228 PW32_Block W32_Allocator :: ReserveFind ( unsigned int iSize ) {
230 PW32_Block aBlock = myStart;
231 for ( ; aBlock != NULL; aBlock = aBlock -> next )
233 if ( FreeSpace ( aBlock, iSize ) ) break;
237 } // end W32_Allocator :: ReserveFind
239 void* W32_Allocator :: ReserveBlock ( PW32_Block aBlock, int aQuerySize, BOOL fData ) {
245 retVal = fData ? &( aBlock -> data[ aBlock -> free + 2 ] ) :
246 &( aBlock -> data[ aBlock -> free + 1 ] );
248 aBlock -> data[ aBlock -> free ] = aQuerySize;
249 aBlock -> data[ aBlock -> free + 1 ] = __W32_DATA;
250 aBlock -> free += aQuerySize;
254 } // end W32_Allocator :: ReserveBlock
256 void* W32_Allocator :: NewClass ( unsigned int nBytes ) {
258 PW32_Block aBlock = myEnd;
260 nBytes = ( ( nBytes + 3 ) / sizeof ( int ) );
262 if ( !FreeSpace ( aBlock, nBytes ) ) aBlock = MakeBlock ( nBytes );
264 myFlags &= ~W32F_EMPTY;
266 return ReserveBlock ( aBlock, nBytes );
268 } // end W32_Allocator :: NewClass
270 void* W32_Allocator :: NewData ( unsigned int nBytes, BOOL fFind ) {
274 nBytes = ( ( nBytes + 3 ) / sizeof ( int ) ) + 1;
276 aBlock = fFind ? ReserveFind ( nBytes ) : ReserveData ( nBytes );
278 if ( aBlock == NULL ) aBlock = MakeBlock ( nBytes );
280 return ReserveBlock ( myEnd, nBytes, TRUE );
282 } // end W32_Allocator :: NewData
284 void* W32_Note :: operator new ( size_t cSize, PW32_Allocator anAllocator ) {
286 W32_Note* note = ( W32_Note* )anAllocator -> NewClass ( cSize );
288 note -> myAllocator = anAllocator;
290 return ( void* )note;
292 } // end W32_Note :: operator new