source: git/omalloc/xalloc.h @ 0941e6

spielwiese
Last change on this file since 0941e6 was 0941e6, checked in by Hans Schoenemann <hannes@…>, 3 years ago
fix: omRealloc0Size for xalloc
  • Property mode set to 100644
File size: 9.9 KB
Line 
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
23extern "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
36typedef size_t            omBin;
37
38struct omInfo_s;
39typedef struct omInfo_s omInfo_t;
40struct 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
65extern struct omInfo_s om_Info;
66
67struct omOpts_s;
68extern struct omOpts_s
69{
70  int MinTrack;
71  int MinCheck;
72  int MaxTrack;
73  int MaxCheck;
74  int Keep;
75  int HowToReportErrors;
76  int MarkAsStatic;
77  unsigned int PagesPerRegion;
78  void (*OutOfMemoryFunc)();
79  void (*MemoryLowFunc)();
80  void (*ErrorHook)();
81} om_Opts;
82
83typedef struct omOpts_s omOpts_t;
84
85extern int om_sing_opt_show_mem;
86
87static 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}
96static 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
102static inline void * omAlloc0(size_t s)
103{ void *d=omAlloc(s);memset(d,0,s); return d; }
104static inline void * omalloc0(size_t s)
105{ if (s!=0) { void *d=omAlloc(s);memset(d,0,s); return d;} else return NULL; }
106
107static 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)
120static 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}
132static 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
141static 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
148static 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}
191static 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
198static inline char * omStrDup(const char *s)
199{ size_t l=strlen(s);char *ns=(char *)omAlloc(l+1);
200  return strcpy(ns,s);
201}
202static 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 *******************************************************************/
232enum omError_e
233{
234  omError_NoError = 0,
235  omError_Unknown,
236  omError_InternalBug,
237  omError_MemoryCorrupted,
238  omError_NullAddr,
239  omError_InvalidRangeAddr,
240  omError_FalseAddr,
241  omError_FalseAddrOrMemoryCorrupted,
242  omError_WrongSize,
243  omError_FreedAddr,
244  omError_FreedAddrOrMemoryCorrupted,
245  omError_WrongBin,
246  omError_UnknownBin,
247  omError_NotBinAddr,
248  omError_UnalignedAddr,
249  omError_NullSizeAlloc,
250  omError_ListCycleError,
251  omError_SortedListError,
252  omError_KeptAddrListCorrupted,
253  omError_FreePattern,
254  omError_BackPattern,
255  omError_FrontPattern,
256  omError_NotString,
257  omError_StickyBin,
258  omError_MaxError
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
Note: See TracBrowser for help on using the repository browser.