X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fnmem.c;h=9acaaeec3d25695554d1c1fdf4b8c77883e47b7e;hb=59fc25662cfb2144a8f9173616aa70a1da9e4f44;hp=16d84219bc2e2678e59feee31c3fde53723a8da6;hpb=4d45f16d48247ba07d2eca0592239bba2d36b65d;p=yaz-moved-to-github.git diff --git a/util/nmem.c b/util/nmem.c index 16d8421..9acaaee 100644 --- a/util/nmem.c +++ b/util/nmem.c @@ -1,10 +1,31 @@ /* - * Copyright (c) 1995-1999, Index Data. + * Copyright (c) 1995-2000, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * * $Log: nmem.c,v $ - * Revision 1.17 1999-07-13 13:28:25 adam + * Revision 1.24 2000-05-11 14:37:55 adam + * Minor changes. + * + * Revision 1.23 2000/05/09 10:55:05 adam + * Public nmem_print_list (for debugging). + * + * Revision 1.22 2000/05/03 22:00:00 adam + * Reference counter (if multiple modules are init/freeing nmem). + * + * Revision 1.21 2000/02/29 13:44:55 adam + * Check for config.h (currently not generated). + * + * Revision 1.20 2000/01/06 14:59:13 adam + * Added oid_init/oid_exit. Changed oid_exit. + * + * Revision 1.19 1999/11/30 13:47:12 adam + * Improved installation. Moved header files to include/yaz. + * + * Revision 1.18 1999/08/27 09:40:32 adam + * Renamed logf function to yaz_log. Removed VC++ project files. + * + * Revision 1.17 1999/07/13 13:28:25 adam * Better debugging for NMEM routines. * * Revision 1.16 1999/03/31 11:18:25 adam @@ -68,12 +89,15 @@ * This is a simple and fairly wasteful little module for nibble memory * allocation. Evemtually we'll put in something better. */ +#if HAVE_CONFIG_H +#include +#endif #include #include -#include -#include -#include +#include +#include +#include #ifdef WIN32 #include #elif _REENTRANT @@ -107,14 +131,14 @@ static int nmem_active_no = 0; static int nmem_init_flag = 0; #if NMEM_DEBUG -struct nmem_debug { +struct nmem_debug_info { void *p; char file[40]; int line; - struct nmem_debug *next; + struct nmem_debug_info *next; }; -struct nmem_debug *nmem_debug_list = 0; +struct nmem_debug_info *nmem_debug_list = 0; #endif static void free_block(nmem_block *p) @@ -122,19 +146,20 @@ static void free_block(nmem_block *p) p->next = freelist; freelist = p; #if NMEM_DEBUG - logf (LOG_DEBUG, "nmem free_block p=%p", p); + yaz_log (LOG_DEBUG, "nmem free_block p=%p", p); #endif } #if NMEM_DEBUG void nmem_print_list (void) { - struct nmem_debug *p; + struct nmem_debug_info *p; - logf (LOG_DEBUG, "nmem print list"); + yaz_log (LOG_DEBUG, "nmem print list"); NMEM_ENTER; for (p = nmem_debug_list; p; p = p->next) - logf (LOG_DEBUG, " %s:%d p=%p", p->file, p->line, p->p); + yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p, + nmem_total(p->p)); NMEM_LEAVE; } #endif @@ -146,7 +171,7 @@ static nmem_block *get_block(int size) nmem_block *r, *l; #if NMEM_DEBUG - logf (LOG_DEBUG, "nmem get_block size=%d", size); + yaz_log (LOG_DEBUG, "nmem get_block size=%d", size); #endif for (r = freelist, l = 0; r; l = r, r = r->next) if (r->size >= size) @@ -154,7 +179,7 @@ static nmem_block *get_block(int size) if (r) { #if NMEM_DEBUG - logf (LOG_DEBUG, "nmem get_block found free block p=%p", r); + yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r); #endif if (l) l->next = r->next; @@ -168,7 +193,7 @@ static nmem_block *get_block(int size) if (get < size) get = size; #if NMEM_DEBUG - logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get); + yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get); #endif r = (nmem_block *)xmalloc(sizeof(*r)); r->buf = (char *)xmalloc(r->size = get); @@ -182,7 +207,7 @@ void nmem_reset(NMEM n) nmem_block *t; #if NMEM_DEBUG - logf (LOG_DEBUG, "nmem_reset p=%p", n); + yaz_log (LOG_DEBUG, "nmem_reset p=%p", n); #endif if (!n) return; @@ -207,7 +232,7 @@ void *nmem_malloc(NMEM n, int size) char *r; #if NMEM_DEBUG - logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line, + yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line, n, size); #endif if (!n) @@ -247,7 +272,7 @@ NMEM nmem_create(void) { NMEM r; #if NMEM_DEBUG - struct nmem_debug *debug_p; + struct nmem_debug_info *debug_p; #endif NMEM_ENTER; @@ -260,7 +285,7 @@ NMEM nmem_create(void) NMEM_LEAVE; #if NMEM_DEBUG - logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line, + yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line, nmem_active_no, r); #endif r->blocks = 0; @@ -271,7 +296,7 @@ NMEM nmem_create(void) for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next) if (debug_p->p == r) { - logf (LOG_FATAL, "multi used block in nmem"); + yaz_log (LOG_FATAL, "multi used block in nmem"); abort (); } debug_p = xmalloc (sizeof(*debug_p)); @@ -294,20 +319,20 @@ void nmem_destroy(NMEM n) #endif { #if NMEM_DEBUG - struct nmem_debug **debug_p; + struct nmem_debug_info **debug_p; int ok = 0; #endif if (!n) return; #if NMEM_DEBUG - logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line, + yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line, nmem_active_no-1, n); NMEM_ENTER; for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next) if ((*debug_p)->p == n) { - struct nmem_debug *debug_save = *debug_p; + struct nmem_debug_info *debug_save = *debug_p; *debug_p = (*debug_p)->next; xfree (debug_save); ok = 1; @@ -317,8 +342,8 @@ void nmem_destroy(NMEM n) nmem_print_list(); if (!ok) { - logf (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p", - file, line, n); + yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p", + file, line, n); return; } #endif @@ -355,32 +380,37 @@ void nmem_critical_leave (void) void nmem_init (void) { - nmem_init_flag = 1; + if (++nmem_init_flag == 1) + { #ifdef WIN32 - InitializeCriticalSection(&critical_section); + InitializeCriticalSection(&critical_section); #endif - nmem_active_no = 0; - freelist = NULL; - cfreelist = NULL; + nmem_active_no = 0; + freelist = NULL; + cfreelist = NULL; + } } void nmem_exit (void) { - while (freelist) + if (--nmem_init_flag == 0) { - struct nmem_block *fl = freelist; - freelist = freelist->next; - xfree (fl->buf); - xfree (fl); - } - while (cfreelist) - { - struct nmem_control *cfl = cfreelist; - cfreelist = cfreelist->next; - xfree (cfl); - } + while (freelist) + { + struct nmem_block *fl = freelist; + freelist = freelist->next; + xfree (fl->buf); + xfree (fl); + } + while (cfreelist) + { + struct nmem_control *cfl = cfreelist; + cfreelist = cfreelist->next; + xfree (cfl); + } #ifdef WIN32 - DeleteCriticalSection(&critical_section); + DeleteCriticalSection(&critical_section); #endif + } }