0023863: Wrong distance value between circle and cylinder
[occt.git] / src / WNT / W32_Allocator.cxx
1 // Created by: PLOTNIKOV Eugeny & CHABROVSKY Dmitry
2 // Copyright (c) 1998-1999 Matra Datavision
3 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 //
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.
9 //
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.
12 //
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.
19
20
21 #include <W32_Allocator.hxx>
22 #include <W95_Allocator.hxx>
23 #include <WNT_Allocator.hxx>
24
25 #include <windowsx.h>
26
27 #pragma comment( lib, "gdi32.lib"  )
28 #pragma comment( lib, "user32.lib" )
29
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 )
34
35 static DWORD s_dwPageSize;
36
37 PW32_Allocator ( *W32_GetAllocator ) ( int, PW32_Allocator );
38 double W32_TextFactor;
39
40 static PW32_Allocator W95_GetAllocator ( int anID, PW32_Allocator head ) {
41
42  return new W95_Allocator ( anID, head );
43
44 }  // end W95_GetAllocator
45
46 static PW32_Allocator WNT_GetAllocator ( int anID, PW32_Allocator head ) {
47
48  return new WNT_Allocator ( anID, head );
49
50 }  // end WNT_GetAllocator
51
52 class _initAllocator {
53
54  public:
55
56   _initAllocator ();
57
58 };
59
60 _initAllocator :: _initAllocator () {
61
62  OSVERSIONINFO os;
63  SYSTEM_INFO   si;
64
65  GetSystemInfo ( &si );
66
67  s_dwPageSize = si.dwPageSize;
68
69  os.dwOSVersionInfoSize = sizeof ( OSVERSIONINFO );
70  GetVersionEx ( &os );
71
72  if ( os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS ) {
73 doWin95:
74   W32_GetAllocator = &W95_GetAllocator;
75   W32_TextFactor = 5.0;
76
77  } else {
78
79   GetEnvironmentVariable (  TEXT( "CSF_WNT_FORCE_WIN95" ), NULL, 0  );
80
81   if (  GetLastError () != ERROR_ENVVAR_NOT_FOUND  ) goto doWin95;
82
83   W32_GetAllocator = &WNT_GetAllocator;
84   W32_TextFactor = 1.0;
85
86  }  // end else
87
88 }  // end constructor
89
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 ),
96                   myNext ( NULL ) {
97
98  LOGFONT lf;
99  HDC     hdc = GetDC ( NULL );
100
101   GetObject (  hdc, sizeof ( LOGFONT ), &lf  );
102
103  ReleaseDC ( NULL, hdc );
104
105  lf.lfOutPrecision |= OUT_TT_ONLY_PRECIS;
106
107  if ( head != NULL ) {
108
109   while ( head -> myNext != NULL ) head = head -> myNext;
110
111   head -> myNext = this;
112
113  }  // end if
114
115  myStart = MakeBlock ( DEF_BLOCK_SIZE );
116
117  myTextFont   = CreateFontIndirect ( &lf );
118  myTextSlant  = 0.0;
119  myTextHScale = 
120  myTextVScale = 1.0 / W32_TextFactor;
121
122  myScaleX  = myScaleY = 1.0;
123  myAngle   = 0.0;
124  myPivot.x = myMove.x =
125  myPivot.y = myMove.y = 0;
126
127  myFlags     = ( W32F_EMPTY | W32F_POUTL | W32F_DFONT );
128  myPrimitive = zzNone;
129
130  myPointColor       =
131  myMarkerPointColor = RGB( 255, 255, 255 );
132
133 }  // end constructor
134
135 W32_Allocator :: ~W32_Allocator () {
136
137  KillBlocks ( myStart );
138
139  if ( myFlags & W32F_DFONT ) DeleteFont( myTextFont );
140
141 }  // end destructor
142
143 PW32_Block W32_Allocator :: MakeBlock ( int aBlockSize ) {
144
145  PW32_Block retVal;
146
147  aBlockSize = ( s_dwPageSize / aBlockSize + 1 ) * s_dwPageSize;
148
149  retVal = ( PW32_Block )VirtualAlloc (
150                          NULL, aBlockSize, MEM_RESERVE | MEM_COMMIT,
151                          PAGE_READWRITE
152                         );
153
154  if ( retVal == NULL ) RaiseException ( STATUS_NO_MEMORY, 0, 0, NULL );
155
156  if ( myEnd != NULL ) myEnd -> next = retVal;
157   
158  myEnd = retVal;
159
160  retVal -> next = NULL;
161  retVal -> size = aBlockSize / sizeof ( int ) - sizeof ( W32_Block ) / sizeof ( int );
162  retVal -> free = 0;
163   
164  return retVal;
165
166 }  // end  W32_Allocator :: MakeBlock
167
168 PW32_Block W32_Allocator :: KillBlock ( PW32_Block aVictim ) {
169   
170  PW32_Block next = aVictim -> next;
171
172  ReleaseBlock ( aVictim );
173
174  VirtualFree (  ( LPVOID )aVictim, 0, MEM_RELEASE );
175  
176  return next;
177
178 }  // end W32_Allocator :: KillBlock
179
180 void W32_Allocator :: KillBlocks ( PW32_Block aBegin ) {
181  
182  while (   (  aBegin = KillBlock ( aBegin )  ) != NULL   );
183  
184 }  // end W32_Allocator :: KillBlocks
185
186 void W32_Allocator :: ClearBlocks ( void ) {
187
188  if ( myStart -> next != NULL ) KillBlocks ( myStart -> next );
189
190  ReleaseBlock ( myStart );
191
192  myStart -> next = NULL;
193  myStart -> free = 0;
194  myEnd           = myStart;
195
196  myFlags &= ~W32F_DRAWN;
197  myFlags |=  W32F_EMPTY;
198
199 }  // end W32_Allocator :: ClearBlocks
200
201 void W32_Allocator :: ReleaseBlock ( PW32_Block pb ) {
202
203  for ( int i = 0; i < pb -> free; i += pb -> data[ i ] )
204             
205   if ( pb -> data[ i + 1 ] != __W32_DATA ) {                                         
206
207    W32_Note* pNote = ( W32_Note* )&( pb -> data[ i + 1 ] );
208    pNote -> ~W32_Note ();
209
210   }  // end if
211
212 }  // end W32_Allocator :: ReleaseBlock
213
214 BOOL W32_Allocator :: FreeSpace ( PW32_Block aBlock, int aQuerySize ) {
215
216  return (  aBlock -> size >= ( aBlock -> free + aQuerySize + 1 )  );
217
218 }  // end W32_Allocator :: FreeSpace
219
220 PW32_Block W32_Allocator :: ReserveData ( unsigned int iSize ) {
221
222  if (  !FreeSpace ( myEnd, iSize )  ) return NULL;
223
224  return myEnd;
225
226 }  // end W32_Allocator :: ReserveData
227
228 PW32_Block W32_Allocator :: ReserveFind ( unsigned int iSize ) {
229
230   PW32_Block aBlock = myStart;
231   for ( ; aBlock != NULL; aBlock = aBlock -> next )
232
233     if (  FreeSpace ( aBlock, iSize )  ) break;
234
235  return aBlock;
236
237 }  // end W32_Allocator :: ReserveFind
238
239 void* W32_Allocator :: ReserveBlock ( PW32_Block aBlock, int aQuerySize, BOOL fData ) {
240
241  void* retVal;
242
243  ++aQuerySize;
244
245  retVal = fData ? &( aBlock -> data[ aBlock -> free + 2 ] ) :
246                   &( aBlock -> data[ aBlock -> free + 1 ] );
247
248  aBlock -> data[ aBlock -> free     ] = aQuerySize;
249  aBlock -> data[ aBlock -> free + 1 ] = __W32_DATA;
250  aBlock -> free += aQuerySize;
251  
252  return retVal; 
253
254 }  // end W32_Allocator :: ReserveBlock
255
256 void* W32_Allocator :: NewClass ( unsigned int nBytes ) {
257
258  PW32_Block aBlock = myEnd;
259
260  nBytes = (  ( nBytes + 3  ) / sizeof ( int )  );
261
262  if (  !FreeSpace ( aBlock, nBytes )  ) aBlock = MakeBlock ( nBytes );
263
264  myFlags &= ~W32F_EMPTY;
265
266  return ReserveBlock ( aBlock, nBytes );
267
268 }  // end W32_Allocator :: NewClass
269
270 void* W32_Allocator :: NewData ( unsigned int nBytes, BOOL fFind ) {
271   
272  PW32_Block aBlock;
273
274  nBytes = (  ( nBytes + 3 ) / sizeof ( int )  ) + 1;
275
276  aBlock = fFind ? ReserveFind ( nBytes ) : ReserveData ( nBytes );
277
278  if ( aBlock == NULL ) aBlock = MakeBlock ( nBytes );
279
280  return ReserveBlock ( myEnd, nBytes, TRUE ); 
281
282 }  // end W32_Allocator :: NewData
283
284 void* W32_Note :: operator new ( size_t cSize, PW32_Allocator anAllocator ) {
285
286  W32_Note* note = ( W32_Note* )anAllocator -> NewClass ( cSize );
287
288  note -> myAllocator = anAllocator;
289
290  return ( void* )note;
291
292 }  // end W32_Note :: operator new