92b35e3ae9e090c62ce697fb6c34df3d35fbf4f4
[occt.git] / src / NCollection / NCollection_HeapAllocator.cxx
1 // Created on: 2009-12-30
2 // Created by: Alexander GRIGORIEV
3 // Copyright (c) 2009-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_HeapAllocator.hxx>
17 #include <Standard_OutOfMemory.hxx>
18 #include <Standard_Mutex.hxx>
19
20 IMPLEMENT_STANDARD_HANDLE (NCollection_HeapAllocator, NCollection_BaseAllocator)
21 IMPLEMENT_STANDARD_RTTIEXT(NCollection_HeapAllocator, NCollection_BaseAllocator)
22
23 //=======================================================================
24 //function : Allocate
25 //purpose  : 
26 //=======================================================================
27
28 void * NCollection_HeapAllocator::Allocate (const Standard_Size theSize)
29 {
30   // the size is rounded up to word size.
31   const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
32   void* aResult = malloc (aRoundSize);
33   if (aResult == NULL)
34   {
35     char aBuffer[96];
36     Sprintf (aBuffer, "Failed to allocate %" PRIuPTR " bytes in global dynamic heap", theSize);
37     Standard_OutOfMemory::Raise (aBuffer);
38   }
39   return aResult;
40 }
41
42 //=======================================================================
43 //function : Free
44 //purpose  : 
45 //=======================================================================
46
47 void NCollection_HeapAllocator::Free (void * anAddress)
48 {
49   if (anAddress) free(anAddress);
50 }
51
52 //=======================================================================
53 //function : GlobalHeapAllocator
54 //purpose  : 
55 //=======================================================================
56
57 const Handle_NCollection_HeapAllocator&
58        NCollection_HeapAllocator::GlobalHeapAllocator()
59
60   static Handle(NCollection_HeapAllocator) pAllocator;
61   if (pAllocator.IsNull()) {
62     static Standard_Mutex theMutex;
63     Standard_Mutex::Sentry aSentry (theMutex);
64     if (pAllocator.IsNull()) {
65       pAllocator = new NCollection_HeapAllocator;
66     }
67   }
68   return pAllocator;
69 }