summaryrefslogtreecommitdiff
path: root/src/NCollection/NCollection_HeapAllocator.cxx
blob: 50107af5704575dd83ea6892d4b6bdf1897ba444 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// File:      NCollection_HeapAllocator.cxx
// Created:   30.12.09 00:38
// Author:    Alexander GRIGORIEV
// Copyright: Open Cascade 2009


#include <NCollection_HeapAllocator.hxx>
#include <Standard_OutOfMemory.hxx>
#include <Standard_Mutex.hxx>

#include <stdio.h>

#if defined(_MSC_VER)
# define FMT_SZ_Q "I"
#elif defined(__GNUC__)
# define FMT_SZ_Q "z"
#elif defined(_OCC64)
# define FMT_SZ_Q "l"
#else
# define FMT_SZ_Q ""
#endif

IMPLEMENT_STANDARD_HANDLE (NCollection_HeapAllocator, NCollection_BaseAllocator)
IMPLEMENT_STANDARD_RTTIEXT(NCollection_HeapAllocator, NCollection_BaseAllocator)

//=======================================================================
//function : Allocate
//purpose  : 
//=======================================================================

void * NCollection_HeapAllocator::Allocate (const Standard_Size theSize)
{
  // the size is rounded up to word size.
  const Standard_Size aRoundSize = (theSize + 3) & ~0x3;
  void * pResult = malloc(aRoundSize);
  if (!pResult) {
    char buf[128];
    sprintf (buf, "Failed to allocate %"FMT_SZ_Q"u bytes in global dynamic heap",theSize);
    Standard_OutOfMemory::Raise(&buf[0]);
  }
  return pResult;
}

//=======================================================================
//function : Free
//purpose  : 
//=======================================================================

void NCollection_HeapAllocator::Free (void * anAddress)
{
  if (anAddress) free(anAddress);
}

//=======================================================================
//function : GlobalHeapAllocator
//purpose  : 
//=======================================================================

const Handle_NCollection_HeapAllocator&
       NCollection_HeapAllocator::GlobalHeapAllocator()
{ 
  static Handle(NCollection_HeapAllocator) pAllocator;
  if (pAllocator.IsNull()) {
    static Standard_Mutex theMutex;
    Standard_Mutex::Sentry aSentry (theMutex);
    if (pAllocator.IsNull()) {
      pAllocator = new NCollection_HeapAllocator;
    }
  }
  return pAllocator;
}