My Project
Loading...
Searching...
No Matches
Macros | Functions
omAllocSystem.c File Reference
#include <unistd.h>
#include <limits.h>
#include "omConfig.h"
#include "omDefaultConfig.h"
#include "omMalloc.h"
#include "omalloc.h"
#include <string.h>

Go to the source code of this file.

Macros

#define OM_ALLOC_SYSTEM_C
 
#define OM_MALLOC_FROM_SYSTEM   OM_MALLOC_MALLOC
 
#define OM_REALLOC_FROM_SYSTEM   OM_MALLOC_REALLOC
 
#define OM_FREE_TO_SYSTEM   OM_MALLOC_FREE
 
#define _omSizeOfLargeAddr(addr)   (*((size_t*) ((char*) addr - SIZEOF_STRICT_ALIGNMENT)))
 
#define OM_VALLOC_FROM_SYSTEM   omEmulateValloc
 
#define OM_VFREE_TO_SYSTEM   omEmulateVfree
 
#define OM_ALIGN_PAGE(addr)   ( ((long)addr + (SIZEOF_SYSTEM_PAGE -1)) & ~(SIZEOF_SYSTEM_PAGE - 1))
 

Functions

void * omAllocLarge (size_t size)
 
void * omReallocLarge (void *old_addr, size_t new_size)
 
void omFreeLarge (void *addr)
 
void * omAlloc0Large (size_t size)
 
void * omRealloc0Large (void *old_addr, size_t new_size)
 
size_t omSizeOfLargeAddr (void *addr)
 
size_t omSizeOfAddr (const void *addr)
 
size_t omSizeWOfAddr (void *addr)
 
void * omEmulateValloc (size_t size)
 
void omEmulateVfree (void *addr, size_t size)
 
void * omAllocFromSystem (size_t size)
 
void * omReallocFromSystem (void *addr, size_t newsize)
 
void * omReallocSizeFromSystem (void *addr, size_t oldsize, size_t newsize)
 
void omFreeToSystem (void *addr)
 
void omFreeSizeToSystem (void *addr, size_t size)
 
void * _omVallocFromSystem (size_t size, int fail)
 
void omVfreeToSystem (void *page, size_t size)
 

Macro Definition Documentation

◆ _omSizeOfLargeAddr

#define _omSizeOfLargeAddr (   addr)    (*((size_t*) ((char*) addr - SIZEOF_STRICT_ALIGNMENT)))

Definition at line 79 of file omAllocSystem.c.

◆ OM_ALIGN_PAGE

#define OM_ALIGN_PAGE (   addr)    ( ((long)addr + (SIZEOF_SYSTEM_PAGE -1)) & ~(SIZEOF_SYSTEM_PAGE - 1))

Definition at line 157 of file omAllocSystem.c.

◆ OM_ALLOC_SYSTEM_C

#define OM_ALLOC_SYSTEM_C

Definition at line 8 of file omAllocSystem.c.

◆ OM_FREE_TO_SYSTEM

#define OM_FREE_TO_SYSTEM   OM_MALLOC_FREE

Definition at line 25 of file omAllocSystem.c.

◆ OM_MALLOC_FROM_SYSTEM

#define OM_MALLOC_FROM_SYSTEM   OM_MALLOC_MALLOC

Definition at line 23 of file omAllocSystem.c.

◆ OM_REALLOC_FROM_SYSTEM

#define OM_REALLOC_FROM_SYSTEM   OM_MALLOC_REALLOC

Definition at line 24 of file omAllocSystem.c.

◆ OM_VALLOC_FROM_SYSTEM

#define OM_VALLOC_FROM_SYSTEM   omEmulateValloc

Definition at line 154 of file omAllocSystem.c.

◆ OM_VFREE_TO_SYSTEM

#define OM_VFREE_TO_SYSTEM   omEmulateVfree

Definition at line 155 of file omAllocSystem.c.

Function Documentation

◆ _omVallocFromSystem()

void * _omVallocFromSystem ( size_t  size,
int  fail 
)

Definition at line 318 of file omAllocSystem.c.

