12 #if !defined(C99_H) || !defined(FAIL_H)
13 #error "mem.h" requires "c99.h" and "fail.h"
30 # define PRINT_MALLOCS 0
34 # define comm_gbl_id PREFIXED_NAME(comm_gbl_id)
35 # define comm_gbl_np PREFIXED_NAME(comm_gbl_np)
45 static inline void *
smalloc(
size_t size,
const char *file,
unsigned line)
49 fprintf(stdout,
"MEM: proc %04d: %p = malloc(%ld) @ %s(%u)\n",
50 (
int)
comm_gbl_id,res,(
long)size,file,line), fflush(stdout);
53 fail(1,file,line,
"allocation of %ld bytes failed\n",(
long)size);
58 size_t nmemb,
size_t size,
const char *file,
unsigned line)
60 void *
restrict res = calloc(nmemb, size);
62 fprintf(stdout,
"MEM: proc %04d: %p = calloc(%ld) @ %s(%u)\n",
63 (
int)
comm_gbl_id,res,(
long)size*nmemb,file,line), fflush(stdout);
66 fail(1,file,line,
"allocation of %ld bytes failed\n",
72 void *
restrict ptr,
size_t size,
const char *file,
unsigned line)
74 void *
restrict res = realloc(ptr, size);
78 fprintf(stdout,
"MEM: proc %04d: %p freed by realloc @ %s(%u)\n",
80 fprintf(stdout,
"MEM: proc %04d: %p = realloc of %p to %lu @ %s(%u)\n",
81 (
int)comm_gbl_id,res,ptr,(
long)size,file,line), fflush(stdout);
83 fprintf(stdout,
"MEM: proc %04d: %p realloc'd to %lu @ %s(%u)\n",
84 (
int)
comm_gbl_id,res,(
long)size,file,line), fflush(stdout);
87 fail(1,file,line,
"allocation of %ld bytes failed\n",(
long)size);
91 #define tmalloc(type, count) \
92 ((type*) smalloc((count)*sizeof(type),__FILE__,__LINE__) )
93 #define tcalloc(type, count) \
94 ((type*) scalloc((count),sizeof(type),__FILE__,__LINE__) )
95 #define trealloc(type, ptr, count) \
96 ((type*) srealloc((ptr),(count)*sizeof(type),__FILE__,__LINE__) )
99 static inline void sfree(
void *
restrict ptr,
const char *file,
unsigned line)
102 fprintf(stdout,
"MEM: proc %04d: %p freed @ %s(%u)\n",
105 #define free(x) sfree(x,__FILE__,__LINE__)
112 #define null_array {0,0,0}
114 const char *file,
unsigned line)
119 const char *file,
unsigned line)
124 const char *file,
unsigned line)
135 #define array_free(a) (free((a)->ptr))
136 #define array_init(T,a,max) array_init_(a,max,sizeof(T),__FILE__,__LINE__)
137 #define array_resize(T,a,max) array_resize_(a,max,sizeof(T),__FILE__,__LINE__)
138 #define array_reserve(T,a,min) array_reserve_(a,min,sizeof(T),__FILE__,__LINE__)
141 const char *file,
unsigned line)
144 memcpy(out+d->
n*size, s, n*size);
148 #define array_cat(T,d,s,n) array_cat_(sizeof(T),d,s,n,__FILE__,__LINE__)
154 #define null_buffer null_array
155 #define buffer_init(b,max) array_init(char,b,max)
156 #define buffer_resize(b,max) array_resize(char,b,max)
157 #define buffer_reserve(b,max) array_reserve(char,b,max)
158 #define buffer_free(b) array_free(b)
163 #define ALIGNOF(T) offsetof(struct { char c; T x; }, x)
164 static size_t align_as_(
size_t a,
size_t n) {
return (n+a-1)/a*a; }
165 #define align_as(T,n) align_as_(ALIGNOF(T),n)
166 #define align_ptr(T,base,offset) ((T*)((char*)(base)+align_as(T,offset)))
static void * array_reserve_(struct array *a, size_t min, size_t size, const char *file, unsigned line)
static void array_resize_(struct array *a, size_t max, size_t size, const char *file, unsigned line)
static void array_init_(struct array *a, size_t max, size_t size, const char *file, unsigned line)
static size_t align_as_(size_t a, size_t n)
static void * srealloc(void *restrict ptr, size_t size, const char *file, unsigned line)
static void array_cat_(size_t size, struct array *d, const void *s, size_t n, const char *file, unsigned line)
static void * scalloc(size_t nmemb, size_t size, const char *file, unsigned line)
static void * smalloc(size_t size, const char *file, unsigned line)
void fail(int status, const char *file, unsigned line, const char *fmt,...)