My Project
Loading...
Searching...
No Matches
omAllocSystem.c
Go to the documentation of this file.
1/*******************************************************************
2 * File: omAllocSystem.c
3 * Purpose: implementation of main lowl-level alloc functions
4 * Author: [email protected] (Olaf Bachmann)
5 * Created: 11/99
6 *******************************************************************/
7#ifndef OM_ALLOC_SYSTEM_C
8#define OM_ALLOC_SYSTEM_C
9
10#include <unistd.h>
11#include <limits.h>
12
13
14#include "omConfig.h"
15
16#ifdef HAVE_OMALLOC
17#include "omDefaultConfig.h"
18#include "omMalloc.h"
19#include "omalloc.h"
20/* include after omMalloc.h */
21#include <string.h>
22
23#define OM_MALLOC_FROM_SYSTEM OM_MALLOC_MALLOC
24#define OM_REALLOC_FROM_SYSTEM OM_MALLOC_REALLOC
25#define OM_FREE_TO_SYSTEM OM_MALLOC_FREE
26
27/*******************************************************************
28 *
29 * AllocLarge/FreeLarge if malloc can not return sizeof(addr)
30 *
31 *******************************************************************/
32/* allocation of large addr */
33
34#if defined(HAVE_MALLOC_SIZE) || defined(HAVE_MALLOC_USABLE_SIZE)
35 #include <stdlib.h>
36 #ifdef HAVE_MALLOC_H
37 #include <malloc.h>
38 #elif defined(HAVE_MALLOC_MALLOC_H)
39 #include <malloc/malloc.h>
40 #endif
41#endif
42
43#if defined(HAVE_MALLOC_SIZE)
44 #define _omSizeOfLargeAddr(addr) (malloc_size(addr))
45#elif defined(HAVE_MALLOC_USABLE_SIZE)
46 #define _omSizeOfLargeAddr(addr) (malloc_usable_size(addr))
47#else
48void* omAllocLarge(size_t size)
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}
56
57void* omReallocLarge(void* old_addr, size_t new_size)
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}
72
73void omFreeLarge(void* addr)
74{
75 char* _addr = (char *)addr - SIZEOF_STRICT_ALIGNMENT;
76 omFreeSizeToSystem(_addr, *((size_t*) _addr) + SIZEOF_STRICT_ALIGNMENT);
77}
78
79#define _omSizeOfLargeAddr(addr) (*((size_t*) ((char*) addr - SIZEOF_STRICT_ALIGNMENT)))
80#endif /* HAVE_MALLOC_SIZE/HAVE_MALLOC_USABLE_SIZE */
81
82void* omAlloc0Large(size_t size)
83{
84 void* addr = omAllocLarge(size);
85 memset(addr, 0, size);
86 return addr;
87}
88
89void* omRealloc0Large(void* old_addr, size_t new_size)
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}
104
105size_t omSizeOfLargeAddr(void* addr)
106{
107 return _omSizeOfLargeAddr((char *)addr);
108}
109
110size_t omSizeOfAddr(const void* addr)
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}
122
123size_t omSizeWOfAddr(void* addr)
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}
134
135/*******************************************************************
136 *
137 * Valloc
138 *
139 *******************************************************************/
140#ifdef OM_HAVE_VALLOC_MMAP
141
142#include "omMmap.c"
143
144#define OM_VALLOC_FROM_SYSTEM omVallocMmap
145#define OM_VFREE_TO_SYSTEM omVfreeMmap
146
147#elif defined(OM_HAVE_VALLOC_MALLOC)
148
149#define OM_VALLOC_FROM_SYSTEM OM_MALLOC_VALLOC
150#define OM_VFREE_TO_SYSTEM OM_MALLOC_VFREE
151
152#else
153
154#define OM_VALLOC_FROM_SYSTEM omEmulateValloc
155#define OM_VFREE_TO_SYSTEM omEmulateVfree
156
157#define OM_ALIGN_PAGE(addr) ( ((long)addr + (SIZEOF_SYSTEM_PAGE -1)) & ~(SIZEOF_SYSTEM_PAGE - 1))
158/* now we implement an emulation */
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}
181
182void omEmulateVfree(void* addr, size_t size)
183{
184 size = OM_ALIGN_SIZE(size);
185 OM_FREE_TO_SYSTEM( *((void**) ((void*) addr + size)) );
186}
187#endif /* OM_HAVE_VALLOC_MMAP */
188
189/*******************************************************************
190 *
191 * System-level Alloc/Free
192 *
193 *******************************************************************/
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}
244
245void* omReallocFromSystem(void* addr, size_t newsize)
246{
247 return omReallocSizeFromSystem(addr, omSizeOfAddr(addr), newsize);
248}
249
250void* omReallocSizeFromSystem(void* addr, size_t oldsize, size_t newsize)
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}
305
306void omFreeToSystem(void* addr)
307{
308 omFreeSizeToSystem(addr, omSizeOfAddr(addr));
309}
310
311void omFreeSizeToSystem(void* addr, size_t size)
312{
313 OM_FREE_TO_SYSTEM( addr );
314 om_Info.CurrentBytesFromMalloc -= size;
315 OM_FREE_HOOK(size);
316}
317
318void* _omVallocFromSystem(size_t size, int fail)
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}
367
368void omVfreeToSystem(void* page, size_t size)
369{
372 om_Info.CurrentBytesFromValloc -= size;
373 OM_VFREE_HOOK(size);
374}
375
376#endif /*HAVE_OMALLOC*/
377#endif /* OM_ALLOC_SYSTEM_C */
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
CanonicalForm res
Definition: facAbsFact.cc:60
#define omSizeOfBinAddr(addr)
#define omSizeWOfBinAddr(addr)
void * _omVallocFromSystem(size_t size, int fail)
void * omAllocFromSystem(size_t size)
void omEmulateVfree(void *addr, size_t size)
void * omReallocFromSystem(void *addr, size_t newsize)
void * omEmulateValloc(size_t size)
void omVfreeToSystem(void *page, size_t size)
size_t omSizeOfLargeAddr(void *addr)
#define OM_REALLOC_FROM_SYSTEM
Definition: omAllocSystem.c:24
void omFreeLarge(void *addr)
Definition: omAllocSystem.c:73
void * omRealloc0Large(void *old_addr, size_t new_size)
Definition: omAllocSystem.c:89
void omFreeSizeToSystem(void *addr, size_t size)
#define OM_ALIGN_PAGE(addr)
#define OM_VFREE_TO_SYSTEM
void * omReallocLarge(void *old_addr, size_t new_size)
Definition: omAllocSystem.c:57
size_t omSizeOfAddr(const void *addr)
#define OM_FREE_TO_SYSTEM
Definition: omAllocSystem.c:25
void omFreeToSystem(void *addr)
void * omAllocLarge(size_t size)
Definition: omAllocSystem.c:48
#define _omSizeOfLargeAddr(addr)
Definition: omAllocSystem.c:79
void * omReallocSizeFromSystem(void *addr, size_t oldsize, size_t newsize)
#define OM_MALLOC_FROM_SYSTEM
Definition: omAllocSystem.c:23
#define OM_VALLOC_FROM_SYSTEM
void * omAlloc0Large(size_t size)
Definition: omAllocSystem.c:82
#define omIsBinPageAddr(addr)
Definition: omBinPage.h:68
#define omIsAddrPageAligned(addr)
Definition: omBinPage.h:16
unsigned long om_MaxAddr
Definition: omDebugCheck.c:19
unsigned long om_MinAddr
Definition: omDebugCheck.c:20
#define omIsBinAddrTrackAddr(addr)
Definition: omDebug.h:15
#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
#define omSizeWOfAddr(P)
Definition: xalloc.h:223