source: git/xalloc/omalloc.h @ 366350

fieker-DuValspielwiese
Last change on this file since 366350 was 366350, checked in by Hans Schoenemann <hannes@…>, 7 years ago
xalloc: test for malloc_usable_size
  • Property mode set to 100644
File size: 8.7 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#ifdef __cplusplus
15extern "C" {
16#endif
17
18typedef size_t            omBin;
19
20struct omInfo_s;
21typedef struct omInfo_s omInfo_t;
22struct omInfo_s
23{
24  long MaxBytesSystem;      /* set in omUpdateInfo(), is more accurate with malloc support   */
25  long CurrentBytesSystem;  /* set in omUpdateInfo(), is more accurate with malloc support */
26  long MaxBytesSbrk;        /* always up-to-date, not very accurate, needs omInintInfo() */
27  long CurrentBytesSbrk;    /* set in omUpdateInfo(), needs omInintInfo() */
28  long MaxBytesMmap;        /* set in omUpdateInfo(), not very accurate */
29  long CurrentBytesMmap;    /* set in omUpdateInfo(), not very accurate */
30  long UsedBytes;           /* set in omUpdateInfo() */
31  long AvailBytes;          /* set in omUpdateInfo() */
32  long UsedBytesMalloc;     /* set in omUpdateInfo(), needs malloc support */
33  long AvailBytesMalloc;    /* set in omUpdateInfo(), needs malloc support */
34  long MaxBytesFromMalloc;      /* always kept up-to-date */
35  long CurrentBytesFromMalloc;  /* always kept up-to-date */
36  long MaxBytesFromValloc;      /* always kept up-to-date */
37  long CurrentBytesFromValloc;  /* always kept up-to-date */
38  long UsedBytesFromValloc; /* set in omUpdateInfo()  */
39  long AvailBytesFromValloc;/* set in omUpdateInfo()  */
40  long MaxPages;            /* always kept up-to-date */
41  long UsedPages;           /* always kept up-to-date */
42  long AvailPages;          /* always kept up-to-date */
43  long MaxRegionsAlloc;     /* always kept up-to-date */
44  long CurrentRegionsAlloc; /* always kept up-to-date */
45};
46
47extern struct omInfo_s om_Info;
48
49struct omOpts_s;
50extern struct omOpts_s
51{
52  int MinTrack;
53  int MinCheck;
54  int MaxTrack;
55  int MaxCheck;
56  int Keep;
57  int HowToReportErrors;
58  int MarkAsStatic;
59  unsigned int PagesPerRegion;
60  void (*OutOfMemoryFunc)();
61  void (*MemoryLowFunc)();
62  void (*ErrorHook)();
63} om_Opts;
64
65typedef struct omOpts_s omOpts_t;
66
67extern int om_sing_opt_show_mem;
68
69static inline void * omalloc(size_t s)
70{ if (s!=0)
71#ifdef HAVE_MALLOC_USABLE_SIZE
72   { return malloc(s); }
73#else
74  {long *d=(long*)malloc(s+sizeof(long)); *d=s;d++;return d; }
75#endif
76  else return NULL;
77}
78static inline void * omAlloc(size_t s)
79#ifdef HAVE_MALLOC_USABLE_SIZE
80{ return malloc(s); }
81#else
82{ long *d=(long*)malloc(s+sizeof(long)); *d=s;d++;return d; }
83#endif
84static inline void * omAlloc0(size_t s)
85{ void *d=omAlloc(s);memset(d,0,s); return d; }
86static inline void * omalloc0(size_t s)
87{ if (s!=0) { void *d=omAlloc(s);memset(d,0,s); return d;} else return NULL; }
88
89static inline void *omRealloc(void *d, size_t ns)
90{ if (d==NULL) return omAlloc(ns);
91  else
92#ifdef HAVE_MALLOC_USABLE_SIZE
93  return realloc(d,ns);
94#else
95  {
96    long *dd=(long*)d; dd--; dd=(long*)realloc(dd,ns+sizeof(long));
97    *dd=ns+sizeof(long);dd++; return dd;
98  }
99#endif
100}
101#define omReallocAligned(A,B) omRealloc(A,B)
102static inline void *omReallocSize(void *d, __attribute__((unused)) size_t os, size_t ns)
103{ if (d==NULL) return omAlloc(ns);
104  else
105#ifdef HAVE_MALLOC_USABLE_SIZE
106  return realloc(d,ns);
107#else
108  {
109    long *dd=(long*)d; dd--; dd=(long*)realloc(dd,ns+sizeof(long));
110    *dd=ns+sizeof(long);dd++; return dd;
111  }
112#endif
113}
114static inline long omSizeOfAddr(void *d)
115#ifdef HAVE_MALLOC_USABLE_SIZE
116{ return malloc_usable_size(d); }
117#else
118{ long *dd=(long*)d; dd--; return *dd;}
119#endif
120
121static inline void omFree(void *d)
122#ifdef HAVE_MALLOC_USABLE_SIZE
123{ free(d); }
124#else
125{ if (d!=NULL) { long *dd=(long*)d; dd--; free(dd);}}
126#endif
127
128static inline void *omRealloc0(void *d, size_t ns)
129{
130#ifdef HAVE_MALLOC_USABLE_SIZE
131  void *n=realloc(d,ns);
132  memset(n,0,ns);
133#else
134  void *n=omAlloc0(ns);
135  if (d!=NULL)
136  {
137    size_t c;
138    size_t os=omSizeOfAddr(d);
139    if (ns>os) c=os; else c=ns;
140    memcpy(n,d,c);
141    omFree(d);
142  }
143#endif
144  return n;
145}
146static inline void omFreeSize(void *d, __attribute__((unused)) size_t s)
147#ifdef HAVE_MALLOC_USABLE_SIZE
148{ free(d); }
149#else
150{ if (d!=NULL) { long *dd=(long*)d; dd--; free(dd);}}
151#endif
152
153static inline char * omStrDup(const char *s)
154{ size_t l=strlen(s);char *ns=(char *)omAlloc(l+1);
155  return strcpy(ns,s);
156}
157static inline void * omMemDup(void * s)
158#ifdef HAVE_MALLOC_USABLE_SIZE
159{ size_t l=malloc_usable_size(s);
160  void *n=malloc(l);
161  memcpy(n,s,l);
162  return n;
163}
164#else
165{ long *n;long *d=(long*)s; d--;
166  n=(long*)malloc(*d+sizeof(long));
167  memcpy(n,d,(*d)+sizeof(long));
168  n++;
169  return n;
170}
171#endif
172
173/* #define omSizeWOfBin(bin_ptr) ((bin_ptr)->sizeW) */
174#define omSizeWOfBin(bin_ptr) (((bin_ptr)+sizeof(long)-1)/sizeof(long))
175
176/*******************************************************************
177 *
178 *  error codes
179 *
180 *******************************************************************/
181enum omError_e
182{
183  omError_NoError = 0,
184  omError_Unknown,
185  omError_InternalBug,
186  omError_MemoryCorrupted,
187  omError_NullAddr,
188  omError_InvalidRangeAddr,
189  omError_FalseAddr,
190  omError_FalseAddrOrMemoryCorrupted,
191  omError_WrongSize,
192  omError_FreedAddr,
193  omError_FreedAddrOrMemoryCorrupted,
194  omError_WrongBin,
195  omError_UnknownBin,
196  omError_NotBinAddr,
197  omError_UnalignedAddr,
198  omError_NullSizeAlloc,
199  omError_ListCycleError,
200  omError_SortedListError,
201  omError_KeptAddrListCorrupted,
202  omError_FreePattern,
203  omError_BackPattern,
204  omError_FrontPattern,
205  omError_NotString,
206  omError_StickyBin,
207  omError_MaxError
208};
209// typedef enum omError_e omError_t;
210
211#define omSizeWOfAddr(P)         (omSizeOfAddr(P)/sizeof(long))
212
213#define omTypeAllocBin(T,P,B)    P=(T)omAlloc(B)
214#define omTypeAlloc(T,P,S)       P=(T)omAlloc(S)
215#define omTypeAlloc0Bin(T,P,B)   P=(T)omAlloc0(B)
216#define omAlloc0Aligned(S)       omAlloc0(S)
217#define omAllocAligned(S)        omAlloc(S)
218#define omAllocBin(B)            omAlloc(B)
219#define omAllocBin0(B)           omAlloc0(B)
220#define omAlloc0Bin(B)           omAlloc0(B)
221#define omInitInfo()
222#define omInitGetBackTrace()
223#define omUpdateInfo()
224#define omPrintStats(F)
225#define omPrintInfo(F)
226#define omPrintBinStats(F)
227#define omMarkMemoryAsStatic()
228#define omfree(P)                omFree(P)
229#define omFreeBin(P,B)           omFree(P)
230#define omfreeSize(P,S)          omFreeSize(P,S)
231#define omFreeFunc               omFree
232#define omFreeBinAddr(P)         omFree(P)
233#define omrealloc(A,NS)          omRealloc(A,NS)
234#define omreallocSize(A,OS,NS)   omRealloc(A,NS)
235#define omRealloc0Size(A,OS,NS)  omRealloc0(A,NS)
236#define omrealloc0Size(A,OS,NS)  omRealloc(A,NS)
237#define omMarkAsStaticAddr(A)
238#define omMemCpyW(A,B,S)         memcpy(A,B,(S)<<2)
239#define omMemcpyW(A,B,S)         memcpy(A,B,(S)<<2)
240#define omGetSpecBin(A)          (A)
241#define omUnGetSpecBin(A)        do {} while (0)
242#define memcpyW(A,B,C)         memcpy(A,B,(C)*sizeof(long))
243#define omGetStickyBinOfBin(B) omGetSpecBin(B)
244
245
246/* debug dummies: */
247#define omTypeReallocAlignedSize     omTypeReallocSize
248#define omTypeRealloc0AlignedSize    omTypeRealloc0Size
249#define omReallocAlignedSize         omReallocSize
250#define omRealloc0AlignedSize        omRealloc0Size
251#define omMemDupAligned              omMemDup
252#define omCheckIf(cond, test)                    do {} while (0)
253#define omCheckBinAddr(addr)                     do {} while (0)
254#define omCheckAddrBin(addr,bin)                 do {} while (0)
255#define omCheckBinAddrSize(addr,size)            do {} while (0)
256#define omCheckAddrSize(addr,size)               do {} while (0)
257#define omCheckAddr(addr)                        do {} while (0)
258#define omcheckAddrSize(addr,size)               do {} while (0)
259#define omcheckAddr(addr)                        do {} while (0)
260#define omCheckBin(bin)                          do {} while (0)
261#define omCheckMemory()                          do {} while (0)
262#define omPrintCurrentBackTraceMax(A,B)          do {} while (0)
263#define omPrintUsedTrackAddrs(F,max)             do {} while (0)
264#define omPrintCurrentBackTrace(F)               do {} while (0)
265#define omPrintUsedAddrs(F,max)                  do {} while (0)
266#define omdebugAddrSize(A,B)                     do {} while (0)
267#define omPrintAddrInfo(A,B,C)                   do {} while (0)
268#define omIsBinPageAddr(A)                       (1)
269#define omTestBinAddrSize(A,B,C)                 (omError_NoError)
270#define omTestList(ptr, level)                   (omError_NoError)
271#define omInitRet_2_Info(argv0)                  do {} while (0)
272#define omMergeStickyBinIntoBin(A,B)             do {} while (0)
273
274
275#ifdef __cplusplus
276}
277#endif
278
279#undef OMALLOC_USES_MALLOC
280#define X_OMALLOC
281#define omMallocFunc omAlloc
282#define omReallocSizeFunc omReallocSize
283#define omFreeSizeFunc omFreeSize
284/* #define OM_NDEBUG */
285#undef OM_SING_KEEP
286
287#endif
Note: See TracBrowser for help on using the repository browser.