1 | #ifndef MEMHEAP_H |
---|
2 | #define MEMHEAP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mmheap.h,v 1.8 1999-06-08 07:37:18 Singular Exp $ */ |
---|
7 | #include <stdlib.h> |
---|
8 | #include "mod2.h" |
---|
9 | #include "structs.h" |
---|
10 | #include "mmpage.h" |
---|
11 | |
---|
12 | |
---|
13 | #ifdef __cplusplus |
---|
14 | extern "C" { |
---|
15 | #endif |
---|
16 | |
---|
17 | /***************************************************************** |
---|
18 | * |
---|
19 | * Declaration and Configuration |
---|
20 | * |
---|
21 | *****************************************************************/ |
---|
22 | /* define to enable automatic "garbage" collection of heaps */ |
---|
23 | /* #define HAVE_AUTOMATIC_HEAP_COLLECTION */ |
---|
24 | |
---|
25 | /***************************************************************** |
---|
26 | * |
---|
27 | * Basic routines |
---|
28 | * |
---|
29 | *****************************************************************/ |
---|
30 | |
---|
31 | /* Initializes Heap, assumes size < SIZE_OF_HEAP_PAGE */ |
---|
32 | extern void mmInitHeap(memHeap heap, size_t size); |
---|
33 | /* creates and initializes heap */ |
---|
34 | extern memHeap mmCreateHeap(size_t size); |
---|
35 | |
---|
36 | /* UNCONDITIONALLY frees all pages of heap */ |
---|
37 | extern void mmClearHeap(memHeap heap); |
---|
38 | /* UNCONDITIONALLY Clears and destroys heap */ |
---|
39 | void mmDestroyHeap(memHeap *heap); |
---|
40 | |
---|
41 | /* Tries to free as many unused pages of heap as possible */ |
---|
42 | #if defined(HAVE_AUTOMATIC_HEAP_COLLECTION) && defined(HAVE_PAGE_ALIGNMENT) |
---|
43 | /* CleanUpHeap does not do anything when automatic heap collection is on */ |
---|
44 | #define mmCleanHeap(heap) |
---|
45 | extern void _mmCleanHeap(memHeap heap); |
---|
46 | #else |
---|
47 | extern void mmCleanHeap(memHeap heap); |
---|
48 | #endif |
---|
49 | |
---|
50 | /* Merges what is free in Heap "what" into free list of heap "into" */ |
---|
51 | void mmMergeHeap(memHeap into, memHeap what); |
---|
52 | |
---|
53 | /***************************************************************** |
---|
54 | * |
---|
55 | * Alloc, Free and Check for Heaps |
---|
56 | * |
---|
57 | *****************************************************************/ |
---|
58 | |
---|
59 | #ifndef HEAP_DEBUG |
---|
60 | |
---|
61 | #define mmAllocHeap(res, heap) _mmAllocHeap(res, heap) |
---|
62 | #define mmFreeHeap(addr, heap) _mmFreeHeap(addr, heap) |
---|
63 | |
---|
64 | #define mmCheckHeap(heap) 1 |
---|
65 | #define mmCheckHeapAddr(addr, heap) 1 |
---|
66 | |
---|
67 | #else |
---|
68 | |
---|
69 | /* use this variables to control level of HEAP_DEBUG at run-time |
---|
70 | (see mod2.h for details) */ |
---|
71 | extern int mm_HEAP_DEBUG; |
---|
72 | |
---|
73 | #define mmAllocHeap(res, heap)\ |
---|
74 | (res) = mmDebugAllocHeap(heap, __FILE__, __LINE__) |
---|
75 | void * mmDebugAllocHeap(memHeap heap, const char*, int ); |
---|
76 | |
---|
77 | #define mmFreeHeap(addr, heap)\ |
---|
78 | mmDebugFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
79 | void mmDebugFreeHeap(void* addr, memHeap heap, const char*, int ); |
---|
80 | |
---|
81 | #define mmCheckHeap(heap)\ |
---|
82 | mmDebugCheckHeap(heap, __FILE__, __LINE__) |
---|
83 | int mmDebugCheckHeap(memHeap heap, const char* fn, int line); |
---|
84 | |
---|
85 | #define mmCheckHeapAddr(addr, heap) \ |
---|
86 | mmDebugCheckHeapAdr(addr, heap, MM_HEAP_ADDR_USED_FLAG, __FILE__, __LINE__) |
---|
87 | int mmDebugCheckHeapAddr(void* addr, memHeap heap, int flag, |
---|
88 | const char* fn, int l); |
---|
89 | |
---|
90 | #endif |
---|
91 | |
---|
92 | /***************************************************************** |
---|
93 | * |
---|
94 | * Low-level allocation routines |
---|
95 | * |
---|
96 | *****************************************************************/ |
---|
97 | |
---|
98 | struct sip_memHeapPage; |
---|
99 | typedef struct sip_memHeapPage * memHeapPage; |
---|
100 | |
---|
101 | struct sip_memHeapPage |
---|
102 | { |
---|
103 | memHeapPage next; |
---|
104 | long counter; |
---|
105 | }; |
---|
106 | |
---|
107 | struct sip_memHeap |
---|
108 | { |
---|
109 | void* current; |
---|
110 | memHeapPage pages; |
---|
111 | size_t size; |
---|
112 | }; |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | #define SIZE_OF_HEAP_PAGE_HEADER (SIZEOF_VOIDP + SIZEOF_LONG) |
---|
117 | |
---|
118 | #ifdef HAVE_PAGE_ALIGNMENT |
---|
119 | |
---|
120 | #define mmGetHeapPageOfAddr(addr) (memHeapPage) mmGetPageOfAddr(addr) |
---|
121 | |
---|
122 | #define mmIncrHeapPageCounterOfAddr(addr) \ |
---|
123 | do \ |
---|
124 | { \ |
---|
125 | register memHeapPage page = mmGetHeapPageOfAddr(addr); \ |
---|
126 | (page->counter)++; \ |
---|
127 | } \ |
---|
128 | while (0) |
---|
129 | |
---|
130 | extern void mmRemoveHeapBlocksOfPage(memHeap heap, memHeapPage hpage); |
---|
131 | |
---|
132 | #define mmDecrCurrentHeapPageCounter(heap) \ |
---|
133 | do \ |
---|
134 | { \ |
---|
135 | register memHeapPage page = mmGetHeapPageOfAddr((heap)->current); \ |
---|
136 | if (--(page->counter) == 0) mmRemoveHeapBlocksOfPage(heap, page); \ |
---|
137 | } \ |
---|
138 | while (0) |
---|
139 | #endif /* HAVE_PAGE_ALIGNMENT */ |
---|
140 | |
---|
141 | |
---|
142 | #define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZE_OF_HEAP_PAGE_HEADER) |
---|
143 | #define mmGetHeapBlockSize(heap) ((heap)->size) |
---|
144 | |
---|
145 | extern void mmAllocNewHeapPage(memHeap heap); |
---|
146 | |
---|
147 | #if defined (HAVE_PAGE_ALIGNMENT) && defined(HAVE_AUTOMATIC_HEAP_COLLECTION) |
---|
148 | /* Allocates memory block from a heap */ |
---|
149 | #define _mmAllocHeap(what, heap) \ |
---|
150 | do \ |
---|
151 | { \ |
---|
152 | register memHeap _heap = heap; \ |
---|
153 | if ((_heap)->current == NULL) mmAllocNewHeapPage(_heap); \ |
---|
154 | mmIncrHeapPageCounterOfAddr((_heap)->current); \ |
---|
155 | (what) = (void *)(_heap)->current; \ |
---|
156 | (_heap)->current = *((void**)(_heap)->current); \ |
---|
157 | } \ |
---|
158 | while (0) |
---|
159 | |
---|
160 | /* Frees addr into heap, assumes addr was previously allocated from heap */ |
---|
161 | #define _mmFreeHeap(addr, heap) \ |
---|
162 | do \ |
---|
163 | { \ |
---|
164 | register memHeap _heap = heap; \ |
---|
165 | *((void**) addr) = (_heap)->current; \ |
---|
166 | (_heap)->current = (void*) addr; \ |
---|
167 | mmDecrCurrentHeapPageCounter(_heap); \ |
---|
168 | } \ |
---|
169 | while (0) |
---|
170 | |
---|
171 | #else |
---|
172 | |
---|
173 | /* Allocates memory block from a heap */ |
---|
174 | #define _mmAllocHeap(what, heap) \ |
---|
175 | do \ |
---|
176 | { \ |
---|
177 | register memHeap _heap = heap; \ |
---|
178 | if ((_heap)->current == NULL) mmAllocNewHeapPage(_heap); \ |
---|
179 | (what) = (void *)((_heap)->current); \ |
---|
180 | (_heap)->current = *((void**)(_heap)->current); \ |
---|
181 | } \ |
---|
182 | while (0) |
---|
183 | |
---|
184 | /* Frees addr into heap, assumes addr was previously allocated from heap */ |
---|
185 | #define _mmFreeHeap(addr, heap) \ |
---|
186 | do \ |
---|
187 | { \ |
---|
188 | register memHeap _heap = heap; \ |
---|
189 | *((void**) addr) = (_heap)->current; \ |
---|
190 | (_heap)->current = (void*) addr; \ |
---|
191 | } \ |
---|
192 | while (0) |
---|
193 | |
---|
194 | #endif /* defined(HAVE_PAGE_ALIGNMENT) && ... */ |
---|
195 | |
---|
196 | #define MM_HEAP_ADDR_UNKNOWN_FLAG 0 |
---|
197 | #define MM_HEAP_ADDR_USED_FLAG 1 |
---|
198 | #define MM_HEAP_ADDR_FREE_FLAG 2 |
---|
199 | |
---|
200 | #ifdef __cplusplus |
---|
201 | } |
---|
202 | #endif |
---|
203 | |
---|
204 | #endif /* MEMHEAP_H */ |
---|
205 | |
---|