[a92ff6] | 1 | #ifndef MMEMORY_H |
---|
| 2 | #define MMEMORY_H |
---|
| 3 | /**************************************** |
---|
| 4 | * Computer Algebra System SINGULAR * |
---|
| 5 | ****************************************/ |
---|
[416465] | 6 | /* $Id: mmemory.h,v 1.31 1999-11-15 17:20:24 obachman Exp $ */ |
---|
[a92ff6] | 7 | /* |
---|
| 8 | * ABSTRACT |
---|
| 9 | */ |
---|
| 10 | #include <stdlib.h> |
---|
[5c187b] | 11 | #include <stdio.h> |
---|
[a92ff6] | 12 | |
---|
| 13 | #ifdef __cplusplus |
---|
| 14 | extern "C" { |
---|
[b7b08c] | 15 | #else |
---|
| 16 | #define inline static |
---|
[a92ff6] | 17 | #endif |
---|
| 18 | |
---|
[b7b08c] | 19 | #include "mod2.h" |
---|
[a92ff6] | 20 | #include "structs.h" |
---|
| 21 | |
---|
| 22 | /********************************************************************** |
---|
| 23 | * |
---|
| 24 | * Memory allocation |
---|
| 25 | * |
---|
| 26 | **********************************************************************/ |
---|
| 27 | #ifndef MDEBUG |
---|
| 28 | void * mmAllocBlock( size_t ); |
---|
| 29 | void * mmAllocBlock0( size_t ); |
---|
| 30 | void mmFreeBlock( void*, size_t ); |
---|
| 31 | void * mmReallocBlock( void*, size_t, size_t ); |
---|
| 32 | void * mmReallocBlock0( void*, size_t, size_t ); |
---|
| 33 | void * mmAlloc( size_t ); |
---|
| 34 | void * mmRealloc( void*, size_t ); |
---|
| 35 | void mmFree( void* ); |
---|
| 36 | char * mmStrdup( const char* ); |
---|
[421acf] | 37 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
[fff984] | 38 | void * mmAllocAlignedBlock( size_t ); |
---|
| 39 | void * mmAllocAlignedBlock0( size_t ); |
---|
[f8519e] | 40 | void mmFreeAlignedBlock( void*, size_t ); |
---|
[421acf] | 41 | #endif |
---|
[d6681d] | 42 | #define AllocHeap(h) _AllocHeap(h) |
---|
| 43 | #define Alloc0Heap(h) _Alloc0Heap(h) |
---|
| 44 | #define AllocHeapType(h, t) (t) AllocHeap(h) |
---|
| 45 | #define Alloc0HeapType(h, t) (t) Alloc0Heap(h) |
---|
[a92ff6] | 46 | #define FreeHeap mmFreeHeap |
---|
| 47 | #define Alloc mmAllocBlock |
---|
| 48 | #define Alloc0 mmAllocBlock0 |
---|
| 49 | #define Free mmFreeBlock |
---|
| 50 | #define ReAlloc mmReallocBlock |
---|
| 51 | #define ReAlloc0 mmReallocBlock0 |
---|
| 52 | #define FreeL mmFree |
---|
| 53 | #define AllocL mmAlloc |
---|
| 54 | #define mstrdup mmStrdup |
---|
[421acf] | 55 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
[fff984] | 56 | #define AllocAligned0 mmAllocAlignedBlock0 |
---|
| 57 | #define AllocAligned mmAllocAlignedBlock |
---|
| 58 | #define FreeAligned mmFreeAlignedBlock |
---|
[421acf] | 59 | #elif SIZEOF_DOUBLE == SIZEOF_VOIDP |
---|
| 60 | #define AllocAligned0 mmAllocBlock0 |
---|
| 61 | #define AllocAligned mmAllocBlock |
---|
| 62 | #define FreeAligned mmFreeBlock |
---|
| 63 | #endif |
---|
[a92ff6] | 64 | |
---|
| 65 | #else /* MDEBUG */ |
---|
| 66 | |
---|
| 67 | void * mmDBAllocHeap(memHeap heap, char*, int ); |
---|
[b7b08c] | 68 | void * mmDBAlloc0Heap(memHeap heap, char*, int ); |
---|
[a92ff6] | 69 | void mmDBFreeHeap(void* addr, memHeap heap, char*, int ); |
---|
| 70 | void * mmDBAllocBlock( size_t, char*, int ); |
---|
| 71 | void * mmDBAllocBlock0( size_t, char*, int); |
---|
| 72 | void mmDBFreeBlock( void*, size_t, char*, int); |
---|
| 73 | void * mmDBReallocBlock( void*, size_t, size_t, char*, int ); |
---|
| 74 | void * mmDBReallocBlock0( void*, size_t, size_t, char*, int ); |
---|
| 75 | void * mmDBAlloc( size_t, char*, int ); |
---|
| 76 | void * mmDBRealloc( void*, size_t, char*, int ); |
---|
| 77 | void mmDBFree( void*, char*, int ); |
---|
| 78 | char * mmDBStrdup( const char * s, char *fname, int lineno); |
---|
[421acf] | 79 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
[fff984] | 80 | void * mmDBAllocAlignedBlock( size_t, char*, int ); |
---|
| 81 | void * mmDBAllocAlignedBlock0( size_t, char*, int); |
---|
[f8519e] | 82 | void mmDBFreeAlignedBlock( void*, size_t, char*, int ); |
---|
[421acf] | 83 | #endif |
---|
[a92ff6] | 84 | |
---|
[b7b08c] | 85 | #define AllocHeap(heap) mmDBAllocHeap(heap, __FILE__, __LINE__) |
---|
| 86 | #define Alloc0Heap(heap) mmDBAlloc0Heap(heap, __FILE__, __LINE__) |
---|
[d6681d] | 87 | #define AllocHeapType(heap,t) (t) mmDBAllocHeap(heap, __FILE__, __LINE__) |
---|
| 88 | #define Alloc0HeapType(heap,t) (t) mmDBAlloc0Heap(heap, __FILE__, __LINE__) |
---|
[b7b08c] | 89 | #define FreeHeap(addr, heap) mmDBFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
[a92ff6] | 90 | #define Alloc(s) mmDBAllocBlock(s, __FILE__, __LINE__) |
---|
| 91 | #define Alloc0(s) mmDBAllocBlock0(s, __FILE__, __LINE__) |
---|
| 92 | #define Free(a,s) mmDBFreeBlock(a, s, __FILE__, __LINE__) |
---|
| 93 | #define ReAlloc(a,o,n) mmDBReallocBlock(a, o, n, __FILE__, __LINE__) |
---|
| 94 | #define ReAlloc0(a,o,n) mmDBReallocBlock0(a, o, n, __FILE__, __LINE__) |
---|
| 95 | #define AllocL(s) mmDBAlloc(s, __FILE__, __LINE__) |
---|
| 96 | #define FreeL(a) mmDBFree(a,__FILE__,__LINE__) |
---|
| 97 | #define mstrdup(s) mmDBStrdup(s, __FILE__, __LINE__) |
---|
[421acf] | 98 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
[fff984] | 99 | #define AllocAligned(s) mmDBAllocAlignedBlock(s, __FILE__, __LINE__) |
---|
| 100 | #define AllocAligned0(s) mmDBAllocAlignedBlock0(s, __FILE__, __LINE__) |
---|
| 101 | #define FreeAligned(a,s) mmDBFreeAlignedBlock(a, s, __FILE__, __LINE__) |
---|
[421acf] | 102 | #elif SIZEOF_DOUBLE == SIZEOF_VOIDP |
---|
| 103 | #define AllocAligned(s) mmDBAllocBlock(s, __FILE__, __LINE__) |
---|
| 104 | #define AllocAligned0(s) mmDBAllocBlock0(s, __FILE__, __LINE__) |
---|
| 105 | #define FreeAligned(a,s) mmDBFreeBlock(a, s, __FILE__, __LINE__) |
---|
| 106 | #endif |
---|
[a92ff6] | 107 | |
---|
| 108 | #endif /* MDEBUG */ |
---|
| 109 | |
---|
[b7b08c] | 110 | /********************************************************************** |
---|
| 111 | * |
---|
| 112 | * ASO stuff -- enable/disable in mod2.h |
---|
| 113 | * |
---|
| 114 | **********************************************************************/ |
---|
| 115 | #if defined(HAVE_ASO) && HAVE_ASO == 1 |
---|
| 116 | |
---|
| 117 | /* definitions of ALLOC_SIZE_OF_## are given in *.aso */ |
---|
| 118 | |
---|
| 119 | #define AllocSizeOf(x) ALLOC_SIZE_OF_##x |
---|
| 120 | #define Alloc0SizeOf(x) ALLOC0_SIZE_OF_##x |
---|
| 121 | #define FreeSizeOf(x,y) FREE_SIZE_OF_##y(x) |
---|
| 122 | |
---|
| 123 | #if defined(ASO_DEBUG) || defined(MDEBUG) |
---|
| 124 | void* mmDBAllocHeapSizeOf(memHeap heap, size_t size, char* file, int line); |
---|
| 125 | void* mmDBAlloc0HeapSizeOf(memHeap heap, size_t size, char* file, int line); |
---|
| 126 | void mmDBFreeHeapSizeOf(void* addr, memHeap heap, size_t size, |
---|
| 127 | char* file, int line); |
---|
[d6681d] | 128 | #define AllocHeapSizeOf(h, s, t) (t) mmDBAllocHeapSizeOf(h, s, __FILE__, __LINE__) |
---|
| 129 | #define Alloc0HeapSizeOf(h, s, t) (t) mmDBAlloc0HeapSizeOf(h, s, __FILE__, __LINE__) |
---|
[b7b08c] | 130 | #define FreeHeapSizeOf(x, h, s) mmDBFreeHeapSizeOf(x, h, s, __FILE__, __LINE__) |
---|
| 131 | #else /* ! (ASO_DEBUG || MDEBUG) */ |
---|
[d6681d] | 132 | #define AllocHeapSizeOf(h, s, t) AllocHeapType(h, t) |
---|
| 133 | #define Alloc0HeapSizeOf(h, s, t) Alloc0HeapType(h, t) |
---|
[b7b08c] | 134 | #define FreeHeapSizeOf(x, h, s) FreeHeap(x, h) |
---|
| 135 | #endif /* ASO_DEBUG || MDEBUG */ |
---|
| 136 | |
---|
| 137 | #else |
---|
| 138 | /* defaults for AllocSizeOf stuff -- should be redefined by *.aso files */ |
---|
| 139 | #define AllocSizeOf(x) Alloc(sizeof(x)) |
---|
| 140 | #define Alloc0SizeOf(x) Alloc0(sizeof(x)) |
---|
[d6681d] | 141 | #define FreeSizeOf(x, y) Free(x, sizeof(y)) |
---|
[b7b08c] | 142 | #endif /* HAVE_ASO */ |
---|
| 143 | |
---|
[a92ff6] | 144 | |
---|
| 145 | /********************************************************************** |
---|
| 146 | * |
---|
| 147 | * Tests of memory (for MDEBUG, only) |
---|
| 148 | * |
---|
| 149 | **********************************************************************/ |
---|
| 150 | |
---|
| 151 | #ifdef MDEBUG |
---|
| 152 | |
---|
| 153 | /* use this variables to control level of MDEBUG at run-time |
---|
| 154 | (see mod2.h for details) */ |
---|
| 155 | extern int mm_MDEBUG; |
---|
[e48debd] | 156 | |
---|
[a92ff6] | 157 | BOOLEAN mmDBTestHeapBlock(const void* adr, const memHeap heap, |
---|
| 158 | const char * fname, const int lineno ); |
---|
| 159 | BOOLEAN mmDBTestBlock(const void* adr, const size_t size, |
---|
| 160 | const char * fname, const int lineno ); |
---|
| 161 | BOOLEAN mmDBTest(const void* adr, const char * fname, const int lineno); |
---|
| 162 | |
---|
[b061fe] | 163 | #if MDEBUG >= 0 |
---|
[a92ff6] | 164 | #define mmTestHeap(a, h)\ |
---|
[e48debd] | 165 | mmDBTestHeapBlock(a, h, __FILE__, __LINE__) |
---|
[a92ff6] | 166 | #define mmTest(A,B) mmDBTestBlock(A,B,__FILE__,__LINE__) |
---|
| 167 | #define mmTestL(A) mmDBTest(A,__FILE__,__LINE__) |
---|
| 168 | #define mmTestP(A,B) mmDBTestBlock(A,B,__FILE__,__LINE__) |
---|
| 169 | #define mmTestLP(A) mmDBTest(A,__FILE__,__LINE__) |
---|
[b061fe] | 170 | #else /* MDEBUG < 0 */ |
---|
| 171 | #define mmTestHeap(addr, heap) mmCheckHeapAddr(addr, heap) |
---|
| 172 | #define mmTest(A,B) TRUE |
---|
| 173 | #define mmTestL(A) TRUE |
---|
| 174 | #define mmTestP(A,B) TRUE |
---|
| 175 | #define mmTestLP(A) TRUE |
---|
| 176 | #endif /* MDEBUG >= 0 */ |
---|
[a92ff6] | 177 | |
---|
| 178 | int mmTestMemory(); |
---|
[e48debd] | 179 | |
---|
[a92ff6] | 180 | void mmPrintUsedList(); |
---|
[6e5833] | 181 | void mmMarkInitDBMCB(); |
---|
[e56c23] | 182 | void mmMarkCurrentUsageState(); |
---|
| 183 | void mmMarkCurrentUsageStart(); |
---|
| 184 | void mmMarkCurrentUsageStop(); |
---|
| 185 | void mmPrintUnMarkedBlocks(); |
---|
| 186 | void mmStartReferenceWatch(); |
---|
| 187 | void mmStopReferenceWatch(); |
---|
[5c187b] | 188 | void mmTestList(FILE *fd, int all); |
---|
[a92ff6] | 189 | |
---|
| 190 | #else |
---|
| 191 | |
---|
| 192 | |
---|
| 193 | #define mmTestHeap(addr, heap) mmCheckHeapAddr(addr, heap) |
---|
| 194 | #define mmTest(A,B) TRUE |
---|
| 195 | #define mmTestL(A) TRUE |
---|
[b061fe] | 196 | #define mmTestP(A,B) TRUE |
---|
| 197 | #define mmTestLP(A) TRUE |
---|
| 198 | #define mmTestMemory() TRUE |
---|
[6e5833] | 199 | #define mmMarkInitDBMCB() |
---|
| 200 | #define mmTestList(a) |
---|
[e48debd] | 201 | |
---|
[a92ff6] | 202 | #endif /* MDEBUG */ |
---|
| 203 | |
---|
| 204 | /********************************************************************** |
---|
| 205 | * |
---|
[b7b08c] | 206 | * Public Heap Routines |
---|
[a92ff6] | 207 | * |
---|
| 208 | **********************************************************************/ |
---|
| 209 | |
---|
[b7b08c] | 210 | /********************************** |
---|
| 211 | * |
---|
| 212 | * Creation/Destruction/Garbage Collection |
---|
| 213 | * |
---|
| 214 | **********************************/ |
---|
| 215 | /* creates and initializes a temporary heap */ |
---|
[942846] | 216 | extern memHeap mmGetTempHeap(size_t size); |
---|
[b7b08c] | 217 | #ifndef HEAP_DEBUG |
---|
[942846] | 218 | /* UNCONDITIONALLY clears and destroys temporary heap */ |
---|
| 219 | extern void mmUnGetTempHeap(memHeap *heap_p); |
---|
[b7b08c] | 220 | #else |
---|
[942846] | 221 | #define mmUnGetTempHeap(h) mmDebugDestroyTempHeap(h) |
---|
| 222 | extern void mmUnGetTempHeap(memHeap *heap); |
---|
[b7b08c] | 223 | #endif /* HEAP_DEBUG */ |
---|
| 224 | |
---|
[b061fe] | 225 | // #define HAVE_AUTOMATIC_GC |
---|
[f983ed] | 226 | #ifndef HAVE_AUTOMATIC_GC |
---|
[b7b08c] | 227 | /* removes chunks in freelist which fill one page */ |
---|
| 228 | /* if strict & 1, does it even if free ptr has not changed w.r.t. last gc */ |
---|
| 229 | /* if strict & 2, also releases free pages */ |
---|
[942846] | 230 | /* if strict & 4, gc also of temp heaps */ |
---|
[b7b08c] | 231 | extern void mmGarbageCollectHeaps(int strict); |
---|
| 232 | extern void mmGarbageCollectHeap(memHeap heap, int strict); |
---|
[f983ed] | 233 | #else |
---|
| 234 | #define mmGarbageCollectHeaps(s) |
---|
| 235 | #define mmGarbageCollectHeap(h, s) |
---|
| 236 | #endif /* HAVE_AUTOMATIC_GC */ |
---|
[b7b08c] | 237 | |
---|
| 238 | /* Returns a heap of the given size */ |
---|
| 239 | extern memHeap mmGetSpecHeap( size_t ); |
---|
| 240 | /* use this to "unget" (free) a heap once allocated with mmGetSpecHeap */ |
---|
| 241 | extern void mmUnGetSpecHeap(memHeap *heap); |
---|
| 242 | |
---|
| 243 | /* Merges what is free in Heap "what" into free list of heap "into" */ |
---|
| 244 | extern void mmMergeHeap(memHeap into, memHeap what); |
---|
| 245 | |
---|
| 246 | /********************************** |
---|
| 247 | * |
---|
| 248 | * Allocate, Free from Heaps |
---|
| 249 | * |
---|
| 250 | **********************************/ |
---|
| 251 | #ifndef HEAP_DEBUG |
---|
[d6681d] | 252 | #define mmAllocHeap(res, heap) _mmAllocHeap(res, heap, void*) |
---|
| 253 | #define mmFreeHeap(addr, heap) _mmFreeHeap(addr, heap) |
---|
| 254 | #define mmAllocHeapType(res, heap, type) _mmAllocHeap(res, heap, type) |
---|
[b7b08c] | 255 | #define mmCheckHeap(heap) 1 |
---|
| 256 | #define mmCheckHeapAddr(addr, heap) 1 |
---|
[d6681d] | 257 | #define mmTestHeaps() 1 |
---|
[b7b08c] | 258 | #else |
---|
| 259 | /* |
---|
| 260 | * define HEAP_DEBUG and/or set mm_HEAP_DEBUG to |
---|
| 261 | * 0 to enable basic heap addr checks (at least on each alloc/free) |
---|
| 262 | * 1 for addtl. containment checks in free/alloc list of heap |
---|
| 263 | * 2 for addtl. check of entire heap at each heap addr check |
---|
| 264 | * NOTE: For HEAP_DEBUG > 1 on, it gets very slow |
---|
| 265 | */ |
---|
| 266 | extern int mm_HEAP_DEBUG; |
---|
| 267 | |
---|
[d6681d] | 268 | #define mmAllocHeapType(res, heap, type) \ |
---|
| 269 | (res) = (type) mmDebugAllocHeap(heap, __FILE__, __LINE__) |
---|
| 270 | #define mmAllocHeap(res, heap) \ |
---|
[b7b08c] | 271 | (res) = mmDebugAllocHeap(heap, __FILE__, __LINE__) |
---|
| 272 | void * mmDebugAllocHeap(memHeap heap, const char*, int ); |
---|
| 273 | |
---|
| 274 | #define mmFreeHeap(addr, heap)\ |
---|
| 275 | mmDebugFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
| 276 | void mmDebugFreeHeap(void* addr, memHeap heap, const char*, int ); |
---|
| 277 | |
---|
| 278 | #define mmCheckHeap(heap)\ |
---|
| 279 | mmDebugCheckHeap(heap, __FILE__, __LINE__) |
---|
| 280 | int mmDebugCheckHeap(memHeap heap, const char* fn, int line); |
---|
| 281 | |
---|
| 282 | #define mmCheckHeapAddr(addr, heap) \ |
---|
| 283 | mmDebugCheckHeapAdr(addr, heap, MM_HEAP_ADDR_USED_FLAG, __FILE__, __LINE__) |
---|
| 284 | int mmDebugCheckHeapAddr(void* addr, memHeap heap, int flag, |
---|
| 285 | const char* fn, int l); |
---|
[d6681d] | 286 | int mmTestHeaps(); |
---|
[b7b08c] | 287 | #endif /* HEAP_DEBUG */ |
---|
| 288 | |
---|
| 289 | /********************************** |
---|
| 290 | * |
---|
| 291 | * Low-level heap stuff |
---|
| 292 | * |
---|
| 293 | **********************************/ |
---|
| 294 | /* Need to define it here, has to be known to macros */ |
---|
[f983ed] | 295 | |
---|
| 296 | /* array of static heaps */ |
---|
| 297 | extern struct sip_memHeap mm_theList[]; |
---|
| 298 | extern memHeapPage mmAllocNewHeapPage(memHeap heap); |
---|
| 299 | |
---|
| 300 | #ifndef HAVE_AUTOMATIC_GC |
---|
| 301 | struct sip_memHeapPage |
---|
| 302 | { |
---|
| 303 | memHeapPage next; |
---|
| 304 | long counter; |
---|
| 305 | }; |
---|
| 306 | |
---|
| 307 | /* Change this appropriately, if you change sip_memHeapPage */ |
---|
| 308 | /* However, make sure that sizeof(sip_memHeapPage) is a multiple of 8 */ |
---|
| 309 | #define SIZE_OF_HEAP_PAGE_HEADER (SIZEOF_VOIDP + SIZEOF_LONG) |
---|
| 310 | #define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZE_OF_HEAP_PAGE_HEADER) |
---|
| 311 | |
---|
[b7b08c] | 312 | struct sip_memHeap |
---|
| 313 | { |
---|
| 314 | void* current; /* Freelist pointer */ |
---|
| 315 | memHeapPage pages; /* Pointer to linked list of pages */ |
---|
| 316 | void* last_gc; /* current pointer after last gc */ |
---|
| 317 | long size; /* Size of heap chunks */ |
---|
| 318 | }; |
---|
| 319 | |
---|
| 320 | /* Allocates memory block from a heap */ |
---|
[d6681d] | 321 | #define _mmAllocHeap(what, heap, type) \ |
---|
[b7b08c] | 322 | do \ |
---|
| 323 | { \ |
---|
| 324 | register memHeap _heap = heap; \ |
---|
| 325 | if ((_heap)->current == NULL) mmAllocNewHeapPage(_heap); \ |
---|
[d6681d] | 326 | what = (type)((_heap)->current); \ |
---|
[b7b08c] | 327 | (_heap)->current = *((void**)(_heap)->current); \ |
---|
| 328 | } \ |
---|
| 329 | while (0) |
---|
| 330 | |
---|
| 331 | /* Frees addr into heap, assumes addr was previously allocated from heap */ |
---|
| 332 | #define _mmFreeHeap(addr, heap) \ |
---|
| 333 | do \ |
---|
| 334 | { \ |
---|
| 335 | register memHeap _heap = heap; \ |
---|
| 336 | *((void**) addr) = (_heap)->current; \ |
---|
| 337 | (_heap)->current = (void*) addr; \ |
---|
| 338 | } \ |
---|
| 339 | while (0) |
---|
| 340 | |
---|
[f983ed] | 341 | #else /* HAVE_AUTOMATIC_GC */ |
---|
| 342 | |
---|
| 343 | struct sip_memHeapPage |
---|
| 344 | { |
---|
| 345 | long used_blocks; /* number of used blocks of this page */ |
---|
| 346 | void* current; /* pointer to current freelist */ |
---|
| 347 | memHeapPage next; /* next/prev pointer of pages */ |
---|
| 348 | memHeapPage prev; |
---|
| 349 | }; |
---|
| 350 | |
---|
| 351 | /* Change this appropriately, if you change sip_memHeapPage */ |
---|
| 352 | /* However, make sure that sizeof(sip_memHeapPage) is a multiple of 8 */ |
---|
| 353 | #define SIZE_OF_HEAP_PAGE_HEADER (3*SIZEOF_VOIDP + SIZEOF_LONG) |
---|
| 354 | #define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZE_OF_HEAP_PAGE_HEADER) |
---|
| 355 | |
---|
| 356 | struct sip_memHeap |
---|
| 357 | { |
---|
[c54075] | 358 | memHeapPage current_page; /* page of current freelist */ |
---|
[b061fe] | 359 | memHeapPage last_page; /* pointer to last page of freelist */ |
---|
| 360 | int size; /* size of blocks */ |
---|
| 361 | int max_blocks; /* max(SIZE_OF_HEAP_PAGE / size, 1) */ |
---|
[f983ed] | 362 | }; |
---|
| 363 | |
---|
| 364 | extern memHeapPage mmGetNewCurrentPage(memHeap heap); |
---|
[d6681d] | 365 | extern void mmRearrangeHeapPages(memHeapPage page, void* addr, memHeap heap); |
---|
[f983ed] | 366 | extern struct sip_memHeapPage mmZeroPage[]; |
---|
| 367 | #ifndef mmGetPageOfAddr |
---|
| 368 | #define mmGetPageOfAddr(addr) \ |
---|
| 369 | ((void*) ((long) (addr) & ~(SIZE_OF_SYSTEM_PAGE -1))) |
---|
| 370 | #endif |
---|
| 371 | |
---|
[d6681d] | 372 | #define _mmAllocHeap(what, heap, type) \ |
---|
[f983ed] | 373 | do \ |
---|
| 374 | { \ |
---|
| 375 | register memHeapPage _page = (heap)->current_page; \ |
---|
| 376 | if (_page->current == NULL) _page = mmGetNewCurrentPage(heap); \ |
---|
| 377 | (_page->used_blocks)++; \ |
---|
[d6681d] | 378 | what = (type)((_page)->current); \ |
---|
[f983ed] | 379 | (_page)->current = *((void**)(_page)->current); \ |
---|
| 380 | } \ |
---|
| 381 | while (0) |
---|
| 382 | |
---|
| 383 | #define _mmFreeHeap(addr, heap) \ |
---|
| 384 | do \ |
---|
| 385 | { \ |
---|
| 386 | register memHeapPage _page = mmGetPageOfAddr(addr); \ |
---|
[d6681d] | 387 | (_page->used_blocks)--; \ |
---|
[f983ed] | 388 | if (_page->used_blocks == 0) \ |
---|
[d6681d] | 389 | mmRearrangeHeapPages(_page, addr, heap); \ |
---|
[f983ed] | 390 | else \ |
---|
| 391 | { \ |
---|
[d6681d] | 392 | *((void**) (addr)) = _page->current; \ |
---|
| 393 | _page->current = (addr); \ |
---|
[f983ed] | 394 | } \ |
---|
| 395 | } \ |
---|
| 396 | while (0) |
---|
| 397 | |
---|
| 398 | #endif /* ! HAVE_AUTOMATIC_GC */ |
---|
| 399 | |
---|
[b7b08c] | 400 | #define MM_HEAP_ADDR_UNKNOWN_FLAG 0 |
---|
| 401 | #define MM_HEAP_ADDR_USED_FLAG 1 |
---|
| 402 | #define MM_HEAP_ADDR_FREE_FLAG 2 |
---|
| 403 | /* use this for unknown heaps */ |
---|
| 404 | #define MM_UNKNOWN_HEAP ((memHeap) 1) |
---|
| 405 | |
---|
[a92ff6] | 406 | |
---|
[b7b08c] | 407 | /********************************************************************** |
---|
| 408 | * |
---|
| 409 | * Misc stuff |
---|
| 410 | * |
---|
| 411 | **********************************************************************/ |
---|
[a92ff6] | 412 | /* for handling of memory statistics */ |
---|
| 413 | int mmMemAlloc( void ); |
---|
| 414 | int mmMemUsed( void ); |
---|
| 415 | #ifdef HAVE_SBRK |
---|
| 416 | int mmMemPhysical( void ); |
---|
| 417 | #endif |
---|
| 418 | void mmPrintStat(); |
---|
[e48debd] | 419 | |
---|
[a92ff6] | 420 | size_t mmSizeL( void* ); |
---|
| 421 | |
---|
| 422 | /********************************************************************** |
---|
| 423 | * |
---|
[e48debd] | 424 | * Some operations on linked lists of memory |
---|
[a92ff6] | 425 | * |
---|
| 426 | **********************************************************************/ |
---|
| 427 | /* The following routines assume that Next(list) == *((void**) list) */ |
---|
[f8519e] | 428 | /* Removes element from list, if contained in it and returns list */ |
---|
| 429 | void* mmRemoveFromList(void* list, void* element); |
---|
[a92ff6] | 430 | /* Returns the length of a memory list; assumes list has no cycles */ |
---|
| 431 | int mmListLength(void* list); |
---|
| 432 | /* Returns last non-NULL element of list; assumes list has no cycles */ |
---|
| 433 | void* mmListLast(void* list); |
---|
| 434 | /* returns 1, if addr is contained in memory list |
---|
| 435 | * 0, otherwise */ |
---|
| 436 | int mmIsAddrOnList(void* addr, void* list); |
---|
| 437 | /* Checks whether memory list has cycles: If yes, returns address of |
---|
| 438 | * first element of list which is contained at least twice in memory |
---|
| 439 | * list. If no, NULL is returned */ |
---|
| 440 | void* mmListHasCycle(void* list); |
---|
| 441 | |
---|
| 442 | /* The following routines assume that Next(list) == *((void**) list + next) */ |
---|
[e48debd] | 443 | |
---|
[a92ff6] | 444 | /* Returns the length of a memory list; assumes list has no cycles */ |
---|
| 445 | int mmGListLength(void* list, int next); |
---|
| 446 | /* Returns last non-NULL element of list; assumes list has no cycles */ |
---|
| 447 | void* mmGListLast(void* list, int next); |
---|
| 448 | /* returns 1, if addr is contained in memory list |
---|
| 449 | * 0, otherwise */ |
---|
| 450 | int mmIsAddrOnGList(void* addr, void* list, int next); |
---|
| 451 | /* Checks whether memory list has cycles: If yes, returns address of |
---|
| 452 | * first element of list which is contained at least twice in memory |
---|
| 453 | * list. If no, NULL is returned */ |
---|
| 454 | void* mmGListHasCycle(void* list, int next); |
---|
[b7b08c] | 455 | /* Removes element from list, if contained in it and returns list */ |
---|
| 456 | void* mmRemoveFromGList(void* list, int next, void* element); |
---|
| 457 | |
---|
| 458 | /* The following cast (list + int_field) to a pointer to int |
---|
| 459 | and assume list is sorted in ascending order w.r.t. *(list + int_field) |
---|
| 460 | */ |
---|
| 461 | /* Inserts element at the right place */ |
---|
| 462 | void* mmSortedInsertInGList(void* list, int next, int int_field, void* element); |
---|
| 463 | /* Finds element */ |
---|
| 464 | void* mmFindInSortedGList(void* list, int next, int int_field, int what); |
---|
| 465 | |
---|
| 466 | |
---|
| 467 | |
---|
[a92ff6] | 468 | /********************************************************************** |
---|
| 469 | * |
---|
| 470 | * some fast macros for basic memory operations |
---|
| 471 | * |
---|
| 472 | **********************************************************************/ |
---|
| 473 | #ifdef DO_DEEP_PROFILE |
---|
[a5c76f] | 474 | extern void _memcpyW(long* p1, long* p2, long l); |
---|
| 475 | #define memcpy_nwEVEN(p1, p2, l) _memcpyW((long*) p1, (long*) p2, (long) l) |
---|
| 476 | #define memcpy_nwODD(p1, p2, l) _memcpyW((long*) p1, (long*) p2, (long) l) |
---|
| 477 | #define memcpyW(p1, p2, l) _memcpyW((long*) p1, (long*) p2, (long) l) |
---|
[e48debd] | 478 | |
---|
[a5c76f] | 479 | extern void _memaddW(long* p1, long* p2, long* p3, long l); |
---|
[a92ff6] | 480 | #define memaddW(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
| 481 | #define memadd_nwODD(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
| 482 | #define memadd_nwEVEN(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
| 483 | #define memadd_nwONE(p1, p2, p3) _memaddW(p1, p2, p3, 1) |
---|
| 484 | #define memadd_nwTWO(p1, p2, p3) _memaddW(p1, p2, p3, 2) |
---|
| 485 | |
---|
[a5c76f] | 486 | extern void _memsetW(long* p1, long w, long l); |
---|
[a92ff6] | 487 | #define memsetW(p1, w, l) _memsetW(p1, w, l) |
---|
| 488 | |
---|
| 489 | #else /* ! DO_DEEP_PROFILE */ |
---|
| 490 | |
---|
| 491 | #define memcpyW(p1, p2, l) \ |
---|
| 492 | do \ |
---|
| 493 | { \ |
---|
| 494 | long _i = l; \ |
---|
| 495 | long* _s1 = (long*) p1; \ |
---|
| 496 | const long* _s2 = (long*) p2; \ |
---|
| 497 | \ |
---|
| 498 | for (;;) \ |
---|
| 499 | { \ |
---|
| 500 | *_s1 = *_s2; \ |
---|
| 501 | _i--; \ |
---|
| 502 | if (_i == 0) break; \ |
---|
| 503 | _s1++; \ |
---|
| 504 | _s2++; \ |
---|
| 505 | } \ |
---|
| 506 | } \ |
---|
| 507 | while(0) |
---|
| 508 | |
---|
| 509 | #define memcpy_nwODD(p1, p2, l) \ |
---|
| 510 | do \ |
---|
| 511 | { \ |
---|
| 512 | long _i = l - 1; \ |
---|
| 513 | long* _s1 = (long*) p1; \ |
---|
| 514 | const long* _s2 = (long*) p2; \ |
---|
| 515 | \ |
---|
| 516 | *_s1++ = *_s2++; \ |
---|
| 517 | for (;;) \ |
---|
| 518 | { \ |
---|
| 519 | *_s1++ = *_s2++; \ |
---|
| 520 | *_s1++ = *_s2++; \ |
---|
| 521 | _i -= 2; \ |
---|
| 522 | if (_i == 0) break; \ |
---|
| 523 | } \ |
---|
| 524 | } \ |
---|
| 525 | while(0) |
---|
| 526 | |
---|
| 527 | #define memcpy_nwEVEN(p1, p2, l) \ |
---|
| 528 | do \ |
---|
| 529 | { \ |
---|
| 530 | long _i = l; \ |
---|
| 531 | long* _s1 = (long*) p1; \ |
---|
| 532 | const long* _s2 = (long*) p2; \ |
---|
| 533 | \ |
---|
| 534 | for (;;) \ |
---|
| 535 | { \ |
---|
| 536 | *_s1++ = *_s2++; \ |
---|
| 537 | *_s1++ = *_s2++; \ |
---|
| 538 | _i -= 2; \ |
---|
| 539 | if (_i == 0) break; \ |
---|
| 540 | } \ |
---|
| 541 | } \ |
---|
| 542 | while(0) |
---|
| 543 | |
---|
| 544 | #define memaddW(P1, P2, P3, L) \ |
---|
| 545 | do \ |
---|
| 546 | { \ |
---|
| 547 | unsigned long* _p1 = P1; \ |
---|
| 548 | const unsigned long* _p2 = P2; \ |
---|
| 549 | const unsigned long* _p3 = P3; \ |
---|
| 550 | unsigned long l = L; \ |
---|
| 551 | \ |
---|
| 552 | do \ |
---|
| 553 | { \ |
---|
| 554 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 555 | l--; \ |
---|
| 556 | } \ |
---|
| 557 | while(l); \ |
---|
| 558 | } \ |
---|
| 559 | while(0) |
---|
| 560 | |
---|
| 561 | #define memadd_nwODD(P1, P2, P3, L) \ |
---|
| 562 | do \ |
---|
| 563 | { \ |
---|
| 564 | unsigned long* _p1 = P1; \ |
---|
| 565 | const unsigned long* _p2 = P2; \ |
---|
| 566 | const unsigned long* _p3 = P3; \ |
---|
| 567 | unsigned long l = L; \ |
---|
| 568 | \ |
---|
| 569 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 570 | l--; \ |
---|
| 571 | \ |
---|
| 572 | do \ |
---|
| 573 | { \ |
---|
| 574 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 575 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 576 | l -=2; \ |
---|
| 577 | } \ |
---|
| 578 | while(l); \ |
---|
| 579 | } \ |
---|
| 580 | while(0) |
---|
[e48debd] | 581 | |
---|
[a92ff6] | 582 | #define memadd_nwEVEN(P1, P2, P3, L) \ |
---|
| 583 | do \ |
---|
| 584 | { \ |
---|
| 585 | unsigned long* _p1 = P1; \ |
---|
| 586 | const unsigned long* _p2 = P2; \ |
---|
| 587 | const unsigned long* _p3 = P3; \ |
---|
| 588 | unsigned long l = L; \ |
---|
| 589 | \ |
---|
| 590 | do \ |
---|
| 591 | { \ |
---|
| 592 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 593 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 594 | l -=2; \ |
---|
| 595 | } \ |
---|
| 596 | while(l); \ |
---|
| 597 | } \ |
---|
| 598 | while(0) |
---|
[e48debd] | 599 | |
---|
[a92ff6] | 600 | #define memadd_nwONE(P1, P2, P3) \ |
---|
| 601 | do \ |
---|
| 602 | { \ |
---|
| 603 | unsigned long* _p1 = P1; \ |
---|
| 604 | const unsigned long* _p2 = P2; \ |
---|
| 605 | const unsigned long* _p3 = P3; \ |
---|
| 606 | \ |
---|
| 607 | *_p1 = *_p2 + *_p3; \ |
---|
| 608 | } \ |
---|
| 609 | while(0) |
---|
| 610 | |
---|
| 611 | #define memadd_nwTWO(P1, P2, P3) \ |
---|
| 612 | do \ |
---|
| 613 | { \ |
---|
| 614 | unsigned long* _p1 = P1; \ |
---|
| 615 | const unsigned long* _p2 = P2; \ |
---|
| 616 | const unsigned long* _p3 = P3; \ |
---|
| 617 | \ |
---|
| 618 | *_p1++ = *_p2++ + *_p3++; \ |
---|
| 619 | *_p1 = *_p2 + *_p3; \ |
---|
| 620 | } \ |
---|
| 621 | while(0) |
---|
| 622 | |
---|
| 623 | #define memsetW(P1, W, L) \ |
---|
| 624 | do \ |
---|
| 625 | { \ |
---|
[b7b08c] | 626 | long* _p1 = (long*) P1; \ |
---|
[a92ff6] | 627 | unsigned long _l = L; \ |
---|
[b7b08c] | 628 | long _w = (long) W; \ |
---|
[a92ff6] | 629 | \ |
---|
| 630 | while(_l) \ |
---|
| 631 | { \ |
---|
| 632 | *_p1++ = W; \ |
---|
| 633 | _l--; \ |
---|
| 634 | } \ |
---|
| 635 | } \ |
---|
| 636 | while(0) |
---|
| 637 | |
---|
| 638 | #endif /* DO_DEEP_PROFILE */ |
---|
| 639 | |
---|
| 640 | #ifdef __cplusplus |
---|
| 641 | } |
---|
[9235af] | 642 | int mmInit(); |
---|
[a92ff6] | 643 | #endif |
---|
| 644 | |
---|
[b7b08c] | 645 | #if ! defined(MDEBUG) |
---|
[d6681d] | 646 | inline void* _AllocHeap(memHeap heap) |
---|
[b7b08c] | 647 | { |
---|
| 648 | void* ptr; |
---|
| 649 | mmAllocHeap(ptr, heap); |
---|
| 650 | return ptr; |
---|
| 651 | } |
---|
[d6681d] | 652 | inline void* _Alloc0Heap(memHeap heap) |
---|
[b7b08c] | 653 | { |
---|
| 654 | void* ptr; |
---|
| 655 | mmAllocHeap(ptr, heap); |
---|
| 656 | memsetW(ptr, 0, (heap->size) >> LOG_SIZEOF_LONG); |
---|
| 657 | return ptr; |
---|
| 658 | } |
---|
| 659 | #endif |
---|
| 660 | |
---|
[a92ff6] | 661 | |
---|
| 662 | #endif |
---|