Defined log level bits app2 and app3
[yaz-moved-to-github.git] / util / xmalloc.c
index 16469d7..5f9b519 100644 (file)
@@ -1,66 +1,9 @@
 /*
- * Copyright (C) 1994-2000, Index Data
+ * Copyright (C) 1994-2003, Index Data
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
- * $Log: xmalloc.c,v $
- * Revision 1.12  2001-09-24 21:51:56  adam
- * New Z39.50 OID utilities: yaz_oidval_to_z3950oid, yaz_str_to_z3950oid
- * and yaz_z3950oid_to_str.
- *
- * Revision 1.11  2000/02/29 13:44:55  adam
- * Check for config.h (currently not generated).
- *
- * Revision 1.10  1999/11/30 13:47:12  adam
- * Improved installation. Moved header files to include/yaz.
- *
- * Revision 1.9  1999/09/10 08:58:32  adam
- * Set TRACE_XMALLOC to 1.
- *
- * Revision 1.8  1999/08/27 09:40:32  adam
- * Renamed logf function to yaz_log. Removed VC++ project files.
- *
- * Revision 1.7  1999/07/13 13:24:53  adam
- * Updated memory debugging memory allocatation routines.
- *
- * Revision 1.6  1998/02/11 11:53:36  adam
- * Changed code so that it compiles as C++.
- *
- * Revision 1.5  1997/10/31 12:20:09  adam
- * Improved memory debugging for xmalloc/nmem.c. References to NMEM
- * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
- * Bug fix: missing fclose in data1_read_espec1.
- *
- * Revision 1.4  1996/07/03 13:21:36  adam
- * Function xfree_f checks for NULL pointer.
- *
- * Revision 1.3  1995/12/05  15:08:44  adam
- * Fixed verbose of xrealloc.
- *
- * Revision 1.2  1995/12/05  11:08:37  adam
- * More verbose malloc routines.
- *
- * Revision 1.1  1995/11/01  11:56:53  quinn
- * Added Xmalloc.
- *
- * Revision 1.6  1995/10/16  14:03:11  quinn
- * Changes to support element set names and espec1
- *
- * Revision 1.5  1995/09/04  12:34:06  adam
- * Various cleanup. YAZ util used instead.
- *
- * Revision 1.4  1994/10/05  10:16:16  quinn
- * Added xrealloc. Fixed bug in log.
- *
- * Revision 1.3  1994/09/26  16:31:37  adam
- * Added xcalloc_f.
- *
- * Revision 1.2  1994/08/18  08:23:26  adam
- * Res.c now use handles. xmalloc defines xstrdup.
- *
- * Revision 1.1  1994/08/17  13:37:54  adam
- * xmalloc.c added to util.
- *
+ * $Id: xmalloc.c,v 1.19 2003-05-16 13:14:26 adam Exp $
  */
 
 #if HAVE_CONFIG_H
 #include <yaz/log.h>
 #include <yaz/xmalloc.h>
 
-#define TRACE_XMALLOC 2
+#ifndef TRACE_XMALLOC
+#define TRACE_XMALLOC 1
+#endif
 
 #if TRACE_XMALLOC > 1
 
-static const unsigned char head[] = {44, 33, 22, 11};
-static const unsigned char tail[] = {11, 22, 33, 44};
-static const unsigned char freed[] = {11, 22, 33, 44};
+static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11};
+static const unsigned char tail[] = {11, 22, 33, 44, 55, 66, 77, 88};
+static const unsigned char freed[] = {11, 22, 33, 44, 55, 66, 77, 88};
 
 struct dmalloc_info {
     int len;
@@ -97,7 +42,7 @@ void *xmalloc_d(size_t nbytes, const char *file, int line)
     char *res;
     struct dmalloc_info *dinfo;
     
-    if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
+    if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
        return 0;
     dinfo = (struct dmalloc_info *) res;
     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
@@ -111,9 +56,9 @@ void *xmalloc_d(size_t nbytes, const char *file, int line)
        dinfo->next->prev = dinfo;
     dmalloc_list = dinfo;
     
-    memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
-    res += sizeof(*dinfo) + 4*sizeof(char);
-    memcpy(res + nbytes, tail, 4*sizeof(char));
+    memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
+    res += sizeof(*dinfo) + 8*sizeof(char);
+    memcpy(res + nbytes, tail, 8*sizeof(char));
     return res;
 }
 
