My Project
Loading...
Searching...
No Matches
xalloc.h
Go to the documentation of this file.
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/* use of Bins: define for optimal performancei(6%), undef for valgrind */
12#define XALLOC_BIN 1
13
14/* performancce of xalloc+XALLOC_BIN: +32.6 %, xalloc w/o XALLOC_BIN: +40.7 %
15 * (omalloc=100 %) */
16#include <stdlib.h>
17#include <string.h>
18#include "omalloc/omConfig.h"
19#ifdef __cplusplus
20extern "C" {
21 #if __cplusplus >= 201402L
22 /* clang 3.7, gcc 5.1 sets 201402L */
23 #define REGISTER
24 #elif defined(__clang__)
25 #define REGISTER
26 #else
27 #define REGISTER register
28 #endif
29#else
30 #define REGISTER register
31#endif
32
33
34
35struct omInfo_s;
36typedef struct omInfo_s omInfo_t;
37struct omInfo_s
38{
39 long MaxBytesSystem; /* set in omUpdateInfo(), is more accurate with malloc support */
40 long CurrentBytesSystem; /* set in omUpdateInfo(), is more accurate with malloc support */
41 long MaxBytesSbrk; /* always up-to-date, not very accurate, needs omInintInfo() */
42 long CurrentBytesSbrk; /* set in omUpdateInfo(), needs omInintInfo() */
43 long MaxBytesMmap; /* set in omUpdateInfo(), not very accurate */
44 long CurrentBytesMmap; /* set in omUpdateInfo(), not very accurate */
45 long UsedBytes; /* set in omUpdateInfo() */
46 long AvailBytes; /* set in omUpdateInfo() */
47 long UsedBytesMalloc; /* set in omUpdateInfo(), needs malloc support */
48 long AvailBytesMalloc; /* set in omUpdateInfo(), needs malloc support */
49 long MaxBytesFromMalloc; /* always kept up-to-date */
50 long CurrentBytesFromMalloc; /* always kept up-to-date */
51 long MaxBytesFromValloc; /* always kept up-to-date */
52 long CurrentBytesFromValloc; /* always kept up-to-date */
53 long UsedBytesFromValloc; /* set in omUpdateInfo() */
54 long AvailBytesFromValloc;/* set in omUpdateInfo() */
55 long MaxPages; /* always kept up-to-date */
56 long UsedPages; /* always kept up-to-date */
57 long AvailPages; /* always kept up-to-date */
58 long MaxRegionsAlloc; /* always kept up-to-date */
59 long CurrentRegionsAlloc; /* always kept up-to-date */
60};
61
62extern struct omInfo_s om_Info;
63
64struct omOpts_s;
65extern struct omOpts_s
66{
67 int MinTrack;
68 int MinCheck;
69 int MaxTrack;
70 int MaxCheck;
71 int Keep;
73 int MarkAsStatic;
74 unsigned int PagesPerRegion;
75 void (*OutOfMemoryFunc)();
76 void (*MemoryLowFunc)();
77 void (*ErrorHook)();
78} om_Opts;
79
80typedef struct omOpts_s omOpts_t;
81
82extern int om_sing_opt_show_mem;
83
84#define omalloc(s) malloc(s)
85#define omAlloc(s) malloc(s)
86
87static inline void * omAlloc0(size_t s)
88{ void *d=malloc(s);memset(d,0,s); return d; }
89static inline void * omalloc0(size_t s)
90{ if (s!=0) { void *d=malloc(s);memset(d,0,s); return d;} else return NULL; }
91
92static inline void *omRealloc0Size(void *d, __attribute__((unused)) size_t os, size_t ns)
93{ if (d==NULL)
94 return omAlloc0(ns);
95 else
96 {
97 char *p=(char *)realloc(d,ns);
98 if (ns>os) memset(p+os,0,ns-os);
99 return (void*)p;
100 }
101}
102
103#define omfree(d) free(d)
104#define omFree(d) free(d)
105#define omFreeSize(d,s) free(d)
106
107#define omStrDup(s) strdup(s)
108
109#ifdef XALLOC_BIN
110typedef struct omBin_next_s omBin_next_t;
111typedef omBin_next_t* omBin_next;
113{
115};
116
117struct omBin_s
118{
119 omBin_next curr; /* current freelist */
120 size_t size; /* size in bytes */
121};
122typedef struct omBin_s omBin_t;
123typedef omBin_t* omBin;
124#define omSizeWOfBin(bin_ptr) (((bin_ptr->size)+SIZEOF_LONG-1)/SIZEOF_LONG)
125static inline void* omAllocBin(omBin b)
126{
127 if (b->curr!=NULL)
128 {
129 omBin_next p=b->curr;
130 b->curr=p->next;
131 return p;
132 }
133 else return omAlloc(b->size);
134}
135static inline void* omAlloc0Bin(omBin b)
136{
137 if (b->curr!=NULL)
138 {
139 omBin_next p=b->curr;
140 b->curr=p->next;
141 memset(p,0,b->size);
142 return p;
143 }
144 else return omAlloc0(b->size);
145}
146static inline void omFreeBin(void *p, omBin b)
147{
148 *((void**) p) = b->curr;
149 b->curr=(omBin_next)p;
150}
151
152#define omTypeAllocBin(T,P,B) P=(T)omAllocBin(B)
153#define omTypeAlloc0Bin(T,P,B) P=(T)omAlloc0Bin(B)
154static inline omBin omGetSpecBin(size_t s)
155{
156 omBin b=(omBin)omAlloc(sizeof(*b));
157 b->size=s;
158 b->curr=NULL;
159 return b;
160}
161static inline void omUnGetSpecBin(omBin *A)
162{
163 omBin_next p=(*A)->curr;
165 while(p!=NULL)
166 {
167 pp=p->next;
168 omFree(p);
169 p=pp;
170 }
171 (*A)->curr=NULL;
172 omFree(*A);
173}
174
175#else
176
177typedef size_t omBin;
178#define omSizeWOfBin(bin_ptr) (((bin_ptr)+SIZEOF_LONG-1)/SIZEOF_LONG)
179#define omTypeAllocBin(T,P,B) P=(T)omAlloc(B)
180#define omTypeAlloc0Bin(T,P,B) P=(T)omAlloc0(B)
181#define omAllocBin(B) omAlloc(B)
182#define omAlloc0Bin(B) omAlloc0(B)
183#define omFreeBin(P,B) omFree(P)
184#define omGetSpecBin(A) (A)
185#define omUnGetSpecBin(A) do {} while (0)
186#endif
187
188/*******************************************************************
189 *
190 * error codes
191 *
192 *******************************************************************/
194{
221// typedef enum omError_e omError_t;
222
223#define omSizeWOfAddr(P) (omSizeOfAddr(P)/SIZEOF_LONG)
224
225#define omTypeAlloc(T,P,S) P=(T)omAlloc(S)
226#define omAlloc0Aligned(S) omAlloc0(S)
227#define omAllocAligned(S) omAlloc(S)
228#define omInitInfo()
229#define omInitGetBackTrace()
230#define omUpdateInfo()
231#define omPrintStats(F)
232#define omPrintInfo(F)
233#define omPrintBinStats(F)
234#define omMarkMemoryAsStatic()
235#define omfreeSize(P,S) free(P)
236#define omFreeFunc free
237#define omFreeBinAddr(P) free(P)
238#define omrealloc(A,NS) realloc(A,NS)
239#define omreallocSize(A,OS,NS) realloc(A,NS)
240#define omrealloc0Size(A,OS,NS) omRealloc0Size(A,OS,NS)
241#define omRealloc(A,B) realloc(A,B)
242#define omReallocAligned(A,B) realloc(A,B)
243#define omReallocSize(A,B,C) realloc(A,C)
244#define omReallocAlignedSize(A,B) realloc(A,B)
245#define omMarkAsStaticAddr(A)
246#define omMemcpyW(A,B,C) memcpy(A,B,(C)*SIZEOF_LONG)
247#define omGetStickyBinOfBin(B) (B)
248
249
250/* debug dummies: */
251#define omTypeReallocAlignedSize omTypeReallocSize
252#define omTypeRealloc0AlignedSize omTypeRealloc0Size
253#define omRealloc0AlignedSize omRealloc0Size
254#define omMemDupAligned omMemDup
255#define omCheckIf(cond, test) do {} while (0)
256#define omCheckBinAddr(addr) do {} while (0)
257#define omCheckAddrBin(addr,bin) do {} while (0)
258#define omCheckBinAddrSize(addr,size) do {} while (0)
259#define omCheckAddrSize(addr,size) do {} while (0)
260#define omCheckAddr(addr) do {} while (0)
261#define omcheckAddrSize(addr,size) do {} while (0)
262#define omcheckAddr(addr) do {} while (0)
263#define omCheckBin(bin) do {} while (0)
264#define omCheckMemory() do {} while (0)
265#define omPrintCurrentBackTraceMax(A,B) do {} while (0)
266#define omPrintUsedTrackAddrs(F,max) do {} while (0)
267#define omPrintCurrentBackTrace(F) do {} while (0)
268#define omPrintUsedAddrs(F,max) do {} while (0)
269#define omdebugAddrSize(A,B) do {} while (0)
270#define omPrintAddrInfo(A,B,C) do {} while (0)
271#define omIsBinPageAddr(A) (1)
272#define omTestBinAddrSize(A,B,C) (omError_NoError)
273#define omTestList(ptr, level) (omError_NoError)
274#define omInitRet_2_Info(argv0) do {} while (0)
275#define omMergeStickyBinIntoBin(A,B) do {} while (0)
276
277
278#ifdef __cplusplus
279}
280#endif
281
282#undef OMALLOC_USES_MALLOC
283#define X_OMALLOC
284#define omMallocFunc malloc
285#define omFreeSizeFunc omFreeSize
286#define omReallocSizeFunc realloc
287/* #define OM_NDEBUG */
288#undef OM_SING_KEEP
289
290#endif
CanonicalForm FACTORY_PUBLIC pp(const CanonicalForm &)
CanonicalForm pp ( const CanonicalForm & f )
Definition: cf_gcd.cc:676
int p
Definition: cfModGcd.cc:4078
CanonicalForm b
Definition: cfModGcd.cc:4103
const CanonicalForm int s
Definition: facAbsFact.cc:51
#define __attribute__(x)
Definition: mod2.h:429
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omalloc0(size)
Definition: omAllocDecl.h:229
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
#define omRealloc0Size(addr, o_size, size)
Definition: omAllocDecl.h:221
#define realloc
Definition: omAllocFunc.c:16
#define malloc
Definition: omAllocFunc.c:12
#define omGetSpecBin(size)
Definition: omBin.h:11
#define omUnGetSpecBin(bin_ptr)
Definition: omBin.h:14
omError_e
Definition: omError.h:17
#define NULL
Definition: omList.c:12
omBin_t * omBin
Definition: omStructs.h:12
#define A
Definition: sirandom.c:24
int MinCheck
Definition: omOpts.h:14
void(* MemoryLowFunc)(void)
Definition: omOpts.h:22
int Keep
Definition: omOpts.h:17
int MinTrack
Definition: omOpts.h:13
int HowToReportErrors
Definition: omOpts.h:18
int MarkAsStatic
Definition: omOpts.h:19
void(* OutOfMemoryFunc)(void)
Definition: omOpts.h:21
int MaxTrack
Definition: omOpts.h:15
unsigned int PagesPerRegion
Definition: omOpts.h:20
void(* ErrorHook)(void)
Definition: omOpts.h:23
int MaxCheck
Definition: omOpts.h:16
long UsedBytesMalloc
Definition: omStats.h:20
long CurrentBytesFromValloc
Definition: omStats.h:26
size_t size
Definition: xalloc.h:120
#define omFree(d)
Definition: xalloc.h:104
long CurrentBytesFromMalloc
Definition: omStats.h:24
long AvailBytesMalloc
Definition: omStats.h:22
omBin_next next
Definition: xalloc.h:114
long CurrentBytesMmap
Definition: omStats.h:17
long AvailPages
Definition: omStats.h:31
long CurrentRegionsAlloc
Definition: omStats.h:33
omBin_next curr
Definition: xalloc.h:119
long UsedBytesFromValloc
Definition: omStats.h:27
omBin_next_t * omBin_next
Definition: xalloc.h:111
long MaxBytesFromValloc
Definition: omStats.h:25
struct omInfo_s om_Info
Definition: omStats.c:16
long MaxPages
Definition: omStats.h:29
#define omAlloc(s)
Definition: xalloc.h:85
omBin_t * omBin
Definition: xalloc.h:123
long CurrentBytesSystem
Definition: omStats.h:13
@ omError_WrongSize
Definition: xalloc.h:203
@ omError_ListCycleError
Definition: xalloc.h:211
@ omError_BackPattern
Definition: xalloc.h:215
@ omError_NullAddr
Definition: xalloc.h:199
@ omError_NullSizeAlloc
Definition: xalloc.h:210
@ omError_MaxError
Definition: xalloc.h:219
@ omError_FrontPattern
Definition: xalloc.h:216
@ omError_UnalignedAddr
Definition: xalloc.h:209
@ omError_MemoryCorrupted
Definition: xalloc.h:198
@ omError_FreedAddr
Definition: xalloc.h:204
@ omError_UnknownBin
Definition: xalloc.h:207
@ omError_FreedAddrOrMemoryCorrupted
Definition: xalloc.h:205
@ omError_NotString
Definition: xalloc.h:217
@ omError_SortedListError
Definition: xalloc.h:212
@ omError_KeptAddrListCorrupted
Definition: xalloc.h:213
@ omError_InternalBug
Definition: xalloc.h:197
@ omError_NotBinAddr
Definition: xalloc.h:208
@ omError_InvalidRangeAddr
Definition: xalloc.h:200
@ omError_FalseAddr
Definition: xalloc.h:201
@ omError_WrongBin
Definition: xalloc.h:206
@ omError_NoError
Definition: xalloc.h:195
@ omError_Unknown
Definition: xalloc.h:196
@ omError_StickyBin
Definition: xalloc.h:218
@ omError_FreePattern
Definition: xalloc.h:214
@ omError_FalseAddrOrMemoryCorrupted
Definition: xalloc.h:202
long AvailBytesFromValloc
Definition: omStats.h:28
long MaxBytesSbrk
Definition: omStats.h:14
long MaxRegionsAlloc
Definition: omStats.h:32
long UsedPages
Definition: omStats.h:30
long UsedBytes
Definition: omStats.h:18
int om_sing_opt_show_mem
long MaxBytesFromMalloc
Definition: omStats.h:23
long CurrentBytesSbrk
Definition: omStats.h:15
long MaxBytesMmap
Definition: omStats.h:16
long MaxBytesSystem
Definition: omStats.h:12
struct omOpts_s om_Opts
Definition: omOpts.c:13
long AvailBytes
Definition: omStats.h:19