1 // File: NCollection_BaseAllocator.cxx
2 // Created: Fri Apr 12 12:53:28 2002
3 // Author: Alexander KARTOMIN (akm)
4 // <a-kartomin@opencascade.com>
5 // Purpose: Implementation of the BaseAllocator class
7 #include <NCollection_BaseAllocator.hxx>
8 #include <NCollection_DataMap.hxx>
9 #include <NCollection_List.hxx>
10 #include <Standard_Mutex.hxx>
12 IMPLEMENT_STANDARD_HANDLE(NCollection_BaseAllocator,MMgt_TShared)
13 IMPLEMENT_STANDARD_RTTIEXT(NCollection_BaseAllocator,MMgt_TShared)
15 //=======================================================================
17 //purpose : Standard allocation
18 //=======================================================================
20 void* NCollection_BaseAllocator::Allocate(const size_t size)
22 return Standard::Allocate(size);
25 //=======================================================================
27 //purpose : Standard deallocation
28 //=======================================================================
30 void NCollection_BaseAllocator::Free(void *anAddress)
32 if (anAddress) Standard::Free((Standard_Address&)anAddress);
35 //=======================================================================
36 //function : CommonBaseAllocator
37 //purpose : Creates the only one BaseAllocator
38 //=======================================================================
40 const Handle(NCollection_BaseAllocator)&
41 NCollection_BaseAllocator::CommonBaseAllocator(void)
43 static Handle(NCollection_BaseAllocator) pAllocator =
44 new NCollection_BaseAllocator;
48 // global variable to ensure that allocator will be created during loading the library
49 static Handle(NCollection_BaseAllocator) theAllocInit =
50 NCollection_BaseAllocator::CommonBaseAllocator();
52 //=======================================================================
53 //function : StandardCallBack
54 //purpose : Callback function to register alloc/free calls
55 //=======================================================================
59 Standard_Size roundSize;
63 : roundSize(0), nbAlloc(0), nbFree(0) {}
64 StorageInfo(Standard_Size theSize)
65 : roundSize(theSize), nbAlloc(0), nbFree(0) {}
68 static NCollection_DataMap<Standard_Size, StorageInfo> StorageMap;
70 void NCollection_BaseAllocator::StandardCallBack
71 (const Standard_Boolean theIsAlloc,
72 const Standard_Address /*theStorage*/,
73 const Standard_Size theRoundSize,
74 const Standard_Size /*theSize*/)
80 if (!StorageMap.IsBound(theRoundSize))
82 StorageInfo aEmpty(theRoundSize);
83 StorageMap.Bind(theRoundSize, aEmpty);
85 StorageInfo& aInfo = StorageMap(theRoundSize);
93 //=======================================================================
94 //function : PrintMemUsageStatistics
95 //purpose : Prints memory usage statistics cumulated by StandardCallBack
96 //=======================================================================
98 void NCollection_BaseAllocator::PrintMemUsageStatistics()
101 NCollection_List<StorageInfo> aColl;
102 NCollection_List<StorageInfo>::Iterator itLst;
103 NCollection_DataMap<Standard_Size, StorageInfo>::Iterator itMap(StorageMap);
104 for (; itMap.More(); itMap.Next())
106 for (itLst.Init(aColl); itLst.More(); itLst.Next())
107 if (itMap.Value().roundSize < itLst.Value().roundSize)
110 aColl.InsertBefore(itMap.Value(), itLst);
112 aColl.Append(itMap.Value());
114 Standard_Size aTotAlloc = 0;
115 Standard_Size aTotLeft = 0;
117 printf("%12s %12s %12s %12s %12s\n",
118 "BlockSize", "NbAllocated", "NbLeft", "Allocated", "Left");
119 for (itLst.Init(aColl); itLst.More(); itLst.Next())
121 const StorageInfo& aInfo = itLst.Value();
122 Standard_Integer nbLeft = aInfo.nbAlloc - aInfo.nbFree;
123 Standard_Size aSizeAlloc = aInfo.nbAlloc * aInfo.roundSize;
124 Standard_Size aSizeLeft = nbLeft * aInfo.roundSize;
125 printf("%12d %12d %12d %12d %12d\n", aInfo.roundSize,
126 aInfo.nbAlloc, nbLeft, aSizeAlloc, aSizeLeft);
127 aTotAlloc += aSizeAlloc;
128 aTotLeft += aSizeLeft;
130 printf("%12s %12s %12s %12d %12d\n", "Total:", "", "",
131 aTotAlloc, aTotLeft);