@@ -124,13 +69,13 @@ void xfree_d(void *ptr, const char *file, int line)
     if (!ptr)
        return;
     dinfo = (struct dmalloc_info *)
-       ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
-    if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
+       ((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);
         abort();
     }
-    if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
+    if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char)))
     {
        yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
         abort();
@@ -141,7 +86,7 @@ void xfree_d(void *ptr, const char *file, int line)
        dmalloc_list = dinfo->next;
     if (dinfo->next)
        dinfo->next->prev = dinfo->prev;
-    memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
+    memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
     free(dinfo);
     return;
 }
@@ -156,18 +101,18 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
     {
        if (!nbytes)
            return 0;
-       res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
+       res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char));
     }
     else
     {
-       if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
+       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-4*sizeof(char) - sizeof(*dinfo));
-       if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
+       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);
@@ -186,7 +131,7 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
            return 0;
        }
        res = (char *)
-           realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
+           realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char));
     }
     if (!res)
        return 0;
@@ -202,9 +147,9 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
        dmalloc_list->prev = dinfo;
     dmalloc_list = dinfo;
     
-    memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
-    res += sizeof(*dinfo) + 4*sizeof(char);
-    memcpy(res + nbytes, tail, 4*sizeof(char));
+    memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
+    res += sizeof(*dinfo) + 8*sizeof(char);
+    memcpy(res + nbytes, tail, 8*sizeof(char));
     return res;
 }
 
@@ -214,7 +159,7 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
     struct dmalloc_info *dinfo;
     size_t nbytes = nmemb * size;
     
-    if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
+    if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
        return 0;
     dinfo = (struct dmalloc_info *) res;
     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
@@ -228,9 +173,9 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
        dinfo->next->prev = dinfo;
     dmalloc_list = dinfo;
     
-    memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
-    res += sizeof(*dinfo) + 4*sizeof(char);
-    memcpy(res + nbytes, tail, 4*sizeof(char));
+    memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
+    res += sizeof(*dinfo) + 8*sizeof(char);
+    memcpy(res + nbytes, tail, 8*sizeof(char));
     return res;
 }
 
@@ -239,15 +184,15 @@ void xmalloc_trav_d(const char *file, int line)
     size_t size = 0;
     struct dmalloc_info *dinfo = dmalloc_list;
     
-    yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
+    yaz_log (LOG_MALLOC, "malloc_trav %s:%d", file, line);
     while (dinfo)
     {
-       yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
-             ((char*) dinfo)+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
+       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_LOG, "total bytes %ld", (long) size);
+    yaz_log (LOG_MALLOC, "total bytes %ld", (long) size);
 }
 
 #else
@@ -269,7 +214,7 @@ void *xrealloc_f (void *o, size_t size, const char *file, int line)
     void *p = xrealloc_d (o, size, file, line);
 
 #if TRACE_XMALLOC
-    yaz_log (LOG_DEBUG,
+    yaz_log (LOG_MALLOC,
             "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
 #endif
     if (!p)
@@ -286,7 +231,7 @@ void *xmalloc_f (size_t size, const char *file, int line)
     void *p = xmalloc_d (size, file, line);
     
 #if TRACE_XMALLOC
-    yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
+    yaz_log (LOG_MALLOC, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
 #endif
     if (!p)
     {
@@ -300,7 +245,7 @@ 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_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
+    yaz_log (LOG_MALLOC, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
 #endif
     if (!p)
     {
@@ -314,7 +259,7 @@ 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_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
+    yaz_log (LOG_MALLOC, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
 #endif
     strcpy (p, s);
     return p;
@@ -326,7 +271,7 @@ void xfree_f(void *p, const char *file, int line)
         return ;
 #if TRACE_XMALLOC
     if (p)
-        yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
+        yaz_log (LOG_MALLOC, "%s:%d: xfree %p", file, line, p);
 #endif
     xfree_d(p, file, line);
 }