1
/* $Id: memory.h,v 1.17 2003/09/28 23:44:32 jonas Exp $ */
3
#ifndef EL__UTIL_MEMORY_H
4
#define EL__UTIL_MEMORY_H
6
/* If defined, we'll crash if ALLOC_MAXTRIES is attained,
7
* if not defined, we'll try to continue. */
8
/* #define CRASH_IF_ALLOC_MAXTRIES */
9
/* Max. number of retry in case of memory allocation failure. */
10
#define ALLOC_MAXTRIES 3
11
/* Delay in seconds between each alloc try. */
14
#define fmem_alloc(x) mem_alloc(x)
15
#define fmem_free(x) mem_free(x)
20
#include "util/memdebug.h"
22
#define mem_alloc(x) debug_mem_alloc(__FILE__, __LINE__, x)
23
#define mem_calloc(x, y) debug_mem_calloc(__FILE__, __LINE__, x, y)
24
#define mem_free(x) debug_mem_free(__FILE__, __LINE__, x)
25
#define mem_realloc(x, y) debug_mem_realloc(__FILE__, __LINE__, x, y)
31
/* Cygwin wants some size_t definition here... let's try to make it happy
33
#include <sys/types.h>
36
void *mem_alloc(size_t);
37
void *mem_calloc(size_t, size_t);
38
void mem_free(void *);
39
void *mem_realloc(void *, size_t);
45
/* TODO: For enhanced portability, checks at configure time:
47
* realloc(NULL, 0) -> NULL
48
* realloc(p, 0) <-> free(p)
49
* realloc(NULL, n) <-> malloc(n)
50
* Some old implementations may not respect these rules.
51
* For these we need some replacement functions.
52
* This should not be an issue on most modern systems.
55
# define mem_alloc(size) malloc(size)
56
# define mem_calloc(count, size) calloc(count, size)
57
# define mem_free(p) free(p)
58
# define mem_realloc(p, size) realloc(p, size)
61
/* fmem_* functions should be use for allocation and freeing of memory
63
* See alloca(3) manpage. */
73
#define fmem_alloc(x) alloca(x)
76
#else /* HAVE_ALLOCA */
78
#define fmem_alloc(x) mem_alloc(x)
79
#define fmem_free(x) mem_free(x)
81
#endif /* HAVE_ALLOCA */
85
#endif /* LEAK_DEBUG */
88
/* Granular memory allocation. */
90
/* The ``old'' style granularity. XXX: Must be power of 2 */
91
#define ALLOC_GR 0x100
93
#include <string.h> /* for memset() */
95
/* The granularity used by the aligned memory functions below must be a mask
96
* with all bits set from but not including the most significant bit and down.
97
* So if an alignment of 256 is wanted use 0xFF. */
99
#define ALIGN_MEMORY_SIZE(x, gr) (((x) + (gr)) & ~(gr))
104
unsigned char *file, int line,
106
void **ptr, size_t old, size_t new, size_t objsize, int mask)
108
size_t newsize = ALIGN_MEMORY_SIZE(new, mask);
109
size_t oldsize = ALIGN_MEMORY_SIZE(old, mask);
111
if (newsize > oldsize) {
118
data = debug_mem_realloc(file, line, *ptr, newsize);
120
data = mem_realloc(*ptr, newsize);
122
if (!data) return NULL;
125
memset(&data[oldsize], 0, newsize - oldsize);
132
#define mem_align_alloc(ptr, old, new, objsize, mask) \
133
mem_align_alloc__(__FILE__, __LINE__, (void **)ptr, old, new, objsize, mask)
135
#define mem_align_alloc(ptr, old, new, objsize, mask) \
136
mem_align_alloc__((void **)ptr, old, new, objsize, mask)