1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2012 OPEN CASCADE SAS
4 // The content of this file is subject to the Open CASCADE Technology Public
5 // License Version 6.5 (the "License"). You may not use the content of this file
6 // except in compliance with the License. Please obtain a copy of the License
7 // at http://www.opencascade.org and read it completely before using this file.
9 // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its
10 // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France.
12 // The Original Code and all software distributed under the License is
13 // distributed on an "AS IS" basis, without warranty of any kind, and the
14 // Initial Developer hereby disclaims all such warranties, including without
15 // limitation, any warranties of merchantability, fitness for a particular
16 // purpose or non-infringement. Please see the License for the specific terms
17 // and conditions governing the rights and limitations under the License.
23 #include <MMgt_StackManager.ixx>
24 #include <Standard_ProgramError.hxx>
31 #define SHALLOWDUMP ShallowDump(cout)
36 //============================================================================
37 //==== There are two declaration of classes just for protections =============
38 //============================================================================
54 static class _Element: public _BaseElement
59 {Standard_ProgramError::Raise("Attempt to access to a 'deleted' object");}
71 Standard_Address myNext;
73 } *anElement = new _Element;
75 //============================================================================
77 //============================================================================
78 MMgt_StackManager::MMgt_StackManager()
80 myFreeListSize = sizeof(_Element);
81 myFreeList = (Standard_Address)calloc((int)(myFreeListSize+1),
85 //============================================================================
86 void MMgt_StackManager::Destructor()
91 free((char*) myFreeList);
97 //============================================================================
98 void MMgt_StackManager::ShallowDump(Standard_OStream& s) const
100 Standard_Address aFree;
101 Standard_Integer i, NbFree;
103 s << "Begin class MMgt_StackManager\n" << endl
104 << "\t... Size:" << myFreeListSize << endl;
106 //==== A loop for Dumping all the storage in the Free List ===========
107 for(i=sizeof(Standard_Address); i<= myFreeListSize; i++){
108 aFree = ((Standard_Address *)myFreeList)[i];
111 //==== A loop for deallocating all the storage with the same size =======
113 aFree = ((_Element *)aFree)->myNext;
116 if(NbFree) s<< "\t... ["<< i<< "]: ("<< NbFree<< ") Free Block "<< endl;
118 s << "End class MMgt_StackManager" << endl;
121 //============================================================================
122 MMgt_StackManager MMgt_StackManager::ShallowCopy() const
124 Standard_ProgramError::Raise
125 ("Attempt to make a ShallowCopy of a 'MMgt_StackManager'");
129 //============================================================================
130 Standard_Address MMgt_StackManager::Allocate(const Standard_Integer aSize)
132 Standard_Address aStack;
134 if(aSize <= myFreeListSize && ((void* *)myFreeList)[aSize] != NULL){
136 //==== There is the free storage in the Free List, so we use it ==========
137 aStack = ((Standard_Address *)myFreeList)[aSize];
138 ((Standard_Address *)myFreeList)[aSize] = ((_Element *)aStack)->myNext;
140 //==== The storage is set to 0 ===========================================
141 memset(aStack,0,(int)aSize);
145 //==== There is no storage to be used, so we allocated it from "heap" ====
146 aStack = (void *)calloc((int)aSize, sizeof(char));
152 //============================================================================
153 void MMgt_StackManager::Free(Standard_Address& aStack,
154 const Standard_Integer aSize)
156 //==== Only the Storage large than a 'Element' can be used ================
157 if((unsigned int ) aSize > sizeof(_Element)){
159 if(aSize > myFreeListSize) {
161 //==== If there is no storage of this size in FreeList ================
162 myFreeList=(Standard_Address)realloc((char *)myFreeList,
163 (int)(aSize+1)*sizeof(myFreeList));
165 //==== Initialize to "NULL" the new case of FreeList =================
166 for(Standard_Integer i=myFreeListSize+1; i<=aSize; i++){
167 ((Standard_Address *)myFreeList)[i] = NULL;
170 myFreeListSize = aSize;
173 //==== Recycling the storage in the Free List ===========================
174 anElement->myNext = ((Standard_Address *)myFreeList)[aSize];
176 memcpy((char *)aStack, (char *)anElement, sizeof(_Element));
177 ((Standard_Address *)myFreeList)[aSize] = aStack;
179 //==== The littles storgas will be managed by system ====================
180 free((char *) aStack);
183 //==== Nullify ============================================================
187 void MMgt_StackManager::Purge()
189 Standard_Address aFree;
190 Standard_Address aOther;
194 //==== A loop for deallocating all the storage in the Free List ===========
195 for(i=sizeof(Standard_Address); i<= myFreeListSize; i++){
196 aFree = ((Standard_Address *)myFreeList)[i];
198 //==== A loop for deallocating all the storage with the same size =======
201 aFree = ((_Element *)aFree)->myNext;
202 free((char *)aOther);
205 ((Standard_Address *)myFreeList)[i] = NULL;