23 #define TRACE_XMALLOC 1
27 #define MALLOC_SIZE_MAX ((size_t)(-1) / 4)
34 static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11};
35 static const unsigned char tail[] = {11, 22, 33, 44, 55, 66, 77, 88};
36 static const unsigned char freed[] = {11, 22, 33, 44, 55, 66, 77, 88};
42 struct dmalloc_info *next;
43 struct dmalloc_info *prev;
46 struct dmalloc_info *dmalloc_list = 0;
49 void *
xmalloc_d(
size_t nbytes,
const char *file,
int line)
52 struct dmalloc_info *dinfo;
60 if (!(res = (
char*)
malloc(nbytes +
sizeof(*dinfo)+16*
sizeof(
char))))
62 dinfo = (
struct dmalloc_info *) res;
63 strncpy(dinfo->file, file,
sizeof(dinfo->file)-1);
64 dinfo->file[
sizeof(dinfo->file)-1] =
'\0';
69 dinfo->next = dmalloc_list;
71 dinfo->next->prev = dinfo;
74 memcpy(res +
sizeof(*dinfo), head, 8*
sizeof(
char));
75 res +=
sizeof(*dinfo) + 8*
sizeof(char);
76 memcpy(res + nbytes, tail, 8*
sizeof(
char));
80 void xfree_d(
void *ptr,
const char *file,
int line)
82 struct dmalloc_info *dinfo;
86 dinfo = (
struct dmalloc_info *)
87 ((
char*)ptr - 8*
sizeof(char) -
sizeof(*dinfo));
88 if (memcmp(head, (
char*) ptr - 8*
sizeof(char), 8*
sizeof(
char)))
93 if (memcmp((
char*) ptr + dinfo->len, tail, 8*
sizeof(
char)))
99 dinfo->prev->next = dinfo->next;
101 dmalloc_list = dinfo->next;
103 dinfo->next->prev = dinfo->prev;
104 memcpy((
char*) ptr - 8*
sizeof(
char), freed, 8*
sizeof(
char));
109 void *
xrealloc_d(
void *p,
size_t nbytes,
const char *file,
int line)
111 struct dmalloc_info *dinfo;
112 char *ptr = (
char*) p;
125 res = (
char *)
malloc(nbytes +
sizeof(*dinfo) + 16*
sizeof(char));
129 if (memcmp(head, ptr - 8*
sizeof(
char), 8*
sizeof(char)))
135 dinfo = (
struct dmalloc_info *) (ptr-8*
sizeof(
char) -
sizeof(*dinfo));
136 if (memcmp(ptr + dinfo->len, tail, 8*
sizeof(
char)))
143 dinfo->prev->next = dinfo->next;
145 dmalloc_list = dinfo->next;
147 dinfo->next->prev = dinfo->prev;
155 realloc(dinfo, nbytes +
sizeof(*dinfo) + 16*
sizeof(char));
159 dinfo = (
struct dmalloc_info *) res;
160 strncpy(dinfo->file, file,
sizeof(dinfo->file)-1);
161 dinfo->file[
sizeof(dinfo->file)-1] =
'\0';
166 dinfo->next = dmalloc_list;
168 dmalloc_list->prev = dinfo;
169 dmalloc_list = dinfo;
171 memcpy(res +
sizeof(*dinfo), head, 8*
sizeof(
char));
172 res +=
sizeof(*dinfo) + 8*
sizeof(char);
173 memcpy(res + nbytes, tail, 8*
sizeof(
char));
177 void *
xcalloc_d(
size_t nmemb,
size_t size,
const char *file,
int line)
180 struct dmalloc_info *dinfo;
181 size_t nbytes = nmemb * size;
189 if (!(res = (
char*) calloc(1, nbytes+
sizeof(*dinfo)+16*
sizeof(
char))))
191 dinfo = (
struct dmalloc_info *) res;
192 strncpy(dinfo->file, file,
sizeof(dinfo->file)-1);
193 dinfo->file[
sizeof(dinfo->file)-1] =
'\0';
198 dinfo->next = dmalloc_list;
200 dinfo->next->prev = dinfo;
201 dmalloc_list = dinfo;
203 memcpy(res +
sizeof(*dinfo), head, 8*
sizeof(
char));
204 res +=
sizeof(*dinfo) + 8*
sizeof(char);
205 memcpy(res + nbytes, tail, 8*
sizeof(
char));
212 struct dmalloc_info *dinfo = dmalloc_list;
224 ((
char*) dinfo)+
sizeof(*dinfo)+8*
sizeof(
char), dinfo->len);
233 #define xrealloc_d(o, x, f, l) realloc(o, x)
234 #define xmalloc_d(x, f, l) malloc(x)
235 #define xcalloc_d(x,y, f, l) calloc(x,y)
236 #define xfree_d(x, f, l) free(x)
237 #define xmalloc_trav_d(f, l)
257 void *
xrealloc_f(
void *o,
size_t size,
const char *file,
int line)
269 "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (
long) size, o, p);
273 file, line, (
long) size);
279 void *
xmalloc_f(
size_t size,
const char *file,
int line)
296 file, line, (
long) size);
302 void *
xcalloc_f(
size_t nmemb,
size_t size,
const char *file,
int line)
304 void *p =
xcalloc_d(nmemb, size, file, line);
318 file, line, (
long) nmemb, (
long) size);
324 char *
xstrdup_f(
const char *s,
const char *file,
int line)
326 char *p = (
char *)
xmalloc_d(strlen(s)+1, file, line);
335 (
long) strlen(s)+1, p);
341 void xfree_f(
void *p,
const char *file,
int line)
350 char *
xstrndup_f(
const char *s,
size_t n,
const char *file,
int line)
352 size_t l = strlen(s);
356 char *a = (
char*)
xmalloc_f(n+1, file, line);
void yaz_log(int level, const char *fmt,...)
Writes log message.
int yaz_log_module_level(const char *name)
returns level for module
#define YLOG_FATAL
log level: fatal
#define xmalloc_trav_d(f, l)
char * xstrndup_f(const char *s, size_t n, const char *file, int line)
strndup
void * xrealloc_f(void *o, size_t size, const char *file, int line)
realloc
#define xcalloc_d(x, y, f, l)
void xmalloc_trav_f(const char *s, const char *file, int line)
logs all xmalloc buffers
#define xrealloc_d(o, x, f, l)
#define xmalloc_d(x, f, l)
static int log_level_initialized
void xmalloc_fatal(size_t size)
void xfree_f(void *p, const char *file, int line)
free
void * xcalloc_f(size_t nmemb, size_t size, const char *file, int line)
calloc
char * xstrdup_f(const char *s, const char *file, int line)
strdup
void * xmalloc_f(size_t size, const char *file, int line)
malloc
Header for memory handling functions.