00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00099
00100
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
00139 assert ( _main_is_running == 0xfeedcafe );
00140
00141
00142
00143
00144
00145
if ( 0 != (t & 0x7) )
00146 {
00147 t += 0x8;
00148 t &= ~0x7;
00149 }
00150
00151
00152
00153
00154
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
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
00220