Incrementation of OCCT version up to 6.8.0beta
[occt.git] / src / NCollection / NCollection_WinHeapAllocator.cxx
1 // Created on: 22.07.11
2 // Created by: Kirill GAVRILOV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #include <NCollection_WinHeapAllocator.hxx>
17 #include <Standard_OutOfMemory.hxx>
18
19 #if(defined(_WIN32) || defined(__WIN32__))
20   #include <windows.h>
21 #endif
22
23 IMPLEMENT_STANDARD_HANDLE (NCollection_WinHeapAllocator,
24                            NCollection_BaseAllocator)
25 IMPLEMENT_STANDARD_RTTIEXT(NCollection_WinHeapAllocator,
26                            NCollection_BaseAllocator)
27
28 //=======================================================================
29 //function : NCollection_WinHeapAllocator
30 //purpose  : Main constructor
31 //=======================================================================
32 NCollection_WinHeapAllocator::NCollection_WinHeapAllocator
33                                         (const size_t theInitSizeBytes)
34 : NCollection_BaseAllocator(),
35 #if(defined(_WIN32) || defined(__WIN32__))
36   myHeapH (HeapCreate (0, theInitSizeBytes, 0)),
37 #endif
38   myToZeroMemory (Standard_False)
39 {
40 #if(defined(_WIN32) || defined(__WIN32__))
41   // activate LHF to improve small size allocations
42   ULONG aHeapInfo = 2;
43   HeapSetInformation (myHeapH, HeapCompatibilityInformation,
44                       &aHeapInfo, sizeof(aHeapInfo));
45 #endif
46 }
47
48 //=======================================================================
49 //function : ~NCollection_WinHeapAllocator
50 //purpose  : Destructor
51 //=======================================================================
52 NCollection_WinHeapAllocator::~NCollection_WinHeapAllocator()
53 {
54 #if(defined(_WIN32) || defined(__WIN32__))
55   HeapDestroy (myHeapH);
56 #endif
57 }
58
59
60 //=======================================================================
61 //function : Allocate
62 //purpose  :
63 //=======================================================================
64 void* NCollection_WinHeapAllocator::Allocate (const Standard_Size theSize)
65 {
66   // the size is rounded up to word size.
67   const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
68 #if(defined(_WIN32) || defined(__WIN32__))
69   void* aResult = HeapAlloc (myHeapH, myToZeroMemory ? HEAP_ZERO_MEMORY : 0,
70                              aRoundSize);
71 #else
72   void* aResult = malloc (aRoundSize);
73 #endif
74   if (aResult == NULL)
75   {
76     char aBuf[128];
77     Sprintf (aBuf, "Failed to allocate "PRIuPTR" bytes in local dynamic heap", theSize);
78     Standard_OutOfMemory::Raise (aBuf);
79   }
80   return aResult;
81 }
82
83 //=======================================================================
84 //function : Free
85 //purpose  :
86 //=======================================================================
87 void NCollection_WinHeapAllocator::Free (void* theAddress)
88 {
89   if (theAddress != NULL)
90   {
91   #if(defined(_WIN32) || defined(__WIN32__))
92     HeapFree (myHeapH, 0, theAddress);
93   #else
94     free (theAddress);
95   #endif
96   }
97 }