source: git/Singular/mmemory.h @ 7df2ef

spielwiese
Last change on this file since 7df2ef was 7df2ef, checked in by Olaf Bachmann <obachman@…>, 25 years ago
* reimplemented rInit * added top-level command leadmonom * re-added mmTestList git-svn-id: file:///usr/local/Singular/svn/trunk@2961 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 14.0 KB
Line 
1#ifndef MMEMORY_H
2#define MMEMORY_H
3/****************************************
4*  Computer Algebra System SINGULAR     *
5****************************************/
6/* $Id: mmemory.h,v 1.16 1999-03-19 14:18:01 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();
119
120#else
121
122
123#define mmTestHeap(addr, heap) mmCheckHeapAddr(addr, heap)
124#define mmTest(A,B) TRUE
125#define mmTestL(A)  TRUE
126#define mmTestP(A,B)
127#define mmTestLP(A)
128#define mmTestMemory 1
129#define mmTestHeaps 1
130
131#endif /* MDEBUG */
132
133
134/**********************************************************************
135 *
136 * Misc stuff
137 *
138 **********************************************************************/
139
140/* For handling of monomials */
141size_t mmSpecializeBlock( size_t );
142size_t mmGetSpecSize();
143extern memHeap mm_specHeap;
144
145/* for handling of memory statistics */
146int mmMemAlloc( void );
147int mmMemUsed( void );
148#ifdef HAVE_SBRK
149int mmMemPhysical( void );
150#endif
151void mmPrintStat();
152
153size_t mmSizeL( void* );
154
155/* max size of blocks which our memory managment handles */
156#define MAX_BLOCK_SIZE  (((SIZE_OF_HEAP_PAGE) / 16)*4)
157
158/**********************************************************************
159 *
160 * Some operations on linked lists of memory
161 *
162 **********************************************************************/
163/* The following routines assume that Next(list) == *((void**) list) */
164/* Removes element from list, if contained in it and returns list */
165void* mmRemoveFromList(void* list, void* element);
166/* Returns the length of a memory list; assumes list has no cycles */
167int mmListLength(void* list);
168/* Returns last non-NULL element of list; assumes list has no cycles */
169void* mmListLast(void* list);
170/* returns 1, if addr is contained in memory list
171 * 0, otherwise */
172int mmIsAddrOnList(void* addr, void* list);
173/* Checks whether memory list has cycles: If yes, returns address of
174 * first element of list which is contained at least twice in memory
175 * list. If no, NULL is returned */
176void* mmListHasCycle(void* list);
177
178/* The following routines assume that Next(list) == *((void**) list + next) */
179
180/* Returns the length of a memory list; assumes list has no cycles */
181int mmGListLength(void* list, int next);
182/* Returns last non-NULL element of list; assumes list has no cycles */
183void* mmGListLast(void* list, int next);
184/* returns 1, if addr is contained in memory list
185 * 0, otherwise */
186int mmIsAddrOnGList(void* addr, void* list, int next);
187/* Checks whether memory list has cycles: If yes, returns address of
188 * first element of list which is contained at least twice in memory
189 * list. If no, NULL is returned */
190void* mmGListHasCycle(void* list, int next);
191
192/**********************************************************************
193 *
194 * some fast macros for basic memory operations
195 *
196 **********************************************************************/
197#ifdef DO_DEEP_PROFILE
198extern void _memcpyW(void* p1, void* p2, long l);
199#define memcpy_nwEVEN(p1, p2, l)    _memcpyW((void*) p1, (void*) p2, (long) l)
200#define memcpy_nwODD(p1, p2, l)     _memcpyW((void*) p1, (void*) p2, (long) l)
201#define memcpyW(p1, p2, l)          _memcpyW((void*) p1, (void*) p2, (long) l)
202
203extern void _memaddW(void* p1, void* p2, void* p3, long l);
204#define memaddW(p1, p2, p3, l)          _memaddW(p1, p2, p3, l)
205#define memadd_nwODD(p1, p2, p3, l)     _memaddW(p1, p2, p3, l)
206#define memadd_nwEVEN(p1, p2, p3, l)    _memaddW(p1, p2, p3, l)
207#define memadd_nwONE(p1, p2, p3)        _memaddW(p1, p2, p3, 1)
208#define memadd_nwTWO(p1, p2, p3)        _memaddW(p1, p2, p3, 2)
209
210extern void _memsetW(void* p1, long w, long l);
211#define memsetW(p1, w, l) _memsetW(p1, w, l)
212
213#else /* ! DO_DEEP_PROFILE */
214
215#define memcpyW(p1, p2, l)                      \
216do                                              \
217{                                               \
218  long _i = l;                                  \
219  long* _s1 = (long*) p1;                       \
220  const long* _s2 = (long*) p2;                 \
221                                                \
222  for (;;)                                      \
223  {                                             \
224    *_s1 = *_s2;                                \
225    _i--;                                       \
226    if (_i == 0) break;                         \
227    _s1++;                                      \
228    _s2++;                                      \
229  }                                             \
230}                                               \
231while(0)
232
233#define memcpy_nwODD(p1, p2, l)                 \
234do                                              \
235{                                               \
236  long _i = l - 1;                              \
237  long* _s1 = (long*) p1;                       \
238  const long* _s2 = (long*) p2;                 \
239                                                \
240  *_s1++ = *_s2++;                              \
241  for (;;)                                      \
242  {                                             \
243    *_s1++ = *_s2++;                            \
244    *_s1++ = *_s2++;                            \
245    _i -= 2;                                    \
246    if (_i == 0) break;                         \
247  }                                             \
248}                                               \
249while(0)
250
251#define memcpy_nwEVEN(p1, p2, l)                \
252do                                              \
253{                                               \
254  long _i = l;                                  \
255  long* _s1 = (long*) p1;                       \
256  const long* _s2 = (long*) p2;                 \
257                                                \
258  for (;;)                                      \
259  {                                             \
260    *_s1++ = *_s2++;                            \
261    *_s1++ = *_s2++;                            \
262    _i -= 2;                                    \
263    if (_i == 0) break;                         \
264  }                                             \
265}                                               \
266while(0)
267
268#define memaddW(P1, P2, P3, L)                  \
269do                                              \
270{                                               \
271  unsigned long* _p1 = P1;                      \
272  const unsigned long* _p2 = P2;                \
273  const unsigned long* _p3 = P3;                \
274  unsigned long l = L;                          \
275                                                \
276  do                                            \
277  {                                             \
278    *_p1++ = *_p2++ + *_p3++;                   \
279    l--;                                        \
280  }                                             \
281  while(l);                                     \
282}                                               \
283while(0)
284
285#define memadd_nwODD(P1, P2, P3, L)             \
286do                                              \
287{                                               \
288  unsigned long* _p1 = P1;                      \
289  const unsigned long* _p2 = P2;                \
290  const unsigned long* _p3 = P3;                \
291  unsigned long l = L;                          \
292                                                \
293 *_p1++ = *_p2++ + *_p3++;                      \
294  l--;                                          \
295                                                \
296  do                                            \
297  {                                             \
298     *_p1++ = *_p2++ + *_p3++;                  \
299     *_p1++ = *_p2++ + *_p3++;                  \
300     l -=2;                                     \
301  }                                             \
302  while(l);                                     \
303}                                               \
304while(0)
305
306#define memadd_nwEVEN(P1, P2, P3, L)            \
307do                                              \
308{                                               \
309  unsigned long* _p1 = P1;                      \
310  const unsigned long* _p2 = P2;                \
311  const unsigned long* _p3 = P3;                \
312  unsigned long l = L;                          \
313                                                \
314  do                                            \
315  {                                             \
316     *_p1++ = *_p2++ + *_p3++;                  \
317     *_p1++ = *_p2++ + *_p3++;                  \
318     l -=2;                                     \
319  }                                             \
320  while(l);                                     \
321}                                               \
322while(0)
323
324#define memadd_nwONE(P1, P2, P3)                \
325do                                              \
326{                                               \
327  unsigned long* _p1 = P1;                      \
328  const unsigned long* _p2 = P2;                \
329  const unsigned long* _p3 = P3;                \
330                                                \
331 *_p1 = *_p2 + *_p3;                            \
332}                                               \
333while(0)
334
335#define memadd_nwTWO(P1, P2, P3)                \
336do                                              \
337{                                               \
338  unsigned long* _p1 = P1;                      \
339  const unsigned long* _p2 = P2;                \
340  const unsigned long* _p3 = P3;                \
341                                                \
342 *_p1++ = *_p2++ + *_p3++;                      \
343 *_p1 = *_p2 + *_p3;                            \
344}                                               \
345while(0)
346
347#define memsetW(P1, W, L)                       \
348do                                              \
349{                                               \
350  long* _p1 = P1;                               \
351  unsigned long _l = L;                         \
352  long _w = W;                                  \
353                                                \
354  while(_l)                                     \
355  {                                             \
356    *_p1++ = W;                                 \
357    _l--;                                       \
358  }                                             \
359}                                               \
360while(0)
361
362#endif /* DO_DEEP_PROFILE */
363
364#ifdef __cplusplus
365}
366#endif
367
368
369#endif
Note: See TracBrowser for help on using the repository browser.