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 omFree(void *d)
149 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
150 { free(d); }
151 #else
152 { long *dd=(long*)d; dd--; free(dd);}
153 #endif
154 
155 static inline void *omRealloc0(void *d, size_t ns)
156 {
157 #ifdef HAVE_MALLOC_USABLE_SIZE
158  size_t os=0;
159  if (d!=NULL) os=malloc_usable_size(d);
160  if (os>=ns)
161  {
162  void *n=realloc(d,ns);
163  return n;
164  }
165  else
166  {
167  char *n=(char*)realloc(d,ns);
168  memset(n+(ns-os),0,ns-os);
169  return (void*)n;
170  }
171 #elif defined(HAVE_MALLOC_SIZE)
172  size_t os=0;
173  if (d!=NULL) os=malloc_size(d);
174  if (os>=ns)
175  {
176  void *n=realloc(d,ns);
177  return n;
178  }
179  else
180  {
181  char *n=(char*)realloc(d,ns);
182  memset(n+(ns-os),0,ns-os);
183  return (void*)n;
184  }
185 #else
186  void *n=omAlloc0(ns);
187  if (d!=NULL)
188  {
189  size_t c;
190  size_t os=omSizeOfAddr(d);
191  if (ns>os) c=os; else c=ns;
192  memcpy(n,d,c);
193  omFree(d);
194  }
195  return n;
196 #endif
197 }
198 static inline void omFreeSize(void *d, __attribute__((unused)) size_t s)
199 #if defined(HAVE_MALLOC_USABLE_SIZE) || defined(HAVE_MALLOC_SIZE)
200 { free(d); }
201 #else
202 { if (d!=NULL) { long *dd=(long*)d; dd--; free(dd);}}
203 #endif
204 
205 static inline char * omStrDup(const char *s)
206 { size_t l=strlen(s);char *ns=(char *)omAlloc(l+1);
207  return strcpy(ns,s);
208 }
209 static inline void * omMemDup(void * s)
210 #ifdef HAVE_MALLOC_USABLE_SIZE
211 { size_t l=malloc_usable_size(s);
212  void *n=malloc(l);
213  memcpy(n,s,l);
214  return n;
215 }
216 #elif defined(HAVE_MALLOC_SIZE)
217 { size_t l=malloc_size(s);
218  void *n=malloc(l);
219  memcpy(n,s,l);
220  return n;
221 }
222 #else
223 { long *n;long *d=(long*)s; d--;
224  n=(long*)malloc(*d+sizeof(long));
225  memcpy(n,d,(*d)+sizeof(long));
226  n++;
227  return n;
228 }
229 #endif
230 
231 /* #define omSizeWOfBin(bin_ptr) ((bin_ptr)->sizeW) */
232 #define omSizeWOfBin(bin_ptr) (((bin_ptr)+sizeof(long)-1)/sizeof(long))
233 
234 /*******************************************************************
235  *
236  * error codes
237  *
238  *******************************************************************/
240 {
266 };
267 // typedef enum omError_e omError_t;
268 
269 #define omSizeWOfAddr(P) (omSizeOfAddr(P)/sizeof(long))
270 
271 #define omTypeAllocBin(T,P,B) P=(T)omAlloc(B)
272 #define omTypeAlloc(T,P,S) P=(T)omAlloc(S)
273 #define omTypeAlloc0Bin(T,P,B) P=(T)omAlloc0(B)
274 #define omAlloc0Aligned(S) omAlloc0(S)
275 #define omAllocAligned(S) omAlloc(S)
276 #define omAllocBin(B) omAlloc(B)
277 #define omAllocBin0(B) omAlloc0(B)
278 #define omAlloc0Bin(B) omAlloc0(B)
279 #define omInitInfo()
280 #define omInitGetBackTrace()
281 #define omUpdateInfo()
282 #define omPrintStats(F)
283 #define omPrintInfo(F)
284 #define omPrintBinStats(F)
285 #define omMarkMemoryAsStatic()
286 #define omFreeBin(P,B) omFree(P)
287 #define omfreeSize(P,S) omFreeSize(P,S)
288 #define omFreeFunc omFree
289 #define omFreeBinAddr(P) omFree(P)
290 #define omrealloc(A,NS) omRealloc(A,NS)
291 #define omreallocSize(A,OS,NS) omRealloc(A,NS)
292 #define omRealloc0Size(A,OS,NS) omRealloc0(A,NS)
293 #define omrealloc0Size(A,OS,NS) omRealloc0(A,NS)
294 #define omMarkAsStaticAddr(A)
295 #define omMemCpyW(A,B,S) memcpy(A,B,(S)<<2)
296 #define omMemcpyW(A,B,S) memcpy(A,B,(S)<<2)
297 #define omGetSpecBin(A) (A)
298 #define omUnGetSpecBin(A) do {} while (0)
299 #define memcpyW(A,B,C) memcpy(A,B,(C)*sizeof(long))
300 #define omGetStickyBinOfBin(B) omGetSpecBin(B)
301 
302 
303 /* debug dummies: */
304 #define omTypeReallocAlignedSize omTypeReallocSize
305 #define omTypeRealloc0AlignedSize omTypeRealloc0Size
306 #define omReallocAlignedSize omReallocSize
307 #define omRealloc0AlignedSize omRealloc0Size
308 #define omMemDupAligned omMemDup
309 #define omCheckIf(cond, test) do {} while (0)
310 #define omCheckBinAddr(addr) do {} while (0)
311 #define omCheckAddrBin(addr,bin) do {} while (0)
312 #define omCheckBinAddrSize(addr,size) do {} while (0)
313 #define omCheckAddrSize(addr,size) do {} while (0)
314 #define omCheckAddr(addr) do {} while (0)
315 #define omcheckAddrSize(addr,size) do {} while (0)
316 #define omcheckAddr(addr) do {} while (0)
317 #define omCheckBin(bin) do {} while (0)
318 #define omCheckMemory() do {} while (0)
319 #define omPrintCurrentBackTraceMax(A,B) do {} while (0)
320 #define omPrintUsedTrackAddrs(F,max) do {} while (0)
321 #define omPrintCurrentBackTrace(F) do {} while (0)
322 #define omPrintUsedAddrs(F,max) do {} while (0)
323 #define omdebugAddrSize(A,B) do {} while (0)
324 #define omPrintAddrInfo(A,B,C) do {} while (0)
325 #define omIsBinPageAddr(A) (1)
326 #define omTestBinAddrSize(A,B,C) (omError_NoError)
327 #define omTestList(ptr, level) (omError_NoError)
328 #define omInitRet_2_Info(argv0) do {} while (0)
329 #define omMergeStickyBinIntoBin(A,B) do {} while (0)
330 
331 
332 #ifdef __cplusplus
333 }
334 #endif
335 
336 #undef OMALLOC_USES_MALLOC
337 #define X_OMALLOC
338 #define omMallocFunc omAlloc
339 #define omReallocSizeFunc omReallocSize
340 #define omFreeSizeFunc omFreeSize
341 /* #define OM_NDEBUG */
342 #undef OM_SING_KEEP
343 
344 #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:85
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
static void omFree(void *d)
Definition: xalloc.h:148
long CurrentBytesFromMalloc
Definition: omStats.h:24
static void * omRealloc0(void *d, size_t ns)
Definition: xalloc.h:155
static void * omMemDup(void *s)
Definition: xalloc.h:209
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:198
static long omSizeOfAddr(void *d)
Definition: xalloc.h:132
long CurrentBytesSystem
Definition: omStats.h:13
@ omError_WrongSize
Definition: xalloc.h:249
@ omError_ListCycleError
Definition: xalloc.h:257
@ omError_BackPattern
Definition: xalloc.h:261
@ omError_NullAddr
Definition: xalloc.h:245
@ omError_NullSizeAlloc
Definition: xalloc.h:256
@ omError_MaxError
Definition: xalloc.h:265
@ omError_FrontPattern
Definition: xalloc.h:262
@ omError_UnalignedAddr
Definition: xalloc.h:255
@ omError_MemoryCorrupted
Definition: xalloc.h:244
@ omError_FreedAddr
Definition: xalloc.h:250
@ omError_UnknownBin
Definition: xalloc.h:253
@ omError_FreedAddrOrMemoryCorrupted
Definition: xalloc.h:251
@ omError_NotString
Definition: xalloc.h:263
@ omError_SortedListError
Definition: xalloc.h:258
@ omError_KeptAddrListCorrupted
Definition: xalloc.h:259
@ omError_InternalBug
Definition: xalloc.h:243
@ omError_NotBinAddr
Definition: xalloc.h:254
@ omError_InvalidRangeAddr
Definition: xalloc.h:246
@ omError_FalseAddr
Definition: xalloc.h:247
@ omError_WrongBin
Definition: xalloc.h:252
@ omError_NoError
Definition: xalloc.h:241
@ omError_Unknown
Definition: xalloc.h:242
@ omError_StickyBin
Definition: xalloc.h:264
@ omError_FreePattern
Definition: xalloc.h:260
@ omError_FalseAddrOrMemoryCorrupted
Definition: xalloc.h:248
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:205
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