source: git/omalloc/xalloc.h @ 695288e

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