319{
320 void* page = OM_VALLOC_FROM_SYSTEM(size);
321 if (page == NULL)
322 {
323 OM_MEMORY_LOW_HOOK();
325 if (page == NULL)
326 {
327 if (fail) return NULL;
328 else
329 {
330 OM_OUT_OF_MEMORY_HOOK();
331 /* should never get here */
332 omAssume(0);
333 exit(1);
334 }
335 }
336 }
337
338#ifndef OM_NDEBUG
339 if (((unsigned long) page) + size > om_MaxAddr)
340 om_MaxAddr = ((unsigned long) page) + size;
341 if (((unsigned long) page) < om_MinAddr)
342 om_MinAddr = ((unsigned long) page);
343#endif
344
346 om_Info.CurrentBytesFromValloc += size;
347 if (om_Info.CurrentBytesFromValloc > om_Info.MaxBytesFromValloc)
348 {
349 om_Info.MaxBytesFromValloc = om_Info.CurrentBytesFromValloc;
350#if defined(OM_HAVE_VALLOC_MMAP) && defined(OM_MALLOC_MAX_BYTES_SYSTEM)
351 if (om_Info.MaxBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM > om_Info.MaxBytesSystem)
352 om_Info.MaxBytesSystem = om_Info.MaxBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM;
353#endif
354#if defined(HAVE_SBRK) && !defined(OM_HAVE_VALLOC_MMAP) && !defined(OM_MALLOC_MAX_BYTES_SBRK)
355 if (! om_SbrkInit) om_SbrkInit = (unsigned long) sbrk(0) - size;
356 if (om_Info.CurrentBytesFromMalloc + om_Info.CurrentBytesFromValloc > om_Info.MaxBytesSbrk)
357 {
358 om_Info.MaxBytesSbrk = (unsigned long) sbrk(0) - om_SbrkInit;
359 omAssume(om_Info.MaxBytesSbrk >= om_Info.CurrentBytesFromMalloc
360 + om_Info.CurrentBytesFromValloc);
361 }
362#endif
363 }
364 OM_VALLOC_HOOK(size);
365 return page;
366}
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define OM_VALLOC_FROM_SYSTEM
#define omIsAddrPageAligned(addr)
Definition: omBinPage.h:16
unsigned long om_MaxAddr
Definition: omDebugCheck.c:19
unsigned long om_MinAddr
Definition: omDebugCheck.c:20
#define omAssume(x)
Definition: omError.h:85
#define NULL
Definition: omList.c:12
unsigned long om_SbrkInit
Definition: omStats.c:18
omInfo_t om_Info
Definition: omStats.c:16

◆ omAlloc0Large()

void * omAlloc0Large ( size_t  size)

Definition at line 82 of file omAllocSystem.c.

83{
84 void* addr = omAllocLarge(size);
85 memset(addr, 0, size);
86 return addr;
87}
void * omAllocLarge(size_t size)
Definition: omAllocSystem.c:48

◆ omAllocFromSystem()

void * omAllocFromSystem ( size_t  size)

Definition at line 194 of file omAllocSystem.c.

195{
196 void* ptr;
197
199 if (ptr == NULL)
200 {
201 OM_MEMORY_LOW_HOOK();
203 if (ptr == NULL)
204 {
205 OM_OUT_OF_MEMORY_HOOK();
206 exit(1);
207 }
208 }
209#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
211#else
212 size=omSizeOfAddr(ptr);
213#endif
214#ifndef OM_NDEBUG
215 if (((unsigned long) ptr) + size > om_MaxAddr)
216 om_MaxAddr = ((unsigned long) ptr) + size;
217 if (((unsigned long) ptr) < om_MinAddr)
218 om_MinAddr = ((unsigned long) ptr);
219#endif
220
221 om_Info.CurrentBytesFromMalloc += size;
222 if (om_Info.CurrentBytesFromMalloc > om_Info.MaxBytesFromMalloc)
223 {
224 om_Info.MaxBytesFromMalloc = om_Info.CurrentBytesFromMalloc;
225#if defined(OM_HAVE_VALLOC_MMAP) && defined(OM_MALLOC_MAX_BYTES_SYSTEM)
226 if (om_Info.CurrentBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM > om_Info.MaxBytesSystem)
227 om_Info.MaxBytesSystem = om_Info.CurrentBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM;
228#endif
229#if defined(HAVE_SBRK) && !defined(OM_MALLOC_MAX_BYTES_SBRK)
230 if (! om_SbrkInit) om_SbrkInit = (unsigned long) sbrk(0) - size;
231 if (om_Info.MaxBytesFromMalloc
232#ifndef OM_HAVE_VALLOC_MMAP
233 + om_Info.CurrentBytesFromValloc
234#endif
235 > om_Info.MaxBytesSbrk)
236 {
237 om_Info.MaxBytesSbrk = (unsigned long) sbrk(0) - om_SbrkInit;
238 }
239#endif
240 }
241 OM_MALLOC_HOOK(size);
242 return ptr;
243}
size_t omSizeOfAddr(const void *addr)
#define _omSizeOfLargeAddr(addr)
Definition: omAllocSystem.c:79
#define OM_MALLOC_FROM_SYSTEM
Definition: omAllocSystem.c:23

◆ omAllocLarge()

void * omAllocLarge ( size_t  size)

Definition at line 48 of file omAllocSystem.c.

