source: git/Singular/mmemory.h @ 6e5833

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