1 | #ifndef MEMHEAP_H |
---|
2 | #define MEMHEAP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mmheap.h,v 1.4 1998-12-16 18:43:41 Singular Exp $ */ |
---|
7 | #include <stdlib.h> |
---|
8 | #include "structs.h" |
---|
9 | #include "mmpage.h" |
---|
10 | |
---|
11 | |
---|
12 | #ifdef __cplusplus |
---|
13 | extern "C" { |
---|
14 | #endif |
---|
15 | /***************************************************************** |
---|
16 | * |
---|
17 | * Declaration and Configuration |
---|
18 | * |
---|
19 | *****************************************************************/ |
---|
20 | struct sip_memHeap |
---|
21 | { |
---|
22 | void* current; |
---|
23 | void* pages; |
---|
24 | size_t size; |
---|
25 | }; |
---|
26 | |
---|
27 | /***************************************************************** |
---|
28 | * |
---|
29 | * Basic routines |
---|
30 | * |
---|
31 | *****************************************************************/ |
---|
32 | |
---|
33 | /* Initializes Heap, assumes size < SIZE_OF_HEAP_PAGE */ |
---|
34 | extern void mmInitHeap(memHeap heap, size_t size); |
---|
35 | /* UNCONDITIONALLY frees all pages of heap */ |
---|
36 | extern void mmClearHeap(memHeap heap); |
---|
37 | /* Tries to free as many unused pages of heap as possible */ |
---|
38 | extern void mmCleanHeap(memHeap heap); |
---|
39 | /* creates and initializes heap */ |
---|
40 | extern memHeap mmCreateHeap(size_t size); |
---|
41 | /* Clears and destroys heap */ |
---|
42 | void mmDestroyHeap(memHeap *heap); |
---|
43 | /* Merges what is free in Heap "what" into free list of heap "into" */ |
---|
44 | void mmMergeHeap(memHeap into, memHeap what); |
---|
45 | |
---|
46 | #define MM_HEAP_ADDR_UNKNOWN_FLAG 0 |
---|
47 | #define MM_HEAP_ADDR_USED_FLAG 1 |
---|
48 | #define MM_HEAP_ADDR_FREE_FLAG 2 |
---|
49 | |
---|
50 | #ifndef HEAP_DEBUG |
---|
51 | |
---|
52 | #define mmAllocHeap _mmAllocHeap |
---|
53 | #define mmFreeHeap _mmFreeHeap |
---|
54 | |
---|
55 | #define mmCheckHeap(heap) 1 |
---|
56 | #define mmCheckHeapAddr(addr, heap) 1 |
---|
57 | |
---|
58 | #else |
---|
59 | |
---|
60 | /* use this variables to control level of HEAP_DEBUG at run-time |
---|
61 | (see mod2.h for details) */ |
---|
62 | extern int mm_HEAP_DEBUG; |
---|
63 | |
---|
64 | #define mmAllocHeap(res, heap)\ |
---|
65 | ((void*)(res)) = mmDebugAllocHeap(heap, __FILE__, __LINE__) |
---|
66 | void * mmDebugAllocHeap(memHeap heap, const char*, int ); |
---|
67 | |
---|
68 | #define mmFreeHeap(addr, heap)\ |
---|
69 | mmDebugFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
70 | void mmDebugFreeHeap(void* addr, memHeap heap, const char*, int ); |
---|
71 | |
---|
72 | #define mmCheckHeap(heap)\ |
---|
73 | mmDebugCheckHeap(heap, __FILE__, __LINE__) |
---|
74 | int mmDebugCheckHeap(memHeap heap, const char* fn, int line); |
---|
75 | |
---|
76 | #define mmCheckHeapAddr(addr, heap) \ |
---|
77 | mmDebugCheckHeapAdr(addr, heap, MM_HEAP_ADDR_USED_FLAG, __FILE__, __LINE__) |
---|
78 | int mmDebugCheckHeapAddr(void* addr, memHeap heap, int flag, |
---|
79 | const char* fn, int l); |
---|
80 | |
---|
81 | #endif |
---|
82 | |
---|
83 | /***************************************************************** |
---|
84 | * |
---|
85 | * Low-level allocation routines |
---|
86 | * |
---|
87 | *****************************************************************/ |
---|
88 | |
---|
89 | /* Allocates memory block from a heap */ |
---|
90 | #define _mmAllocHeap(what, heap) \ |
---|
91 | do \ |
---|
92 | { \ |
---|
93 | if ((heap)->current == NULL) mmAllocNewHeapPage(heap); \ |
---|
94 | ((void*) (what)) = (heap)->current; \ |
---|
95 | (heap)->current = *((void**)(heap)->current); \ |
---|
96 | } \ |
---|
97 | while (0) |
---|
98 | |
---|
99 | /* Frees addr into heap, assumes addr was previously allocated from heap */ |
---|
100 | #define _mmFreeHeap(addr, heap) \ |
---|
101 | do \ |
---|
102 | { \ |
---|
103 | *((void**) addr) = (heap)->current; \ |
---|
104 | (heap)->current = (void*) addr; \ |
---|
105 | } \ |
---|
106 | while (0) |
---|
107 | |
---|
108 | /***************************************************************** |
---|
109 | * |
---|
110 | * Auxillary routines |
---|
111 | * |
---|
112 | *****************************************************************/ |
---|
113 | void mmAllocNewHeapPage(memHeap heap); |
---|
114 | |
---|
115 | #if 0 |
---|
116 | #define mmGetNumberOfAllocatedHeapPages(heap) ((heap)->pnumber) |
---|
117 | #define mmGetNumberOfAllocatedHeapMemory(heap) ((heap)->pnumber * SIZE_OF_PAGE) |
---|
118 | #endif |
---|
119 | #define mmGetHeapBlockSize(heap) ((heap)->size) |
---|
120 | |
---|
121 | #define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZEOF_VOIDP) |
---|
122 | #define SIZE_OF_HEAP_PAGE_HEADER (SIZEOF_VOIDP) |
---|
123 | |
---|
124 | #ifdef __cplusplus |
---|
125 | } |
---|
126 | #endif |
---|
127 | |
---|
128 | #endif /* MEMHEAP_H */ |
---|
129 | |
---|