5 #include <MMgt_StackManager.ixx>
6 #include <Standard_ProgramError.hxx>
13 #define SHALLOWDUMP ShallowDump(cout)
18 //============================================================================
19 //==== There are two declaration of classes just for protections =============
20 //============================================================================
36 static class _Element: public _BaseElement
41 {Standard_ProgramError::Raise("Attempt to access to a 'deleted' object");}
53 Standard_Address myNext;
55 } *anElement = new _Element;
57 //============================================================================
59 //============================================================================
60 MMgt_StackManager::MMgt_StackManager()
62 myFreeListSize = sizeof(_Element);
63 myFreeList = (Standard_Address)calloc((int)(myFreeListSize+1),
67 //============================================================================
68 void MMgt_StackManager::Destructor()
73 free((char*) myFreeList);
79 //============================================================================
80 void MMgt_StackManager::ShallowDump(Standard_OStream& s) const
82 Standard_Address aFree;
83 Standard_Integer i, NbFree;
85 s << "Begin class MMgt_StackManager\n" << endl
86 << "\t... Size:" << myFreeListSize << endl;
88 //==== A loop for Dumping all the storage in the Free List ===========
89 for(i=sizeof(Standard_Address); i<= myFreeListSize; i++){
90 aFree = ((Standard_Address *)myFreeList)[i];
93 //==== A loop for deallocating all the storage with the same size =======
95 aFree = ((_Element *)aFree)->myNext;
98 if(NbFree) s<< "\t... ["<< i<< "]: ("<< NbFree<< ") Free Block "<< endl;
100 s << "End class MMgt_StackManager" << endl;
103 //============================================================================
104 MMgt_StackManager MMgt_StackManager::ShallowCopy() const
106 Standard_ProgramError::Raise
107 ("Attempt to make a ShallowCopy of a 'MMgt_StackManager'");
111 //============================================================================
112 Standard_Address MMgt_StackManager::Allocate(const Standard_Integer aSize)
114 Standard_Address aStack;
116 if(aSize <= myFreeListSize && ((void* *)myFreeList)[aSize] != NULL){
118 //==== There is the free storage in the Free List, so we use it ==========
119 aStack = ((Standard_Address *)myFreeList)[aSize];
120 ((Standard_Address *)myFreeList)[aSize] = ((_Element *)aStack)->myNext;
122 //==== The storage is set to 0 ===========================================
123 memset(aStack,0,(int)aSize);
127 //==== There is no storage to be used, so we allocated it from "heap" ====
128 aStack = (void *)calloc((int)aSize, sizeof(char));
134 //============================================================================
135 void MMgt_StackManager::Free(Standard_Address& aStack,
136 const Standard_Integer aSize)
138 //==== Only the Storage large than a 'Element' can be used ================
139 if((unsigned int ) aSize > sizeof(_Element)){
141 if(aSize > myFreeListSize) {
143 //==== If there is no storage of this size in FreeList ================
144 myFreeList=(Standard_Address)realloc((char *)myFreeList,
145 (int)(aSize+1)*sizeof(myFreeList));
147 //==== Initialize to "NULL" the new case of FreeList =================
148 for(Standard_Integer i=myFreeListSize+1; i<=aSize; i++){
149 ((Standard_Address *)myFreeList)[i] = NULL;
152 myFreeListSize = aSize;
155 //==== Recycling the storage in the Free List ===========================
156 anElement->myNext = ((Standard_Address *)myFreeList)[aSize];
158 memcpy((char *)aStack, (char *)anElement, sizeof(_Element));
159 ((Standard_Address *)myFreeList)[aSize] = aStack;
161 //==== The littles storgas will be managed by system ====================
162 free((char *) aStack);
165 //==== Nullify ============================================================
169 void MMgt_StackManager::Purge()
171 Standard_Address aFree;
172 Standard_Address aOther;
176 //==== A loop for deallocating all the storage in the Free List ===========
177 for(i=sizeof(Standard_Address); i<= myFreeListSize; i++){
178 aFree = ((Standard_Address *)myFreeList)[i];
180 //==== A loop for deallocating all the storage with the same size =======
183 aFree = ((_Element *)aFree)->myNext;
184 free((char *)aOther);
187 ((Standard_Address *)myFreeList)[i] = NULL;