X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=blobdiff_plain;f=util%2Fnmem.c;h=8415772319d2488afaf0dfff47cd7cebfa60f7fa;hp=418962ab3ae98bd05d42f84b14bb5216138c4429;hb=4d531a1a9131d69c3b6c27fbac42837e22cff61c;hpb=9fc822fe67ef4f5a898d9a85fbae7dd9a3435023 diff --git a/util/nmem.c b/util/nmem.c index 418962a..8415772 100644 --- a/util/nmem.c +++ b/util/nmem.c @@ -1,88 +1,163 @@ /* - * Copyright (c) 1995-1998, Index Data. + * Copyright (c) 1995-2003, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * - * $Log: nmem.c,v $ - * Revision 1.10 1998-07-20 12:35:57 adam - * Added more memory diagnostics (when NMEM_DEBUG is 1). - * - * Revision 1.9 1998/07/07 15:49:01 adam - * Reduced chunk size. - * - * Revision 1.8 1998/07/03 14:21:27 adam - * Added critical sections for pthreads-library. Thanks to Ian Ibbotson, - * Fretwell Downing Informatics. - * - * Revision 1.7 1998/02/11 11:53:36 adam - * Changed code so that it compiles as C++. - * - * Revision 1.6 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.5 1997/10/06 09:09:52 adam - * Function mmem_exit releases memory used by the freelists. - * - * Revision 1.4 1997/09/29 07:12:50 adam - * NMEM thread safe. NMEM must be initialized before use (sigh) - - * routine nmem_init/nmem_exit implemented. - * - * Revision 1.3 1997/07/21 12:47:38 adam - * Moved definition of nmem_control and nmem_block. - * - * Revision 1.2 1995/12/13 13:44:37 quinn - * Modified Data1-system to use nmem - * - * Revision 1.1 1995/11/13 09:27:52 quinn - * Fiddling with the variant stuff. - * - * + * $Id: nmem.c,v 1.39 2003-01-06 08:20:28 adam Exp $ */ /* * 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 -#ifdef WINDOWS +#include +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 #include -#elif _REENTRANT +#endif + +#if YAZ_POSIX_THREADS #include #endif +#if YAZ_GNU_THREADS +#include +#endif + #define NMEM_CHUNK (4*1024) -#ifdef WINDOWS +#ifdef WIN32 static CRITICAL_SECTION critical_section; #define NMEM_ENTER EnterCriticalSection(&critical_section) #define NMEM_LEAVE LeaveCriticalSection(&critical_section) -#elif _REENTRANT -static pthread_mutex_t nmem_mutex; +struct nmem_mutex { + CRITICAL_SECTION m_handle; +}; +#elif YAZ_POSIX_THREADS +static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER; #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex); #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex); +struct nmem_mutex { + pthread_mutex_t m_handle; +}; +#elif YAZ_GNU_THREADS +static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT; +#define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0) +#define NMEM_LEAVE pth_mutex_release(&nmem_mutex) +struct nmem_mutex { + pth_mutex_t m_handle; +}; #else #define NMEM_ENTER #define NMEM_LEAVE +struct nmem_mutex { + int dummy; +}; +#endif + +YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p) +{ + NMEM_ENTER; + if (!*p) + { + *p = (NMEM_MUTEX) malloc (sizeof(**p)); +#ifdef WIN32 + InitializeCriticalSection(&(*p)->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_init (&(*p)->m_handle, 0); +#elif YAZ_GNU_THREADS + pth_mutex_init (&(*p)->m_handle); +#endif + } + NMEM_LEAVE; +} + +YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p) +{ + if (p) + { +#ifdef WIN32 + EnterCriticalSection(&p->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_lock(&p->m_handle); #endif + } +} + +YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p) +{ + if (p) + { +#ifdef WIN32 + LeaveCriticalSection(&p->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_unlock(&p->m_handle); +#endif + } +} + +YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p) +{ + NMEM_ENTER; + if (*p) + { +#ifdef WIN32 + DeleteCriticalSection(&(*p)->m_handle); +#endif + free (*p); + *p = 0; + } + NMEM_LEAVE; +} static nmem_block *freelist = NULL; /* "global" freelists */ static nmem_control *cfreelist = NULL; static int nmem_active_no = 0; +static int nmem_init_flag = 0; + +#if NMEM_DEBUG +struct nmem_debug_info { + void *p; + char file[40]; + int line; + struct nmem_debug_info *next; +}; + +struct nmem_debug_info *nmem_debug_list = 0; +#endif 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_info *p; + + yaz_log (LOG_DEBUG, "nmem print list"); + NMEM_ENTER; + for (p = nmem_debug_list; p; p = p->next) + yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p, + nmem_total(p->p)); + NMEM_LEAVE; +} +#endif /* * acquire a block with a minimum of size free bytes. */ @@ -91,7 +166,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) @@ -99,7 +174,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; @@ -113,7 +188,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); @@ -127,7 +202,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; @@ -138,8 +213,8 @@ void nmem_reset(NMEM n) n->blocks = n->blocks->next; free_block(t); } - NMEM_LEAVE; n->total = 0; + NMEM_LEAVE; } #if NMEM_DEBUG @@ -152,11 +227,17 @@ 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) - return xmalloc(size); + { + yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer"); + abort (); + } +#ifdef WIN32 + assert (nmem_init_flag); +#endif NMEM_ENTER; p = n->blocks; if (!p || p->size - p->top < size) @@ -185,6 +266,9 @@ NMEM nmem_create(void) #endif { NMEM r; +#if NMEM_DEBUG + struct nmem_debug_info *debug_p; +#endif NMEM_ENTER; nmem_active_no++; @@ -196,12 +280,30 @@ NMEM nmem_create(void) NMEM_LEAVE; #if NMEM_DEBUG - logf (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line, - nmem_active_no-1, r); + yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line, + nmem_active_no, r); #endif r->blocks = 0; r->total = 0; r->next = 0; + +#if NMEM_DEBUG + for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next) + if (debug_p->p == r) + { + yaz_log (LOG_FATAL, "multi used block in nmem"); + abort (); + } + debug_p = xmalloc (sizeof(*debug_p)); + strncpy (debug_p->file, file, sizeof(debug_p->file)-1); + debug_p->file[sizeof(debug_p->file)-1] = '\0'; + debug_p->line = line; + debug_p->p = r; + debug_p->next = nmem_debug_list; + nmem_debug_list = debug_p; + + nmem_print_list(); +#endif return r; } @@ -211,47 +313,161 @@ void nmem_destroy_f(const char *file, int line, NMEM n) void nmem_destroy(NMEM n) #endif { +#if NMEM_DEBUG + struct nmem_debug_info **debug_p; + int ok = 0; +#endif if (!n) return; + +#if NMEM_DEBUG + 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_info *debug_save = *debug_p; + *debug_p = (*debug_p)->next; + xfree (debug_save); + ok = 1; + break; + } + NMEM_LEAVE; + nmem_print_list(); + if (!ok) + { + yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p", + file, line, n); + return; + } +#endif nmem_reset(n); NMEM_ENTER; nmem_active_no--; n->next = cfreelist; cfreelist = n; NMEM_LEAVE; -#if NMEM_DEBUG - logf (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line, - nmem_active_no, n); -#endif +} + +void nmem_transfer (NMEM dst, NMEM src) +{ + nmem_block *t; + while ((t=src->blocks)) + { + src->blocks = t->next; + t->next = dst->blocks; + dst->blocks = t; + } + dst->total += src->total; + src->total = 0; +} + +void nmem_critical_enter (void) +{ + NMEM_ENTER; +} + +void nmem_critical_leave (void) +{ + NMEM_LEAVE; } void nmem_init (void) { -#ifdef WINDOWS - InitializeCriticalSection(&critical_section); + if (++nmem_init_flag == 1) + { +#ifdef WIN32 + InitializeCriticalSection(&critical_section); +#elif YAZ_GNU_THREADS + yaz_log (LOG_LOG, "pth_init"); + pth_init (); #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); + oid_exit(); + 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); +#endif } - while (cfreelist) +} + + +#ifdef WIN32 +BOOL WINAPI DllMain (HINSTANCE hinstDLL, + DWORD reason, + LPVOID reserved) +{ + switch (reason) { - struct nmem_control *cfl = cfreelist; - cfreelist = cfreelist->next; - xfree (cfl); + case DLL_PROCESS_ATTACH: + nmem_init (); + break; + case DLL_PROCESS_DETACH: + nmem_exit (); } -#ifdef WINDOWS - DeleteCriticalSection(&critical_section); + return TRUE; +} #endif + +int yaz_errno(void) +{ + return errno; } +void yaz_set_errno(int v) +{ + errno = v; +} + +void yaz_strerror(char *buf, int max) +{ + char *cp; +#ifdef WIN32 + DWORD err = GetLastError(); + if (err) + { + FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + err, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) buf, + max-1, + NULL); + } + else + *buf = '\0'; +#else +#if HAVE_STRERROR_R + strerror_r(errno, buf, max); +#else + strcpy(buf, strerror(yaz_errno())); +#endif +#endif + if ((cp=strrchr(buf, '\n'))) + *cp = '\0'; + if ((cp=strrchr(buf, '\r'))) + *cp = '\0'; +}