0024947: Redesign OCCT legacy type system -- automatic
[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
21 //=======================================================================
22 //function : Allocate
23 //purpose  : 
24 //=======================================================================
25
26 void * NCollection_HeapAllocator::Allocate (const Standard_Size theSize)
27 {
28   // the size is rounded up to word size.
29   const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
30   void* aResult = malloc (aRoundSize);
31   if (aResult == NULL)
32   {
33     char aBuffer[96];
34     Sprintf (aBuffer, "Failed to allocate %" PRIuPTR " bytes in global dynamic heap", theSize);
35     Standard_OutOfMemory::Raise (aBuffer);
36   }
37   return aResult;
38 }
39
40 //=======================================================================
41 //function : Free
42 //purpose  : 
43 //=======================================================================
44
45 void NCollection_HeapAllocator::Free (void * anAddress)
46 {
47   if (anAddress) free(anAddress);
48 }
49
50 //=======================================================================
51 //function : GlobalHeapAllocator
52 //purpose  : 
53 //=======================================================================
54
55 const Handle(NCollection_HeapAllocator)&
56        NCollection_HeapAllocator::GlobalHeapAllocator()
57
58   static Handle(NCollection_HeapAllocator) pAllocator;
59   if (pAllocator.IsNull()) {
60     static Standard_Mutex theMutex;
61     Standard_Mutex::Sentry aSentry (theMutex);
62     if (pAllocator.IsNull()) {
63       pAllocator = new NCollection_HeapAllocator;
64     }
65   }
66   return pAllocator;
67 }