0031687: Draw Harness, ViewerTest - extend command vrenderparams with option updating...
[occt.git] / src / NCollection / NCollection_HeapAllocator.cxx
CommitLineData
b311480e 1// Created on: 2009-12-30
2// Created by: Alexander GRIGORIEV
973c2be1 3// Copyright (c) 2009-2014 OPEN CASCADE SAS
b311480e 4//
973c2be1 5// This file is part of Open CASCADE Technology software library.
b311480e 6//
d5f74e42 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
973c2be1 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.
b311480e 12//
973c2be1 13// Alternatively, this file may be used under the terms of Open CASCADE
14// commercial license or contractual agreement.
b311480e 15
7fd59977 16#include <NCollection_HeapAllocator.hxx>
17#include <Standard_OutOfMemory.hxx>
18#include <Standard_Mutex.hxx>
19
7fd59977 20
92efcf78 21IMPLEMENT_STANDARD_RTTIEXT(NCollection_HeapAllocator,NCollection_BaseAllocator)
22
7fd59977 23//=======================================================================
24//function : Allocate
25//purpose :
26//=======================================================================
27
28void * 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;
64531d9c 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);
9775fa61 37 throw Standard_OutOfMemory(aBuffer);
7fd59977 38 }
64531d9c 39 return aResult;
7fd59977 40}
41
42//=======================================================================
43//function : Free
44//purpose :
45//=======================================================================
46
47void NCollection_HeapAllocator::Free (void * anAddress)
48{
49 if (anAddress) free(anAddress);
50}
51
52//=======================================================================
53//function : GlobalHeapAllocator
54//purpose :
55//=======================================================================
56
857ffd5e 57const Handle(NCollection_HeapAllocator)&
7fd59977 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}