0027960: Configuration - fix compilation of OSD_Directory with MinGW-w64
[occt.git] / src / NCollection / NCollection_WinHeapAllocator.cxx
CommitLineData
09711f37 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.
9df7f429 15
16#include <NCollection_WinHeapAllocator.hxx>
17#include <Standard_OutOfMemory.hxx>
18
92efcf78 19IMPLEMENT_STANDARD_RTTIEXT(NCollection_WinHeapAllocator,NCollection_BaseAllocator)
20
9df7f429 21#if(defined(_WIN32) || defined(__WIN32__))
22 #include <windows.h>
23#endif
24
9df7f429 25//=======================================================================
26//function : NCollection_WinHeapAllocator
27//purpose : Main constructor
28//=======================================================================
29NCollection_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{
7c65581d 37#if defined(_WIN32) && (_WIN32_WINNT >= 0x0501)
9df7f429 38 // activate LHF to improve small size allocations
39 ULONG aHeapInfo = 2;
40 HeapSetInformation (myHeapH, HeapCompatibilityInformation,
41 &aHeapInfo, sizeof(aHeapInfo));
c85385c0 42#else
43 (void )theInitSizeBytes;
9df7f429 44#endif
45}
46
47//=======================================================================
48//function : ~NCollection_WinHeapAllocator
49//purpose : Destructor
50//=======================================================================
51NCollection_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//=======================================================================
63void* 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];
4db4247a 76 Sprintf (aBuf, "Failed to allocate %" PRIuPTR " bytes in local dynamic heap", theSize);
9df7f429 77 Standard_OutOfMemory::Raise (aBuf);
78 }
79 return aResult;
80}
81
82//=======================================================================
83//function : Free
84//purpose :
85//=======================================================================
86void 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}