X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fnmem.c;h=131ab1e739103ed4e05bff37771b7656a4f02fd4;hb=9e1594c51c65fa993e9d69928c0ecabee8c2b862;hp=beaac98fa19385db6eed088a09bde28f98f4abfb;hpb=588af2e484a5a48afac3069578959437c9b3271e;p=yaz-moved-to-github.git diff --git a/util/nmem.c b/util/nmem.c index beaac98..131ab1e 100644 --- a/util/nmem.c +++ b/util/nmem.c @@ -1,10 +1,31 @@ /* - * Copyright (c) 1995-2000, Index Data. + * Copyright (c) 1995-2001, Index Data. * See the file LICENSE for details. * Sebastian Hammer, Adam Dickmeiss * * $Log: nmem.c,v $ - * Revision 1.23 2000-05-09 10:55:05 adam + * Revision 1.30 2001-10-05 13:55:17 adam + * Added defines YAZ_GNU_THREADS, YAZ_POSIX_THREADS in code and yaz-config + * + * Revision 1.29 2001/10/04 00:37:58 adam + * Fixes for GNU threads (not working yet). + * + * Revision 1.28 2001/10/03 23:55:18 adam + * GNU threads support. + * + * Revision 1.27 2001/09/27 12:09:18 adam + * Function nmem_exit calls oid_exit (when reference is 0). + * + * Revision 1.26 2001/07/19 19:51:42 adam + * Added typecasts to make C++ happy. + * + * Revision 1.25 2001/06/26 14:11:27 adam + * Added MUTEX functions for NMEM module (used by OID utility). + * + * 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 @@ -95,16 +116,18 @@ #include #include #include +#include + #ifdef WIN32 #include -#elif _REENTRANT +#endif -#if HAVE_PTHREAD_H +#if YAZ_POSIX_THREADS #include -#elif HAVE_THREAD_H -#include #endif +#if YAZ_GNU_THREADS +#include #endif #define NMEM_CHUNK (4*1024) @@ -113,15 +136,86 @@ static CRITICAL_SECTION critical_section; #define NMEM_ENTER EnterCriticalSection(&critical_section) #define NMEM_LEAVE LeaveCriticalSection(&critical_section) -#elif _REENTRANT +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; @@ -155,7 +249,7 @@ void nmem_print_list (void) yaz_log (LOG_DEBUG, "nmem print list"); NMEM_ENTER; for (p = nmem_debug_list; p; p = p->next) - yaz_log (LOG_LOG, " %s:%d p=%p size=%d", 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; } @@ -381,6 +475,9 @@ void nmem_init (void) { #ifdef WIN32 InitializeCriticalSection(&critical_section); +#elif YAZ_GNU_THREADS + yaz_log (LOG_LOG, "pth_init"); + pth_init (); #endif nmem_active_no = 0; freelist = NULL; @@ -392,6 +489,7 @@ void nmem_exit (void) { if (--nmem_init_flag == 0) { + oid_exit(); while (freelist) { struct nmem_block *fl = freelist;