X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=src%2Fxmalloc.c;h=f3ac0b7378551db9210f94e518faa7be05267fe1;hp=4365eff5117fca0350a7b26cc6409da395becb6a;hb=f3fa080453f6e57618216f6e58d871fb06f0d1b6;hpb=c6e47cbbff56f39f6d81b079ebaeac41d793d4d9 diff --git a/src/xmalloc.c b/src/xmalloc.c index 4365eff..f3ac0b7 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -1,9 +1,10 @@ -/* - * Copyright (C) 1994-2003, Index Data - * All rights reserved. - * Sebastian Hammer, Adam Dickmeiss - * - * $Id: xmalloc.c,v 1.1 2003-10-27 12:21:36 adam Exp $ +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data + * See the file LICENSE for details. + */ +/** + * \file xmalloc.c + * \brief Implements malloc interface. */ #if HAVE_CONFIG_H @@ -21,6 +22,9 @@ #define TRACE_XMALLOC 1 #endif +static int log_level = 0; +static int log_level_initialized = 0; + #if TRACE_XMALLOC > 1 static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11}; @@ -37,25 +41,32 @@ struct dmalloc_info { struct dmalloc_info *dmalloc_list = 0; + void *xmalloc_d(size_t nbytes, const char *file, int line) { char *res; struct dmalloc_info *dinfo; - + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + 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); + strncpy(dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; dinfo->line = line; dinfo->len = nbytes; - + 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)); res += sizeof(*dinfo) + 8*sizeof(char); memcpy(res + nbytes, tail, 8*sizeof(char)); @@ -67,26 +78,26 @@ 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(LOG_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(LOG_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; - memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char)); + dinfo->next->prev = dinfo->prev; + memcpy((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char)); free(dinfo); return; } @@ -96,47 +107,53 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) struct dmalloc_info *dinfo; char *ptr = (char*) p; char *res; - + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + 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(LOG_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(LOG_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); + strncpy(dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; dinfo->line = line; dinfo->len = nbytes; @@ -144,9 +161,9 @@ 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)); res += sizeof(*dinfo) + 8*sizeof(char); memcpy(res + nbytes, tail, 8*sizeof(char)); @@ -158,21 +175,27 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line) char *res; struct dmalloc_info *dinfo; size_t nbytes = nmemb * size; - + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + 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); + strncpy(dinfo->file, file, sizeof(dinfo->file)-1); dinfo->file[sizeof(dinfo->file)-1] = '\0'; dinfo->line = line; dinfo->len = nbytes; - + 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)); res += sizeof(*dinfo) + 8*sizeof(char); memcpy(res + nbytes, tail, 8*sizeof(char)); @@ -183,16 +206,22 @@ void xmalloc_trav_d(const char *file, int line) { size_t size = 0; struct dmalloc_info *dinfo = dmalloc_list; - - yaz_log (LOG_MALLOC, "malloc_trav %s:%d", file, line); + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + + yaz_log(log_level, "malloc_trav %s:%d", file, line); while (dinfo) { - yaz_log (LOG_MALLOC, " %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_MALLOC, "total bytes %ld", (long) size); + yaz_log(log_level, "total bytes %ld", (long) size); } #else @@ -201,67 +230,106 @@ void xmalloc_trav_d(const char *file, int line) #define xmalloc_d(x, f, l) malloc(x) #define xcalloc_d(x,y, f, l) calloc(x,y) #define xfree_d(x, f, l) free(x) -#define xmalloc_trav_d(f, l) +#define xmalloc_trav_d(f, l) #endif void xmalloc_trav_f(const char *s, const char *file, int line) { + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + xmalloc_trav_d(file, line); } -void *xrealloc_f (void *o, size_t size, const char *file, int line) +void xmalloc_fatal(void) { - void *p = xrealloc_d (o, size, file, line); + exit(1); +} -#if TRACE_XMALLOC - yaz_log (LOG_MALLOC, - "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p); -#endif +void *xrealloc_f(void *o, size_t size, const char *file, int line) +{ + void *p = xrealloc_d(o, size, file, line); + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + + if (log_level) + yaz_log(log_level, + "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p); if (!p) { - yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)", - size); - exit(1); + yaz_log(YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)", + (long) size); + xmalloc_fatal(); } return p; } -void *xmalloc_f (size_t size, const char *file, int line) +void *xmalloc_f(size_t size, const char *file, int line) { - void *p = xmalloc_d (size, file, line); - -#if TRACE_XMALLOC - yaz_log (LOG_MALLOC, "%s:%d: xmalloc(s=%d) %p", file, line, size, p); -#endif + void *p = xmalloc_d(size, file, line); + + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + + if (log_level) + yaz_log(log_level, "%s:%d: xmalloc(s=%ld) %p", file, line, + (long) size, p); + if (!p) { - yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size); - exit (1); + yaz_log(YLOG_FATAL, "Out of memory - malloc (%ld bytes)", + (long) size); + xmalloc_fatal(); } return p; } -void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line) +void *xcalloc_f(size_t nmemb, size_t size, const char *file, int line) { - void *p = xcalloc_d (nmemb, size, file, line); -#if TRACE_XMALLOC - yaz_log (LOG_MALLOC, "%s:%d: xcalloc(s=%d) %p", file, line, size, p); -#endif + void *p = xcalloc_d(nmemb, size, file, line); + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + + if (log_level) + yaz_log(log_level, "%s:%d: xcalloc(s=%ld) %p", file, line, + (long) size, p); + if (!p) { - yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size); - exit (1); + yaz_log(YLOG_FATAL, "Out of memory - calloc (%ld, %ld)", + (long) nmemb, (long) size); + xmalloc_fatal(); } return p; } -char *xstrdup_f (const char *s, const char *file, int line) +char *xstrdup_f(const char *s, const char *file, int line) { - char *p = (char *)xmalloc_d (strlen(s)+1, file, line); -#if TRACE_XMALLOC - yaz_log (LOG_MALLOC, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p); -#endif - strcpy (p, s); + char *p = (char *)xmalloc_d(strlen(s)+1, file, line); + if (!log_level_initialized) + { + log_level = yaz_log_module_level("malloc"); + log_level_initialized = 1; + } + + if (log_level) + yaz_log(log_level, "%s:%d: xstrdup(s=%ld) %p", file, line, + (long) strlen(s)+1, p); + + strcpy(p, s); return p; } @@ -269,9 +337,30 @@ void xfree_f(void *p, const char *file, int line) { if (!p) return ; -#if TRACE_XMALLOC - if (p) - yaz_log (LOG_MALLOC, "%s:%d: xfree %p", file, line, p); -#endif + if (log_level) + yaz_log(log_level, "%s:%d: xfree %p", file, line, p); xfree_d(p, file, line); } + +char *xstrndup_f(const char *s, size_t n, const char *file, int line) +{ + size_t l = strlen(s); + if (l < n) + return xstrdup_f(s, file, line); + { + char *a = (char*) xmalloc_f(n+1, file, line); + memcpy(a, s, n); + a[n] = '\0'; + return a; + } +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +