Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNMemory.h

Go to the documentation of this file.
00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 00023 */ 00024 #ifndef MN_MEMORY_H 00025 #define MN_MEMORY_H 00026 00027 #include <config.h> 00028 00029 #include <string.h> 00030 #include <stdlib.h> 00031 #include <assert.h> 00032 00033 #include "mnstream.h" 00034 00035 #define MN_MEMORY \ 00036 static void *operator new(size_t t) { return HeapMgmt::heap.allocate(t); } \ 00037 static void operator delete(void * p) { HeapMgmt::heap.deallocate(p); } 00038 00039 #define MN_MEM_INIT() HeapMgmt::heap.init() 00040 #define MN_MEM_ALLOCATE(t) HeapMgmt::heap.allocate(t) 00041 #define MN_MEM_DEALLOCATE(p) HeapMgmt::heap.deallocate(p) 00042 00043 class HeapMgmt 00044 { 00045 enum HeapSlot 00046 { 00047 SLOT_0x0008 = 1, 00048 SLOT_0x0010, 00049 SLOT_0x0018, 00050 SLOT_0x0020, 00051 SLOT_0x0028, 00052 SLOT_0x0030, 00053 SLOT_0x0038, 00054 SLOT_0x0040, 00055 SLOT_0x0048, 00056 SLOT_0x0050, 00057 SLOT_0x0058, 00058 SLOT_0x0060, 00059 SLOT_0x0068, 00060 SLOT_0x0070, 00061 SLOT_0x0078, 00062 SLOT_0x0080, 00063 SLOT_0x0088, 00064 NUM_SLOTS 00065 }; 00066 00067 void* _heap_array[NUM_SLOTS]; 00068 00069 #ifndef NDEBUG 00070 unsigned long _main_is_running; 00071 #define DO(a) a 00072 #else 00073 #define DO(a) 00074 #endif 00075 00076 inline int slot_for_size ( size_t t ) 00077 { 00078 switch ( t ) 00079 { 00080 case 0x0008 : return SLOT_0x0008; break; 00081 case 0x0010 : return SLOT_0x0010; break; 00082 case 0x0018 : return SLOT_0x0018; break; 00083 case 0x0020 : return SLOT_0x0020; break; 00084 case 0x0028 : return SLOT_0x0028; break; 00085 case 0x0030 : return SLOT_0x0030; break; 00086 case 0x0038 : return SLOT_0x0038; break; 00087 case 0x0040 : return SLOT_0x0040; break; 00088 case 0x0048 : return SLOT_0x0048; break; 00089 case 0x0050 : return SLOT_0x0050; break; 00090 case 0x0058 : return SLOT_0x0058; break; 00091 case 0x0060 : return SLOT_0x0060; break; 00092 case 0x0068 : return SLOT_0x0068; break; 00093 case 0x0070 : return SLOT_0x0070; break; 00094 case 0x0078 : return SLOT_0x0078; break; 00095 case 0x0080 : return SLOT_0x0080; break; 00096 case 0x0088 : return SLOT_0x0088; break; 00097 default : break; 00098 // I moved the return to end of file because xlC got 00099 // confused here, below as well. 00100 // cg 24nov1999 00101 } 00102 return 0; 00103 } 00104 00105 inline size_t size_for_slot ( int slot ) 00106 { 00107 switch ( slot ) 00108 { 00109 case SLOT_0x0008 : return 0x0008; break; 00110 case SLOT_0x0010 : return 0x0010; break; 00111 case SLOT_0x0018 : return 0x0018; break; 00112 case SLOT_0x0020 : return 0x0020; break; 00113 case SLOT_0x0028 : return 0x0028; break; 00114 case SLOT_0x0030 : return 0x0030; break; 00115 case SLOT_0x0038 : return 0x0038; break; 00116 case SLOT_0x0040 : return 0x0040; break; 00117 case SLOT_0x0048 : return 0x0048; break; 00118 case SLOT_0x0050 : return 0x0050; break; 00119 case SLOT_0x0058 : return 0x0058; break; 00120 case SLOT_0x0060 : return 0x0060; break; 00121 case SLOT_0x0068 : return 0x0068; break; 00122 case SLOT_0x0070 : return 0x0070; break; 00123 case SLOT_0x0078 : return 0x0078; break; 00124 case SLOT_0x0080 : return 0x0080; break; 00125 case SLOT_0x0088 : return 0x0088; break; 00126 default : break; 00127 } 00128 return 0; 00129 } 00130 00131 public: 00132 HeapMgmt(); 00133 00134 void init(); 00135 00136 inline void* allocate( size_t t ) 00137 { 00138 // ensure that the heap management is not used before main() 00139 assert ( _main_is_running == 0xfeedcafe ); 00140 00141 /* 00142 * Reduce the number of slots in the byte array by 00143 * rouding t up to the next 8 bytes value. 00144 */ 00145 if ( 0 != (t & 0x7) ) 00146 { 00147 t += 0x8; 00148 t &= ~0x7; // bit-wise complement of 7 00149 } 00150 00151 /* 00152 * For frequently requested sizes, hard-compile the sizes 00153 * into an array. The following switch selects whether the 00154 * sizes are hard-compiled or not. 00155 */ 00156 int use_array; 00157 use_array = slot_for_size(t); 00158 00159 if ( use_array != 0 ) 00160 { 00161 if ( _heap_array[use_array] != NULL ) 00162 { 00163 cout << "DEBUG: take " << t << " from stack " << use_array << endl; 00164 void* ret = _heap_array[use_array]; 00165 _heap_array[use_array] = *(void**)ret; 00166 return ret; 00167 } 00168 else 00169 { 00170 cout << "DEBUG: alloc " << t << " for stack " << use_array << endl; 00171 int* ret = (int*)malloc( t + sizeof(int) ); 00172 *ret = use_array; 00173 ret++; 00174 return (void*)ret; 00175 } 00176 } 00177 else 00178 { 00179 cout << "DEBUG: alloc " << t << " for release" << endl; 00180 int* ret = (int*)malloc( t + sizeof(int) ); 00181 *ret = 0; 00182 ret++; 00183 return (void*)ret; 00184 } 00185 } 00186 00187 inline void deallocate( void* ptr ) 00188 { 00189 // ensure that the heap management is not used before main() 00190 assert ( _main_is_running == 0xfeedcafe ); 00191 00192 int* ret; 00193 int use_array; 00194 00195 ret = (int*)ptr; 00196 ret--; 00197 use_array = *ret; 00198 if ( use_array == 0 ) 00199 { 00200 cout << "DEBUG: release" << endl; 00201 free ( ret ); 00202 } 00203 else 00204 { 00205 cout << "DEBUG: push on stack " << use_array << endl; 00206 void** q = (void**)ptr; 00207 *q = _heap_array[use_array]; 00208 _heap_array[use_array] = ptr; 00209 } 00210 } 00211 00212 #ifndef NDEBUG 00213 void dump_stacks( ostream& ostr ); 00214 #endif 00215 00216 static HeapMgmt heap; 00217 }; 00218 00219 #endif /* MN_MEMORY_H */ 00220

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8