1 | #ifndef MM_HEAP_H |
---|
2 | #define MM_HEAP_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mmheap.h,v 1.13 2000-02-01 15:30:25 Singular Exp $ */ |
---|
7 | |
---|
8 | #include "structs.h" |
---|
9 | #include <stdlib.h> |
---|
10 | |
---|
11 | |
---|
12 | #ifdef __cplusplus |
---|
13 | extern "C" { |
---|
14 | #endif |
---|
15 | |
---|
16 | /***************************************************************** |
---|
17 | * |
---|
18 | * Basic routines, see the beginning of mmheap.c for customizations |
---|
19 | * |
---|
20 | *****************************************************************/ |
---|
21 | /* Initializes Heap */ |
---|
22 | extern void mmInitHeap(memHeap heap, size_t size); |
---|
23 | /* creates and initializes heap */ |
---|
24 | extern memHeap mmCreateHeap(size_t size); |
---|
25 | |
---|
26 | #ifndef HEAP_DEBUG |
---|
27 | /* UNCONDITIONALLY frees all pages of heap */ |
---|
28 | extern void mmClearHeap(memHeap heap); |
---|
29 | /* UNCONDITIONALLY Clears and destroys heap */ |
---|
30 | extern void mmDestroyHeap(memHeap *heap); |
---|
31 | #endif |
---|
32 | |
---|
33 | /* if all cunks of one page are in free list, then removes these |
---|
34 | chunks from freelist |
---|
35 | if strict == 0, does it only if current free pointer is different |
---|
36 | from what it was the last time we did a GC |
---|
37 | */ |
---|
38 | extern void mmGarbageCollectHeap(memHeap heap, int strict); |
---|
39 | |
---|
40 | /* Merges what is free in Heap "what" into free list of heap "into" */ |
---|
41 | void mmMergeHeap(memHeap into, memHeap what); |
---|
42 | |
---|
43 | /* Removes addr from freelist of heap, provided it finds it there */ |
---|
44 | void mmRemoveFromCurrentHeap(memHeap heap, void* addr); |
---|
45 | |
---|
46 | |
---|
47 | /***************************************************************** |
---|
48 | * |
---|
49 | * Alloc, Free and Check for Heaps |
---|
50 | * |
---|
51 | *****************************************************************/ |
---|
52 | #ifndef HEAP_DEBUG |
---|
53 | |
---|
54 | #define mmAllocHeap(res, heap) _mmAllocHeap(res, heap) |
---|
55 | #define mmFreeHeap(addr, heap) _mmFreeHeap(addr, heap) |
---|
56 | |
---|
57 | #define mmCheckHeap(heap) 1 |
---|
58 | #define mmCheckHeapAddr(addr, heap) 1 |
---|
59 | |
---|
60 | #else |
---|
61 | /* |
---|
62 | * define HEAP_DEBUG and/or set mm_HEAP_DEBUG to |
---|
63 | * 0 to enable basic heap addr checks (at least on each alloc/free) |
---|
64 | * 1 for addtl. containment checks in free/alloc list of heap |
---|
65 | * 2 for addtl. check of entire heap at each heap addr check |
---|
66 | * NOTE: For HEAP_DEBUG > 1 on, it gets very slow |
---|
67 | */ |
---|
68 | extern int mm_HEAP_DEBUG; |
---|
69 | |
---|
70 | #define mmAllocHeap(res, heap)\ |
---|
71 | (res) = mmDebugAllocHeap(heap, __FILE__, __LINE__) |
---|
72 | void * mmDebugAllocHeap(memHeap heap, const char*, int ); |
---|
73 | |
---|
74 | #define mmFreeHeap(addr, heap)\ |
---|
75 | mmDebugFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
76 | void mmDebugFreeHeap(void* addr, memHeap heap, const char*, int ); |
---|
77 | |
---|
78 | #define mmCheckHeap(heap)\ |
---|
79 | mmDebugCheckHeap(heap, __FILE__, __LINE__) |
---|
80 | int mmDebugCheckHeap(memHeap heap, const char* fn, int line); |
---|
81 | |
---|
82 | #define mmCheckHeapAddr(addr, heap) \ |
---|
83 | mmDebugCheckHeapAdr(addr, heap, MM_HEAP_ADDR_USED_FLAG, __FILE__, __LINE__) |
---|
84 | int mmDebugCheckHeapAddr(void* addr, memHeap heap, int flag, |
---|
85 | const char* fn, int l); |
---|
86 | |
---|
87 | |
---|
88 | #define mmClearHeap(h) mmDebugClearHeap(h, __FILE__, __LINE__) |
---|
89 | #define mmDestroyHeap(h) mmDebugDestroyHeap(h, __FILE__, __LINE__) |
---|
90 | void mmDebugClearHeap(memHeap heap, const char* fn, int line); |
---|
91 | void mmDebugDestroyHeap(memHeap *heap, const char* fn, int line); |
---|
92 | #endif |
---|
93 | |
---|
94 | /* use this for unknown heaps */ |
---|
95 | #define MM_UNKNOWN_HEAP ((memHeap) 1) |
---|
96 | |
---|
97 | /***************************************************************** |
---|
98 | * |
---|
99 | * Low-level allocation/ routines and declarations |
---|
100 | * |
---|
101 | *****************************************************************/ |
---|
102 | |
---|
103 | struct sip_memHeapPage |
---|
104 | { |
---|
105 | memHeapPage next; |
---|
106 | long counter; |
---|
107 | }; |
---|
108 | |
---|
109 | /* Change this appropriately, if you change sip_memHeapPage */ |
---|
110 | /* However, make sure that sizeof(sip_memHeapPage) is a multiple of 8 */ |
---|
111 | #define SIZE_OF_HEAP_PAGE_HEADER (SIZEOF_VOIDP + SIZEOF_LONG) |
---|
112 | #define SIZE_OF_HEAP_PAGE (SIZE_OF_PAGE - SIZE_OF_HEAP_PAGE_HEADER) |
---|
113 | |
---|
114 | struct sip_memHeap |
---|
115 | { |
---|
116 | void* current; /* Freelist pointer */ |
---|
117 | memHeapPage pages; /* Pointer to linked list of pages */ |
---|
118 | void* last_gc; /* current pointer after last gc */ |
---|
119 | int size; /* Size of heap chunks */ |
---|
120 | }; |
---|
121 | |
---|
122 | |
---|
123 | extern void mmAllocNewHeapPage(memHeap heap); |
---|
124 | |
---|
125 | /* Allocates memory block from a heap */ |
---|
126 | #define _mmAllocHeap(what, heap) \ |
---|
127 | do \ |
---|
128 | { \ |
---|
129 | register memHeap _heap = heap; \ |
---|
130 | if ((_heap)->current == NULL) mmAllocNewHeapPage(_heap); \ |
---|
131 | (void*) (what) = (void *)((_heap)->current); \ |
---|
132 | (_heap)->current = *((void**)(_heap)->current); \ |
---|
133 | } \ |
---|
134 | while (0) |
---|
135 | |
---|
136 | /* Frees addr into heap, assumes addr was previously allocated from heap */ |
---|
137 | #define _mmFreeHeap(addr, heap) \ |
---|
138 | do \ |
---|
139 | { \ |
---|
140 | register memHeap _heap = heap; \ |
---|
141 | *((void**) addr) = (_heap)->current; \ |
---|
142 | (_heap)->current = (void*) addr; \ |
---|
143 | } \ |
---|
144 | while (0) |
---|
145 | |
---|
146 | #define MM_HEAP_ADDR_UNKNOWN_FLAG 0 |
---|
147 | #define MM_HEAP_ADDR_USED_FLAG 1 |
---|
148 | #define MM_HEAP_ADDR_FREE_FLAG 2 |
---|
149 | |
---|
150 | #ifdef __cplusplus |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | #endif /* MM_HEAP_H */ |
---|
155 | |
---|