source: git/xalloc/omalloc.h @ fa3704

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