My Project
xalloc.h
Go to the documentation of this file.
1 #ifndef XMEMORY_H
2 #define XMEMORY_H
3 /****************************************
4 * Computer Algebra System SINGULAR *
5 ****************************************/
6 /*
7 * ABSTRACT: omalloc simulation
8 */
9 /* debug routines of omalloc are not implemented, but as dummies provided: */
10 #define OM_NDEBUG 1
11 
12 #include <stdlib.h>
13 #include <string.h>
14 #include "omalloc/omConfig.h"
15 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
16  #ifdef HAVE_MALLOC_H
17  #include <malloc.h>
18  #elif defined(HAVE_MALLOC_MALLOC_H)
19  #include <malloc/malloc.h>
20  #endif
21 #endif
22 #ifdef __cplusplus
23 extern "C" {
24  #if __cplusplus >= 201402L
25  /* clang 3.7, gcc 5.1 sets 201402L */
26  #define REGISTER
27  #elif defined(__clang__)
28  #define REGISTER
29  #else
30  #define REGISTER register
31  #endif
32 #else
33  #define REGISTER register
34 #endif
35 
36 typedef size_t omBin;
37 
38 struct omInfo_s;
39 typedef struct omInfo_s omInfo_t;
40 struct omInfo_s
41 {
42  long MaxBytesSystem; /* set in omUpdateInfo(), is more accurate with malloc support */
43  long CurrentBytesSystem; /* set in omUpdateInfo(), is more accurate with malloc support */
44  long MaxBytesSbrk; /* always up-to-date, not very accurate, needs omInintInfo() */
45  long CurrentBytesSbrk; /* set in omUpdateInfo(), needs omInintInfo() */
46  long MaxBytesMmap; /* set in omUpdateInfo(), not very accurate */
47  long CurrentBytesMmap; /* set in omUpdateInfo(), not very accurate */
48  long UsedBytes; /* set in omUpdateInfo() */
49  long AvailBytes; /* set in omUpdateInfo() */
50  long UsedBytesMalloc; /* set in omUpdateInfo(), needs malloc support */
51  long AvailBytesMalloc; /* set in omUpdateInfo(), needs malloc support */
52  long MaxBytesFromMalloc; /* always kept up-to-date */
53  long CurrentBytesFromMalloc; /* always kept up-to-date */
54  long MaxBytesFromValloc; /* always kept up-to-date */
55  long CurrentBytesFromValloc; /* always kept up-to-date */
56  long UsedBytesFromValloc; /* set in omUpdateInfo() */
57  long AvailBytesFromValloc;/* set in omUpdateInfo() */
58  long MaxPages; /* always kept up-to-date */
59  long UsedPages; /* always kept up-to-date */
60  long AvailPages; /* always kept up-to-date */
61  long MaxRegionsAlloc; /* always kept up-to-date */
62  long CurrentRegionsAlloc; /* always kept up-to-date */
63 };
64 
65 extern struct omInfo_s om_Info;
66 
67 struct omOpts_s;
68 extern struct omOpts_s
69 {
70  int MinTrack;
71  int MinCheck;
72  int MaxTrack;
73  int MaxCheck;
74  int Keep;
76  int MarkAsStatic;
77  unsigned int PagesPerRegion;
78  void (*OutOfMemoryFunc)();
79  void (*MemoryLowFunc)();
80  void (*ErrorHook)();
81 } om_Opts;
82 
83 typedef struct omOpts_s omOpts_t;
84 
85 extern int om_sing_opt_show_mem;
86 
87 static inline void * omalloc(size_t s)
88 { if (s!=0)
89 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
90  { return malloc(s); }
91 #else
92  {long *d=(long*)malloc(s+sizeof(long)); *d=s;d++;return d; }
93 #endif
94  else return NULL;
95 }
96 static inline void * omAlloc(size_t s)
97 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
98 { return malloc(s); }
99 #else
100 { long *d=(long*)malloc(s+sizeof(long)); *d=s;d++;return d; }
101 #endif
102 static inline void * omAlloc0(size_t s)
103 { void *d=omAlloc(s);memset(d,0,s); return d; }
104 static inline void * omalloc0(size_t s)
105 { if (s!=0) { void *d=omAlloc(s);memset(d,0,s); return d;} else return NULL; }
106 
107 static inline void *omRealloc(void *d, size_t ns)
108 { if (d==NULL) return omAlloc(ns);
109  else
110 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
111  return realloc(d,ns);
112 #else
113  {
114  long *dd=(long*)d; dd--; dd=(long*)realloc(dd,ns+sizeof(long));
115  *dd=ns+sizeof(long);dd++; return dd;
116  }
117 #endif
118 }
119 #define omReallocAligned(A,B) omRealloc(A,B)
120 static inline void *omReallocSize(void *d, __attribute__((unused)) size_t os, size_t ns)
121 { if (d==NULL) return omAlloc(ns);
122  else
123 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
124  return realloc(d,ns);
125 #else
126  {
127  long *dd=(long*)d; dd--; dd=(long*)realloc(dd,ns+sizeof(long));
128  *dd=ns+sizeof(long);dd++; return dd;
129  }
130 #endif
131 }
132 static inline long omSizeOfAddr(void *d)
133 #ifdef HAVE_MALLOC_USABLE_SIZE
134 { return malloc_usable_size(d); }
135 #elif defined(HAVE_AMLLOC_SIZE)
136 { return malloc_size(d); }
137 #else
138 { long *dd=(long*)d; dd--; return *dd;}
139 #endif
140 
141 static inline void omFree(void *d)
142 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
143 { free(d); }
144 #else
145 { if (d!=NULL) { long *dd=(long*)d; dd--; free(dd);}}
146 #endif
147 
148 static inline void *omRealloc0(void *d, size_t ns)
149 {
150 #ifdef HAVE_MALLOC_USABLE_SIZE
151  size_t os=0;
152  if (d!=NULL) os=malloc_usable_size(d);
153  if (os>=ns)
154  {
155  void *n=realloc(d,ns);
156  return n;
157  }
158  else
159  {
160  char *n=(char*)realloc(d,ns);
161  memset(n+(ns-os),0,ns-os);
162  return (void*)n;
163  }
164 #elif defined(HAVE_MALLOC_SIZE)
165  size_t os=0;
166  if (d!=NULL) os=malloc_size(d);
167  if (os>=ns)
168  {
169  void *n=realloc(d,ns);
170  return n;
171  }
172  else
173  {
174  char *n=(char*)realloc(d,ns);
175  memset(n+(ns-os),0,ns-os);
176  return (void*)n;
177  }
178 #else
179  void *n=omAlloc0(ns);
180  if (d!=NULL)
181  {
182  size_t c;
183  size_t os=omSizeOfAddr(d);
184  if (ns>os) c=os; else c=ns;
185  memcpy(n,d,c);
186  omFree(d);
187  }
188  return n;
189 #endif
190 }
191 static inline void omFreeSize(void *d, __attribute__((unused)) size_t s)
192 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
193 { free(d); }
194 #else
195 { if (d!=NULL) { long *dd=(long*)d; dd--; free(dd);}}
196 #endif
197 
198 static inline char * omStrDup(const char *s)
199 { size_t l=strlen(s);char *ns=(char *)omAlloc(l+1);
200  return strcpy(ns,s);
201 }
202 static inline void * omMemDup(void * s)
203 #ifdef HAVE_MALLOC_USABLE_SIZE
204 { size_t l=malloc_usable_size(s);
205  void *n=malloc(l);
206  memcpy(n,s,l);
207  return n;
208 }
209 #elif defined(HAVE_MALLOC_SIZE)
210 { size_t l=malloc_size(s);
211  void *n=malloc(l);
212  memcpy(n,s,l);
213  return n;
214 }
215 #else
216 { long *n;long *d=(long*)s; d--;
217  n=(long*)malloc(*d+sizeof(long));
218  memcpy(n,d,(*d)+sizeof(long));
219  n++;
220  return n;
221 }
222 #endif
223 
224 /* #define omSizeWOfBin(bin_ptr) ((bin_ptr)->sizeW) */
225 #define omSizeWOfBin(bin_ptr) (((bin_ptr)+sizeof(long)-1)/sizeof(long))
226 
227 /*******************************************************************
228  *
229  * error codes
230  *
231  *******************************************************************/
233 {
259 };
260 // typedef enum omError_e omError_t;
261 
262 #define omSizeWOfAddr(P) (omSizeOfAddr(P)/sizeof(long))
263 
264 #define omTypeAllocBin(T,P,B) P=(T)omAlloc(B)
265 #define omTypeAlloc(T,P,S) P=(T)omAlloc(S)
266 #define omTypeAlloc0Bin(T,P,B) P=(T)omAlloc0(B)
267 #define omAlloc0Aligned(S) omAlloc0(S)
268 #define omAllocAligned(S) omAlloc(S)
269 #define omAllocBin(B) omAlloc(B)
270 #define omAllocBin0(B) omAlloc0(B)
271 #define omAlloc0Bin(B) omAlloc0(B)
272 #define omInitInfo()
273 #define omInitGetBackTrace()
274 #define omUpdateInfo()
275 #define omPrintStats(F)
276 #define omPrintInfo(F)
277 #define omPrintBinStats(F)
278 #define omMarkMemoryAsStatic()
279 #define omfree(P) omFree(P)
280 #define omFreeBin(P,B) omFree(P)
281 #define omfreeSize(P,S) omFreeSize(P,S)
282 #define omFreeFunc omFree
283 #define omFreeBinAddr(P) omFree(P)
284 #define omrealloc(A,NS) omRealloc(A,NS)
285 #define omreallocSize(A,OS,NS) omRealloc(A,NS)
286 #define omRealloc0Size(A,OS,NS) omRealloc0(A,NS)
287 #define omrealloc0Size(A,OS,NS) omRealloc0(A,NS)
288 #define omMarkAsStaticAddr(A)
289 #define omMemCpyW(A,B,S) memcpy(A,B,(S)<<2)
290 #define omMemcpyW(A,B,S) memcpy(A,B,(S)<<2)
291 #define omGetSpecBin(A) (A)
292 #define omUnGetSpecBin(A) do {} while (0)
293 #define memcpyW(A,B,C) memcpy(A,B,(C)*sizeof(long))
294 #define omGetStickyBinOfBin(B) omGetSpecBin(B)
295 
296 
297 /* debug dummies: */
298 #define omTypeReallocAlignedSize omTypeReallocSize
299 #define omTypeRealloc0AlignedSize omTypeRealloc0Size
300 #define omReallocAlignedSize omReallocSize
301 #define omRealloc0AlignedSize omRealloc0Size
302 #define omMemDupAligned omMemDup
303 #define omCheckIf(cond, test) do {} while (0)
304 #define omCheckBinAddr(addr) do {} while (0)
305 #define omCheckAddrBin(addr,bin) do {} while (0)
306 #define omCheckBinAddrSize(addr,size) do {} while (0)
307 #define omCheckAddrSize(addr,size) do {} while (0)
308 #define omCheckAddr(addr) do {} while (0)
309 #define omcheckAddrSize(addr,size) do {} while (0)
310 #define omcheckAddr(addr) do {} while (0)
311 #define omCheckBin(bin) do {} while (0)
312 #define omCheckMemory() do {} while (0)
313 #define omPrintCurrentBackTraceMax(A,B) do {} while (0)
314 #define omPrintUsedTrackAddrs(F,max) do {} while (0)
315 #define omPrintCurrentBackTrace(F) do {} while (0)
316 #define omPrintUsedAddrs(F,max) do {} while (0)
317 #define omdebugAddrSize(A,B) do {} while (0)
318 #define omPrintAddrInfo(A,B,C) do {} while (0)
319 #define omIsBinPageAddr(A) (1)
320 #define omTestBinAddrSize(A,B,C) (omError_NoError)
321 #define omTestList(ptr, level) (omError_NoError)
322 #define omInitRet_2_Info(argv0) do {} while (0)
323 #define omMergeStickyBinIntoBin(A,B) do {} while (0)
324 
325 
326 #ifdef __cplusplus
327 }
328 #endif
329 
330 #undef OMALLOC_USES_MALLOC
331 #define X_OMALLOC
332 #define omMallocFunc omAlloc
333 #define omReallocSizeFunc omReallocSize
334 #define omFreeSizeFunc omFreeSize
335 /* #define OM_NDEBUG */
336 #undef OM_SING_KEEP
337 
338 #endif
int l
Definition: cfEzgcd.cc:100
const CanonicalForm int s
Definition: facAbsFact.cc:51
#define __attribute__(x)
Definition: mod2.h:427
#define realloc
Definition: omAllocFunc.c:16
#define free
Definition: omAllocFunc.c:14
omError_e
Definition: omError.h:17
#define NULL
Definition: omList.c:12
void * malloc(size_t size)
Definition: omalloc.c:92
int MinCheck
Definition: omOpts.h:14
int Keep
Definition: omOpts.h:17
void(* MemoryLowFunc)()
Definition: omOpts.h:22
int MinTrack
Definition: omOpts.h:13
int HowToReportErrors
Definition: omOpts.h:18
void(* ErrorHook)()
Definition: omOpts.h:23
int MarkAsStatic
Definition: omOpts.h:19
void(* OutOfMemoryFunc)()
Definition: omOpts.h:21
int MaxTrack
Definition: omOpts.h:15
unsigned int PagesPerRegion
Definition: omOpts.h:20
int MaxCheck
Definition: omOpts.h:16
static void * omRealloc(void *d, size_t ns)
Definition: xalloc.h:107
long UsedBytesMalloc
Definition: omStats.h:20
size_t omBin
Definition: xalloc.h:36
long CurrentBytesFromValloc
Definition: omStats.h:26
static void omFree(void *d)
Definition: xalloc.h:141
long CurrentBytesFromMalloc
Definition: omStats.h:24
static void * omRealloc0(void *d, size_t ns)
Definition: xalloc.h:148
static void * omMemDup(void *s)
Definition: xalloc.h:202
long AvailBytesMalloc
Definition: omStats.h:22
long CurrentBytesMmap
Definition: omStats.h:17
long AvailPages
Definition: omStats.h:31
long CurrentRegionsAlloc
Definition: omStats.h:33
static void * omAlloc0(size_t s)
Definition: xalloc.h:102
long UsedBytesFromValloc
Definition: omStats.h:27
long MaxBytesFromValloc
Definition: omStats.h:25
struct omInfo_s om_Info
Definition: omStats.c:16
long MaxPages
Definition: omStats.h:29
static void * omReallocSize(void *d, __attribute__((unused)) size_t os, size_t ns)
Definition: xalloc.h:120
static void * omAlloc(size_t s)
Definition: xalloc.h:96
static void * omalloc0(size_t s)
Definition: xalloc.h:104
static void omFreeSize(void *d, __attribute__((unused)) size_t s)
Definition: xalloc.h:191
static long omSizeOfAddr(void *d)
Definition: xalloc.h:132
long CurrentBytesSystem
Definition: omStats.h:13
@ omError_WrongSize
Definition: xalloc.h:242
@ omError_ListCycleError
Definition: xalloc.h:250
@ omError_BackPattern
Definition: xalloc.h:254
@ omError_NullAddr
Definition: xalloc.h:238
@ omError_NullSizeAlloc
Definition: xalloc.h:249
@ omError_MaxError
Definition: xalloc.h:258
@ omError_FrontPattern
Definition: xalloc.h:255
@ omError_UnalignedAddr
Definition: xalloc.h:248
@ omError_MemoryCorrupted
Definition: xalloc.h:237
@ omError_FreedAddr
Definition: xalloc.h:243
@ omError_UnknownBin
Definition: xalloc.h:246
@ omError_FreedAddrOrMemoryCorrupted
Definition: xalloc.h:244
@ omError_NotString
Definition: xalloc.h:256
@ omError_SortedListError
Definition: xalloc.h:251
@ omError_KeptAddrListCorrupted
Definition: xalloc.h:252
@ omError_InternalBug
Definition: xalloc.h:236
@ omError_NotBinAddr
Definition: xalloc.h:247
@ omError_InvalidRangeAddr
Definition: xalloc.h:239
@ omError_FalseAddr
Definition: xalloc.h:240
@ omError_WrongBin
Definition: xalloc.h:245
@ omError_NoError
Definition: xalloc.h:234
@ omError_Unknown
Definition: xalloc.h:235
@ omError_StickyBin
Definition: xalloc.h:257
@ omError_FreePattern
Definition: xalloc.h:253
@ omError_FalseAddrOrMemoryCorrupted
Definition: xalloc.h:241
static void * omalloc(size_t s)
Definition: xalloc.h:87
long AvailBytesFromValloc
Definition: omStats.h:28
long MaxBytesSbrk
Definition: omStats.h:14
static char * omStrDup(const char *s)
Definition: xalloc.h:198
long MaxRegionsAlloc
Definition: omStats.h:32
long UsedPages
Definition: omStats.h:30
long UsedBytes
Definition: omStats.h:18
int om_sing_opt_show_mem
long MaxBytesFromMalloc
Definition: omStats.h:23
long CurrentBytesSbrk
Definition: omStats.h:15
long MaxBytesMmap
Definition: omStats.h:16
long MaxBytesSystem
Definition: omStats.h:12
struct omOpts_s om_Opts
Definition: omOpts.c:13
long AvailBytes
Definition: omStats.h:19