source: git/xalloc/omalloc.h @ e13a798

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