49{
50 char* addr;
51 size = OM_ALIGN_SIZE(size);
52 addr = omAllocFromSystem(size + SIZEOF_STRICT_ALIGNMENT);
53 *((size_t*) addr) = size;
54 return (void *)(addr + SIZEOF_STRICT_ALIGNMENT);
55}
void * omAllocFromSystem(size_t size)

◆ omEmulateValloc()

void * omEmulateValloc ( size_t  size)

Definition at line 159 of file omAllocSystem.c.

160{
161 void* addr;
162 size_t padding = SIZEOF_VOIDP;
163 size = OM_ALIGN_SIZE(size);
164 while (1)
165 {
166 addr = OM_MALLOC_FROM_SYSTEM(size + padding);
167 if (addr == NULL) return NULL;
168 if ((OM_ALIGN_PAGE(addr) + SIZEOF_VOIDP) - (long) addr <= padding)
169 {
170 void* ret_addr = (void*) OM_ALIGN_PAGE(addr);
171 *((void**) ((void*) ret_addr + size)) = addr;
172 return ret_addr;
173 }
174 else
175 {
176 OM_FREE_TO_SYSTEM(addr);
177 padding = padding << 1;
178 }
179 }
180}
#define OM_ALIGN_PAGE(addr)
#define OM_FREE_TO_SYSTEM
Definition: omAllocSystem.c:25

◆ omEmulateVfree()

void omEmulateVfree ( void *  addr,
size_t  size 
)

Definition at line 182 of file omAllocSystem.c.

183{
184 size = OM_ALIGN_SIZE(size);
185 OM_FREE_TO_SYSTEM( *((void**) ((void*) addr + size)) );
186}

◆ omFreeLarge()

void omFreeLarge ( void *  addr)

Definition at line 73 of file omAllocSystem.c.

74{
75 char* _addr = (char *)addr - SIZEOF_STRICT_ALIGNMENT;
76 omFreeSizeToSystem(_addr, *((size_t*) _addr) + SIZEOF_STRICT_ALIGNMENT);
77}
void omFreeSizeToSystem(void *addr, size_t size)

◆ omFreeSizeToSystem()

void omFreeSizeToSystem ( void *  addr,
size_t  size 
)

Definition at line 311 of file omAllocSystem.c.

312{
313 OM_FREE_TO_SYSTEM( addr );
314 om_Info.CurrentBytesFromMalloc -= size;
315 OM_FREE_HOOK(size);
316}

◆ omFreeToSystem()

void omFreeToSystem ( void *  addr)

Definition at line 306 of file omAllocSystem.c.

307{
308 omFreeSizeToSystem(addr, omSizeOfAddr(addr));
309}

◆ omRealloc0Large()

void * omRealloc0Large ( void *  old_addr,
size_t  new_size 
)

Definition at line 89 of file omAllocSystem.c.

90{
91 size_t old_size;
92 char* new_addr;
93
94 omAssume(!omIsBinPageAddr(old_addr));
95
96 old_size = omSizeOfLargeAddr(old_addr);
97
98 new_addr = omReallocLarge(old_addr, new_size);
99 new_size = omSizeOfLargeAddr(new_addr);
100 if (new_size > old_size)
101 memset(new_addr + old_size, 0, new_size - old_size);
102 return (void *)new_addr;
103}
size_t omSizeOfLargeAddr(void *addr)
void * omReallocLarge(void *old_addr, size_t new_size)
Definition: omAllocSystem.c:57
#define omIsBinPageAddr(addr)
Definition: omBinPage.h:68

◆ omReallocFromSystem()

void * omReallocFromSystem ( void *  addr,
size_t  newsize 
)

Definition at line 245 of file omAllocSystem.c.

246{
247 return omReallocSizeFromSystem(addr, omSizeOfAddr(addr), newsize);
248}
void * omReallocSizeFromSystem(void *addr, size_t oldsize, size_t newsize)

◆ omReallocLarge()

void * omReallocLarge ( void *  old_addr,
size_t  new_size 
)

Definition at line 57 of file omAllocSystem.c.

58{
59 char* _old_addr;
60 char* new_addr;
61
62 omAssume(omIsLargeAddr(old_addr));
63
64 new_size = OM_ALIGN_SIZE(new_size);
65 _old_addr = (char *)old_addr - SIZEOF_STRICT_ALIGNMENT;
66 new_addr = omReallocSizeFromSystem(_old_addr,
67 *((size_t*) _old_addr) + SIZEOF_STRICT_ALIGNMENT,
68 new_size + SIZEOF_STRICT_ALIGNMENT);
69 *((size_t*) new_addr) = new_size;
70 return (void *)(new_addr + SIZEOF_STRICT_ALIGNMENT);
71}

