X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fxmalloc.c;h=d48daa4d5aaee128d8c583ad9938456967de39f8;hp=948e5183fc86f941be974bfb564630afb46879a1;hb=3f282496986cbae7fd4bd7f95d44bf47a2cc6874;hpb=3b96525a40981e162b959f3e842e0ff20e314320 diff --git a/src/xmalloc.c b/src/xmalloc.c index 948e518..d48daa4 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -1,8 +1,8 @@ /* - * Copyright (C) 1994-2004, Index Data + * Copyright (C) 1995-2005, Index Data ApS * All rights reserved. * - * $Id: xmalloc.c,v 1.3 2004-11-18 15:18:14 heikki Exp $ + * $Id: xmalloc.c,v 1.7 2006-04-21 10:28:07 adam Exp $ */ /** * \file xmalloc.c @@ -17,7 +17,7 @@ #include #include -#include +#include #include #ifndef TRACE_XMALLOC @@ -56,7 +56,7 @@ void *xmalloc_d(size_t nbytes, const char *file, int line) } if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char)))) - return 0; + return 0; dinfo = (struct dmalloc_info *) res; strncpy (dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; @@ -66,7 +66,7 @@ void *xmalloc_d(size_t nbytes, const char *file, int line) dinfo->prev = 0; dinfo->next = dmalloc_list; if (dinfo->next) - dinfo->next->prev = dinfo; + dinfo->next->prev = dinfo; dmalloc_list = dinfo; memcpy(res + sizeof(*dinfo), head, 8*sizeof(char)); @@ -80,25 +80,25 @@ void xfree_d(void *ptr, const char *file, int line) struct dmalloc_info *dinfo; if (!ptr) - return; + return; dinfo = (struct dmalloc_info *) - ((char*)ptr - 8*sizeof(char) - sizeof(*dinfo)); + ((char*)ptr - 8*sizeof(char) - sizeof(*dinfo)); if (memcmp(head, (char*) ptr - 8*sizeof(char), 8*sizeof(char))) { - yaz_log(YLOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr); + yaz_log(YLOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr); abort(); } if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char))) { - yaz_log(YLOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr); + yaz_log(YLOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr); abort(); } if (dinfo->prev) - dinfo->prev->next = dinfo->next; + dinfo->prev->next = dinfo->next; else - dmalloc_list = dinfo->next; + dmalloc_list = dinfo->next; if (dinfo->next) - dinfo->next->prev = dinfo->prev; + dinfo->next->prev = dinfo->prev; memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char)); free(dinfo); return; @@ -118,42 +118,42 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) if (!ptr) { - if (!nbytes) - return 0; - res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char)); + if (!nbytes) + return 0; + res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char)); } else { - if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char))) - { - yaz_log(YLOG_FATAL, "xrealloc_d bad head, %s:%d, %p", - file, line, ptr); - abort(); - } - dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo)); - if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char))) - { - yaz_log(YLOG_FATAL, "xrealloc_d bad tail, %s:%d, %p", - file, line, ptr); - abort(); - } - if (dinfo->prev) - dinfo->prev->next = dinfo->next; - else - dmalloc_list = dinfo->next; - if (dinfo->next) - dinfo->next->prev = dinfo->prev; - - if (!nbytes) - { - free (dinfo); - return 0; - } - res = (char *) - realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char)); + if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char))) + { + yaz_log(YLOG_FATAL, "xrealloc_d bad head, %s:%d, %p", + file, line, ptr); + abort(); + } + dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo)); + if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char))) + { + yaz_log(YLOG_FATAL, "xrealloc_d bad tail, %s:%d, %p", + file, line, ptr); + abort(); + } + if (dinfo->prev) + dinfo->prev->next = dinfo->next; + else + dmalloc_list = dinfo->next; + if (dinfo->next) + dinfo->next->prev = dinfo->prev; + + if (!nbytes) + { + free (dinfo); + return 0; + } + res = (char *) + realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char)); } if (!res) - return 0; + return 0; dinfo = (struct dmalloc_info *) res; strncpy (dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; @@ -163,7 +163,7 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) dinfo->prev = 0; dinfo->next = dmalloc_list; if (dmalloc_list) - dmalloc_list->prev = dinfo; + dmalloc_list->prev = dinfo; dmalloc_list = dinfo; memcpy(res + sizeof(*dinfo), head, 8*sizeof(char)); @@ -185,7 +185,7 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line) } if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char)))) - return 0; + return 0; dinfo = (struct dmalloc_info *) res; strncpy (dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; @@ -195,7 +195,7 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line) dinfo->prev = 0; dinfo->next = dmalloc_list; if (dinfo->next) - dinfo->next->prev = dinfo; + dinfo->next->prev = dinfo; dmalloc_list = dinfo; memcpy(res + sizeof(*dinfo), head, 8*sizeof(char)); @@ -218,10 +218,10 @@ void xmalloc_trav_d(const char *file, int line) yaz_log (log_level, "malloc_trav %s:%d", file, line); while (dinfo) { - yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line, - ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len); - size += dinfo->len; - dinfo = dinfo->next; + yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line, + ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len); + size += dinfo->len; + dinfo = dinfo->next; } yaz_log (log_level, "total bytes %ld", (long) size); } @@ -258,12 +258,12 @@ void *xrealloc_f (void *o, size_t size, const char *file, int line) if(log_level) yaz_log (log_level, - "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p); + "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p); if (!p) { - yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%d bytes)", - size); - exit(1); + yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)", + (long) size); + exit(1); } return p; } @@ -279,11 +279,13 @@ void *xmalloc_f (size_t size, const char *file, int line) } if (log_level) - yaz_log (log_level, "%s:%d: xmalloc(s=%d) %p", file, line, size, p); + yaz_log (log_level, "%s:%d: xmalloc(s=%ld) %p", file, line, + (long) size, p); if (!p) { - yaz_log (YLOG_FATAL, "Out of memory - malloc (%d bytes)", size); + yaz_log (YLOG_FATAL, "Out of memory - malloc (%ld bytes)", + (long) size); exit (1); } return p; @@ -299,11 +301,13 @@ void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line) } if (log_level) - yaz_log (log_level, "%s:%d: xcalloc(s=%d) %p", file, line, size, p); + yaz_log (log_level, "%s:%d: xcalloc(s=%ld) %p", file, line, + (long) size, p); if (!p) { - yaz_log (YLOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size); + yaz_log (YLOG_FATAL, "Out of memory - calloc (%ld, %ld)", + (long) nmemb, (long) size); exit (1); } return p; @@ -319,7 +323,8 @@ char *xstrdup_f (const char *s, const char *file, int line) } if (log_level) - yaz_log (log_level, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p); + yaz_log (log_level, "%s:%d: xstrdup(s=%ld) %p", file, line, + (long) strlen(s)+1, p); strcpy (p, s); return p; @@ -333,3 +338,11 @@ void xfree_f(void *p, const char *file, int line) yaz_log (log_level, "%s:%d: xfree %p", file, line, p); xfree_d(p, file, line); } +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +