1 | #ifndef MMEMORY_H |
---|
2 | #define MMEMORY_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mmemory.h,v 1.19 1999-06-30 11:53:59 Singular Exp $ */ |
---|
7 | /* |
---|
8 | * ABSTRACT |
---|
9 | */ |
---|
10 | #include <stdlib.h> |
---|
11 | |
---|
12 | #ifdef __cplusplus |
---|
13 | extern "C" { |
---|
14 | #endif |
---|
15 | |
---|
16 | #include "structs.h" |
---|
17 | #include "mmheap.h" |
---|
18 | |
---|
19 | /********************************************************************** |
---|
20 | * |
---|
21 | * Memory allocation |
---|
22 | * |
---|
23 | **********************************************************************/ |
---|
24 | |
---|
25 | #ifndef MDEBUG |
---|
26 | |
---|
27 | void * mmAllocBlock( size_t ); |
---|
28 | void * mmAllocBlock0( size_t ); |
---|
29 | void mmFreeBlock( void*, size_t ); |
---|
30 | void * mmReallocBlock( void*, size_t, size_t ); |
---|
31 | void * mmReallocBlock0( void*, size_t, size_t ); |
---|
32 | void * mmAlloc( size_t ); |
---|
33 | void * mmRealloc( void*, size_t ); |
---|
34 | void mmFree( void* ); |
---|
35 | char * mmStrdup( const char* ); |
---|
36 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
37 | void * mmAllocAlignedBlock( size_t ); |
---|
38 | void * mmAllocAlignedBlock0( size_t ); |
---|
39 | void mmFreeAlignedBlock( void*, size_t ); |
---|
40 | #endif |
---|
41 | |
---|
42 | #define AllocHeap mmAllocHeap |
---|
43 | #define FreeHeap mmFreeHeap |
---|
44 | #define Alloc mmAllocBlock |
---|
45 | #define Alloc0 mmAllocBlock0 |
---|
46 | #define Free mmFreeBlock |
---|
47 | #define ReAlloc mmReallocBlock |
---|
48 | #define ReAlloc0 mmReallocBlock0 |
---|
49 | #define FreeL mmFree |
---|
50 | #define AllocL mmAlloc |
---|
51 | #define mstrdup mmStrdup |
---|
52 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
53 | #define AllocAligned0 mmAllocAlignedBlock0 |
---|
54 | #define AllocAligned mmAllocAlignedBlock |
---|
55 | #define FreeAligned mmFreeAlignedBlock |
---|
56 | #elif SIZEOF_DOUBLE == SIZEOF_VOIDP |
---|
57 | #define AllocAligned0 mmAllocBlock0 |
---|
58 | #define AllocAligned mmAllocBlock |
---|
59 | #define FreeAligned mmFreeBlock |
---|
60 | #endif |
---|
61 | |
---|
62 | #else /* MDEBUG */ |
---|
63 | |
---|
64 | void * mmDBAllocHeap(memHeap heap, char*, int ); |
---|
65 | void mmDBFreeHeap(void* addr, memHeap heap, char*, int ); |
---|
66 | void * mmDBAllocBlock( size_t, char*, int ); |
---|
67 | void * mmDBAllocBlock0( size_t, char*, int); |
---|
68 | void mmDBFreeBlock( void*, size_t, char*, int); |
---|
69 | void * mmDBReallocBlock( void*, size_t, size_t, char*, int ); |
---|
70 | void * mmDBReallocBlock0( void*, size_t, size_t, char*, int ); |
---|
71 | void * mmDBAlloc( size_t, char*, int ); |
---|
72 | void * mmDBRealloc( void*, size_t, char*, int ); |
---|
73 | void mmDBFree( void*, char*, int ); |
---|
74 | char * mmDBStrdup( const char * s, char *fname, int lineno); |
---|
75 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
76 | void * mmDBAllocAlignedBlock( size_t, char*, int ); |
---|
77 | void * mmDBAllocAlignedBlock0( size_t, char*, int); |
---|
78 | void mmDBFreeAlignedBlock( void*, size_t, char*, int ); |
---|
79 | #endif |
---|
80 | |
---|
81 | #define AllocHeap(res, heap)\ |
---|
82 | (res) = mmDBAllocHeap(heap, __FILE__, __LINE__) |
---|
83 | #define FreeHeap(addr, heap)\ |
---|
84 | mmDBFreeHeap(addr, heap, __FILE__, __LINE__) |
---|
85 | #define Alloc(s) mmDBAllocBlock(s, __FILE__, __LINE__) |
---|
86 | #define Alloc0(s) mmDBAllocBlock0(s, __FILE__, __LINE__) |
---|
87 | #define Free(a,s) mmDBFreeBlock(a, s, __FILE__, __LINE__) |
---|
88 | #define ReAlloc(a,o,n) mmDBReallocBlock(a, o, n, __FILE__, __LINE__) |
---|
89 | #define ReAlloc0(a,o,n) mmDBReallocBlock0(a, o, n, __FILE__, __LINE__) |
---|
90 | #define AllocL(s) mmDBAlloc(s, __FILE__, __LINE__) |
---|
91 | #define FreeL(a) mmDBFree(a,__FILE__,__LINE__) |
---|
92 | #define mstrdup(s) mmDBStrdup(s, __FILE__, __LINE__) |
---|
93 | #if SIZEOF_DOUBLE == SIZEOF_VOIDP + SIZEOF_VOIDP |
---|
94 | #define AllocAligned(s) mmDBAllocAlignedBlock(s, __FILE__, __LINE__) |
---|
95 | #define AllocAligned0(s) mmDBAllocAlignedBlock0(s, __FILE__, __LINE__) |
---|
96 | #define FreeAligned(a,s) mmDBFreeAlignedBlock(a, s, __FILE__, __LINE__) |
---|
97 | #elif SIZEOF_DOUBLE == SIZEOF_VOIDP |
---|
98 | #define AllocAligned(s) mmDBAllocBlock(s, __FILE__, __LINE__) |
---|
99 | #define AllocAligned0(s) mmDBAllocBlock0(s, __FILE__, __LINE__) |
---|
100 | #define FreeAligned(a,s) mmDBFreeBlock(a, s, __FILE__, __LINE__) |
---|
101 | #endif |
---|
102 | |
---|
103 | #endif /* MDEBUG */ |
---|
104 | |
---|
105 | |
---|
106 | /********************************************************************** |
---|
107 | * |
---|
108 | * Tests of memory (for MDEBUG, only) |
---|
109 | * |
---|
110 | **********************************************************************/ |
---|
111 | |
---|
112 | #ifdef MDEBUG |
---|
113 | |
---|
114 | /* use this variables to control level of MDEBUG at run-time |
---|
115 | (see mod2.h for details) */ |
---|
116 | extern int mm_MDEBUG; |
---|
117 | |
---|
118 | BOOLEAN mmDBTestHeapBlock(const void* adr, const memHeap heap, |
---|
119 | const char * fname, const int lineno ); |
---|
120 | BOOLEAN mmDBTestBlock(const void* adr, const size_t size, |
---|
121 | const char * fname, const int lineno ); |
---|
122 | BOOLEAN mmDBTest(const void* adr, const char * fname, const int lineno); |
---|
123 | |
---|
124 | #define mmTestHeap(a, h)\ |
---|
125 | mmDBTestHeapBlock(a, h, __FILE__, __LINE__) |
---|
126 | #define mmTest(A,B) mmDBTestBlock(A,B,__FILE__,__LINE__) |
---|
127 | #define mmTestL(A) mmDBTest(A,__FILE__,__LINE__) |
---|
128 | #define mmTestP(A,B) mmDBTestBlock(A,B,__FILE__,__LINE__) |
---|
129 | #define mmTestLP(A) mmDBTest(A,__FILE__,__LINE__) |
---|
130 | |
---|
131 | int mmTestMemory(); |
---|
132 | int mmTestHeaps(); |
---|
133 | |
---|
134 | void mmPrintUsedList(); |
---|
135 | void mmMarkInitDBMCB(); |
---|
136 | void mmTestList (int all); |
---|
137 | |
---|
138 | #else |
---|
139 | |
---|
140 | |
---|
141 | #define mmTestHeap(addr, heap) mmCheckHeapAddr(addr, heap) |
---|
142 | #define mmTest(A,B) TRUE |
---|
143 | #define mmTestL(A) TRUE |
---|
144 | #define mmTestP(A,B) |
---|
145 | #define mmTestLP(A) |
---|
146 | #define mmTestMemory 1 |
---|
147 | #define mmTestHeaps 1 |
---|
148 | #define mmMarkInitDBMCB() |
---|
149 | #define mmTestList(a) |
---|
150 | |
---|
151 | #endif /* MDEBUG */ |
---|
152 | |
---|
153 | |
---|
154 | /********************************************************************** |
---|
155 | * |
---|
156 | * Misc stuff |
---|
157 | * |
---|
158 | **********************************************************************/ |
---|
159 | |
---|
160 | /* For handling of monomials */ |
---|
161 | size_t mmSpecializeBlock( size_t ); |
---|
162 | size_t mmGetSpecSize(); |
---|
163 | extern memHeap mm_specHeap; |
---|
164 | |
---|
165 | /* for handling of memory statistics */ |
---|
166 | int mmMemAlloc( void ); |
---|
167 | int mmMemUsed( void ); |
---|
168 | #ifdef HAVE_SBRK |
---|
169 | int mmMemPhysical( void ); |
---|
170 | #endif |
---|
171 | void mmPrintStat(); |
---|
172 | |
---|
173 | size_t mmSizeL( void* ); |
---|
174 | |
---|
175 | /* max size of blocks which our memory managment handles */ |
---|
176 | #define MAX_BLOCK_SIZE (((SIZE_OF_HEAP_PAGE) / 16)*4) |
---|
177 | |
---|
178 | /********************************************************************** |
---|
179 | * |
---|
180 | * Some operations on linked lists of memory |
---|
181 | * |
---|
182 | **********************************************************************/ |
---|
183 | /* The following routines assume that Next(list) == *((void**) list) */ |
---|
184 | /* Removes element from list, if contained in it and returns list */ |
---|
185 | void* mmRemoveFromList(void* list, void* element); |
---|
186 | /* Returns the length of a memory list; assumes list has no cycles */ |
---|
187 | int mmListLength(void* list); |
---|
188 | /* Returns last non-NULL element of list; assumes list has no cycles */ |
---|
189 | void* mmListLast(void* list); |
---|
190 | /* returns 1, if addr is contained in memory list |
---|
191 | * 0, otherwise */ |
---|
192 | int mmIsAddrOnList(void* addr, void* list); |
---|
193 | /* Checks whether memory list has cycles: If yes, returns address of |
---|
194 | * first element of list which is contained at least twice in memory |
---|
195 | * list. If no, NULL is returned */ |
---|
196 | void* mmListHasCycle(void* list); |
---|
197 | |
---|
198 | /* The following routines assume that Next(list) == *((void**) list + next) */ |
---|
199 | |
---|
200 | /* Returns the length of a memory list; assumes list has no cycles */ |
---|
201 | int mmGListLength(void* list, int next); |
---|
202 | /* Returns last non-NULL element of list; assumes list has no cycles */ |
---|
203 | void* mmGListLast(void* list, int next); |
---|
204 | /* returns 1, if addr is contained in memory list |
---|
205 | * 0, otherwise */ |
---|
206 | int mmIsAddrOnGList(void* addr, void* list, int next); |
---|
207 | /* Checks whether memory list has cycles: If yes, returns address of |
---|
208 | * first element of list which is contained at least twice in memory |
---|
209 | * list. If no, NULL is returned */ |
---|
210 | void* mmGListHasCycle(void* list, int next); |
---|
211 | |
---|
212 | /********************************************************************** |
---|
213 | * |
---|
214 | * some fast macros for basic memory operations |
---|
215 | * |
---|
216 | **********************************************************************/ |
---|
217 | #ifdef DO_DEEP_PROFILE |
---|
218 | extern void _memcpyW(void* p1, void* p2, long l); |
---|
219 | #define memcpy_nwEVEN(p1, p2, l) _memcpyW((void*) p1, (void*) p2, (long) l) |
---|
220 | #define memcpy_nwODD(p1, p2, l) _memcpyW((void*) p1, (void*) p2, (long) l) |
---|
221 | #define memcpyW(p1, p2, l) _memcpyW((void*) p1, (void*) p2, (long) l) |
---|
222 | |
---|
223 | extern void _memaddW(void* p1, void* p2, void* p3, long l); |
---|
224 | #define memaddW(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
225 | #define memadd_nwODD(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
226 | #define memadd_nwEVEN(p1, p2, p3, l) _memaddW(p1, p2, p3, l) |
---|
227 | #define memadd_nwONE(p1, p2, p3) _memaddW(p1, p2, p3, 1) |
---|
228 | #define memadd_nwTWO(p1, p2, p3) _memaddW(p1, p2, p3, 2) |
---|
229 | |
---|
230 | extern void _memsetW(void* p1, long w, long l); |
---|
231 | #define memsetW(p1, w, l) _memsetW(p1, w, l) |
---|
232 | |
---|
233 | #else /* ! DO_DEEP_PROFILE */ |
---|
234 | |
---|
235 | #define memcpyW(p1, p2, l) \ |
---|
236 | do \ |
---|
237 | { \ |
---|
238 | long _i = l; \ |
---|
239 | long* _s1 = (long*) p1; \ |
---|
240 | const long* _s2 = (long*) p2; \ |
---|
241 | \ |
---|
242 | for (;;) \ |
---|
243 | { \ |
---|
244 | *_s1 = *_s2; \ |
---|
245 | _i--; \ |
---|
246 | if (_i == 0) break; \ |
---|
247 | _s1++; \ |
---|
248 | _s2++; \ |
---|
249 | } \ |
---|
250 | } \ |
---|
251 | while(0) |
---|
252 | |
---|
253 | #define memcpy_nwODD(p1, p2, l) \ |
---|
254 | do \ |
---|
255 | { \ |
---|
256 | long _i = l - 1; \ |
---|
257 | long* _s1 = (long*) p1; \ |
---|
258 | const long* _s2 = (long*) p2; \ |
---|
259 | \ |
---|
260 | *_s1++ = *_s2++; \ |
---|
261 | for (;;) \ |
---|
262 | { \ |
---|
263 | *_s1++ = *_s2++; \ |
---|
264 | *_s1++ = *_s2++; \ |
---|
265 | _i -= 2; \ |
---|
266 | if (_i == 0) break; \ |
---|
267 | } \ |
---|
268 | } \ |
---|
269 | while(0) |
---|
270 | |
---|
271 | #define memcpy_nwEVEN(p1, p2, l) \ |
---|
272 | do \ |
---|
273 | { \ |
---|
274 | long _i = l; \ |
---|
275 | long* _s1 = (long*) p1; \ |
---|
276 | const long* _s2 = (long*) p2; \ |
---|
277 | \ |
---|
278 | for (;;) \ |
---|
279 | { \ |
---|
280 | *_s1++ = *_s2++; \ |
---|
281 | *_s1++ = *_s2++; \ |
---|
282 | _i -= 2; \ |
---|
283 | if (_i == 0) break; \ |
---|
284 | } \ |
---|
285 | } \ |
---|
286 | while(0) |
---|
287 | |
---|
288 | #define memaddW(P1, P2, P3, L) \ |
---|
289 | do \ |
---|
290 | { \ |
---|
291 | unsigned long* _p1 = P1; \ |
---|
292 | const unsigned long* _p2 = P2; \ |
---|
293 | const unsigned long* _p3 = P3; \ |
---|
294 | unsigned long l = L; \ |
---|
295 | \ |
---|
296 | do \ |
---|
297 | { \ |
---|
298 | *_p1++ = *_p2++ + *_p3++; \ |
---|
299 | l--; \ |
---|
300 | } \ |
---|
301 | while(l); \ |
---|
302 | } \ |
---|
303 | while(0) |
---|
304 | |
---|
305 | #define memadd_nwODD(P1, P2, P3, L) \ |
---|
306 | do \ |
---|
307 | { \ |
---|
308 | unsigned long* _p1 = P1; \ |
---|
309 | const unsigned long* _p2 = P2; \ |
---|
310 | const unsigned long* _p3 = P3; \ |
---|
311 | unsigned long l = L; \ |
---|
312 | \ |
---|
313 | *_p1++ = *_p2++ + *_p3++; \ |
---|
314 | l--; \ |
---|
315 | \ |
---|
316 | do \ |
---|
317 | { \ |
---|
318 | *_p1++ = *_p2++ + *_p3++; \ |
---|
319 | *_p1++ = *_p2++ + *_p3++; \ |
---|
320 | l -=2; \ |
---|
321 | } \ |
---|
322 | while(l); \ |
---|
323 | } \ |
---|
324 | while(0) |
---|
325 | |
---|
326 | #define memadd_nwEVEN(P1, P2, P3, L) \ |
---|
327 | do \ |
---|
328 | { \ |
---|
329 | unsigned long* _p1 = P1; \ |
---|
330 | const unsigned long* _p2 = P2; \ |
---|
331 | const unsigned long* _p3 = P3; \ |
---|
332 | unsigned long l = L; \ |
---|
333 | \ |
---|
334 | do \ |
---|
335 | { \ |
---|
336 | *_p1++ = *_p2++ + *_p3++; \ |
---|
337 | *_p1++ = *_p2++ + *_p3++; \ |
---|
338 | l -=2; \ |
---|
339 | } \ |
---|
340 | while(l); \ |
---|
341 | } \ |
---|
342 | while(0) |
---|
343 | |
---|
344 | #define memadd_nwONE(P1, P2, P3) \ |
---|
345 | do \ |
---|
346 | { \ |
---|
347 | unsigned long* _p1 = P1; \ |
---|
348 | const unsigned long* _p2 = P2; \ |
---|
349 | const unsigned long* _p3 = P3; \ |
---|
350 | \ |
---|
351 | *_p1 = *_p2 + *_p3; \ |
---|
352 | } \ |
---|
353 | while(0) |
---|
354 | |
---|
355 | #define memadd_nwTWO(P1, P2, P3) \ |
---|
356 | do \ |
---|
357 | { \ |
---|
358 | unsigned long* _p1 = P1; \ |
---|
359 | const unsigned long* _p2 = P2; \ |
---|
360 | const unsigned long* _p3 = P3; \ |
---|
361 | \ |
---|
362 | *_p1++ = *_p2++ + *_p3++; \ |
---|
363 | *_p1 = *_p2 + *_p3; \ |
---|
364 | } \ |
---|
365 | while(0) |
---|
366 | |
---|
367 | #define memsetW(P1, W, L) \ |
---|
368 | do \ |
---|
369 | { \ |
---|
370 | long* _p1 = P1; \ |
---|
371 | unsigned long _l = L; \ |
---|
372 | long _w = W; \ |
---|
373 | \ |
---|
374 | while(_l) \ |
---|
375 | { \ |
---|
376 | *_p1++ = W; \ |
---|
377 | _l--; \ |
---|
378 | } \ |
---|
379 | } \ |
---|
380 | while(0) |
---|
381 | |
---|
382 | #endif /* DO_DEEP_PROFILE */ |
---|
383 | |
---|
384 | #ifdef __cplusplus |
---|
385 | } |
---|
386 | #endif |
---|
387 | |
---|
388 | |
---|
389 | #endif |
---|