◆ omReallocSizeFromSystem()

void * omReallocSizeFromSystem ( void *  addr,
size_t  oldsize,
size_t  newsize 
)

Definition at line 250 of file omAllocSystem.c.

251{
252 void* res;
253
254 /*oldsize=omSizeOfLargeAddr(addr);*/
255 res = OM_REALLOC_FROM_SYSTEM(addr, newsize);
256 if (res == NULL)
257 {
258 OM_MEMORY_LOW_HOOK();
259 /* Can do a realloc again: manpage reads:
260 "If realloc() fails the original block is left untouched -
261 it is not freed or moved." */
262 res = OM_REALLOC_FROM_SYSTEM(addr, newsize);
263 if (res == NULL)
264 {
265 OM_OUT_OF_MEMORY_HOOK();
266 /* should never get here */
267 omAssume(0);
268 exit(1);
269 }
270 }
271 /*newsize=omSizeOfAddr(res);*/
272
273#ifndef OM_NDEBUG
274 if (((unsigned long) res) + newsize > om_MaxAddr)
275 om_MaxAddr = ((unsigned long) res) + newsize;
276 if (((unsigned long) res) < om_MinAddr)
277 om_MinAddr = ((unsigned long) res);
278#endif
279
280 om_Info.CurrentBytesFromMalloc += (long) newsize - (long) oldsize;
281
282
283 if (om_Info.CurrentBytesFromMalloc > om_Info.MaxBytesFromMalloc)
284 {
285 om_Info.MaxBytesFromMalloc = om_Info.CurrentBytesFromMalloc;
286#if defined(OM_HAVE_VALLOC_MMAP) && defined(OM_MALLOC_MAX_BYTES_SYSTEM)
287 if (om_Info.CurrentBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM > om_Info.MaxBytesSystem)
288 om_Info.MaxBytesSystem = om_Info.CurrentBytesFromValloc + OM_MALLOC_MAX_BYTES_SYSTEM;
289#endif
290#if defined(HAVE_SBRK) && !defined(OM_MALLOC_MAX_BYTES_SBRK)
291 if (om_Info.MaxBytesFromMalloc
292#ifndef OM_HAVE_VALLOC_MMAP
293 + om_Info.CurrentBytesFromValloc
294#endif
295 > om_Info.MaxBytesSbrk)
296 {
297 om_Info.MaxBytesSbrk = (unsigned long) sbrk(0) - om_SbrkInit;
298 }
299#endif
300 }
301
302 OM_REALLOC_HOOK(oldsize, newsize);
303 return res;
304}
CanonicalForm res
Definition: facAbsFact.cc:60
#define OM_REALLOC_FROM_SYSTEM
Definition: omAllocSystem.c:24

◆ omSizeOfAddr()

size_t omSizeOfAddr ( const void *  addr)

Definition at line 110 of file omAllocSystem.c.

111{
112 /*if (addr==NULL) return 0; */
113
114 return (omIsBinPageAddr(addr) ?
115#ifdef OM_HAVE_TRACK
116 (omIsBinAddrTrackAddr(addr) ? omOutSizeOfTrackAddr((char *)addr) : omSizeOfBinAddr(addr)) :
117#else
118 omSizeOfBinAddr(addr) :
119#endif
120 omSizeOfLargeAddr((char *)addr));
121}
#define omSizeOfBinAddr(addr)
#define omIsBinAddrTrackAddr(addr)
Definition: omDebug.h:15

◆ omSizeOfLargeAddr()

size_t omSizeOfLargeAddr ( void *  addr)

Definition at line 105 of file omAllocSystem.c.

106{
107 return _omSizeOfLargeAddr((char *)addr);
108}

◆ omSizeWOfAddr()

size_t omSizeWOfAddr ( void *  addr)

Definition at line 123 of file omAllocSystem.c.

124{
125
126 return (omIsBinPageAddr(addr) ?
127#ifdef OM_HAVE_TRACK
128 (omIsBinAddrTrackAddr(addr) ? omOutSizeOfTrackAddr(addr) >> LOG_SIZEOF_LONG : omSizeWOfBinAddr(addr)) :
129#else
130 omSizeWOfBinAddr(addr) :
131#endif
132 omSizeOfLargeAddr(addr) >> LOG_SIZEOF_LONG);
133}
#define omSizeWOfBinAddr(addr)

◆ omVfreeToSystem()

void omVfreeToSystem ( void *  page,
size_t  size 
)

Definition at line 368 of file omAllocSystem.c.

369{
372 om_Info.CurrentBytesFromValloc -= size;
373 OM_VFREE_HOOK(size);
374}
#define OM_VFREE_TO_SYSTEM