0031671: Coding Rules - eliminate warnings issued by clang 11
[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 IMPLEMENT_STANDARD_RTTIEXT(NCollection_WinHeapAllocator,NCollection_BaseAllocator)
20
21 #if(defined(_WIN32) || defined(__WIN32__))
22   #include <windows.h>
23 #endif
24
25 //=======================================================================
26 //function : NCollection_WinHeapAllocator
27 //purpose  : Main constructor
28 //=======================================================================
29 NCollection_WinHeapAllocator::NCollection_WinHeapAllocator
30                                         (const size_t theInitSizeBytes)
31 : NCollection_BaseAllocator(),
32 #if(defined(_WIN32) || defined(__WIN32__))
33   myHeapH (HeapCreate (0, theInitSizeBytes, 0)),
34 #endif
35   myToZeroMemory (Standard_False)
36 {
37 #if defined(_WIN32) && (_WIN32_WINNT >= 0x0501)
38   // activate LHF to improve small size allocations
39   ULONG aHeapInfo = 2;
40   HeapSetInformation (myHeapH, HeapCompatibilityInformation,
41                       &aHeapInfo, sizeof(aHeapInfo));
42 #else
43   (void )theInitSizeBytes;
44 #endif
45 }
46
47 //=======================================================================
48 //function : ~NCollection_WinHeapAllocator
49 //purpose  : Destructor
50 //=======================================================================
51 NCollection_WinHeapAllocator::~NCollection_WinHeapAllocator()
52 {
53 #if(defined(_WIN32) || defined(__WIN32__))
54   HeapDestroy (myHeapH);
55 #endif
56 }
57
58
59 //=======================================================================
60 //function : Allocate
61 //purpose  :
62 //=======================================================================
63 void* NCollection_WinHeapAllocator::Allocate (const Standard_Size theSize)
64 {
65   // the size is rounded up to word size.
66   const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
67 #if(defined(_WIN32) || defined(__WIN32__))
68   void* aResult = HeapAlloc (myHeapH, myToZeroMemory ? HEAP_ZERO_MEMORY : 0,
69                              aRoundSize);
70 #else
71   void* aResult = malloc (aRoundSize);
72 #endif
73   if (aResult == NULL)
74   {
75     char aBuf[128];
76     Sprintf (aBuf, "Failed to allocate %" PRIuPTR " bytes in local dynamic heap", theSize);
77     throw Standard_OutOfMemory(aBuf);
78   }
79   return aResult;
80 }
81
82 //=======================================================================
83 //function : Free
84 //purpose  :
85 //=======================================================================
86 void NCollection_WinHeapAllocator::Free (void* theAddress)
87 {
88   if (theAddress != NULL)
89   {
90   #if(defined(_WIN32) || defined(__WIN32__))
91     HeapFree (myHeapH, 0, theAddress);
92   #else
93     free (theAddress);
94   #endif
95   }
96 }