X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fnmem.c;h=ceb8210b47a7947cc86e2ea2fd93084c3d660749;hb=aa5f63565232666005bef5c83f2d500d8ce9dfb4;hp=9acaaeec3d25695554d1c1fdf4b8c77883e47b7e;hpb=9ed20d6529182e33ebca7423313eba893d07b69e;p=yaz-moved-to-github.git diff --git a/util/nmem.c b/util/nmem.c index 9acaaee..ceb8210 100644 --- a/util/nmem.c +++ b/util/nmem.c @@ -4,7 +4,16 @@ * Sebastian Hammer, Adam Dickmeiss * * $Log: nmem.c,v $ - * Revision 1.24 2000-05-11 14:37:55 adam + * 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 @@ -98,6 +107,7 @@ #include #include #include +#include #ifdef WIN32 #include #elif _REENTRANT @@ -125,6 +135,70 @@ static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER; #define NMEM_LEAVE #endif + +struct nmem_mutex { +#ifdef WIN32 + CRITICAL_SECTION m_handle; +#elif _REENTRANT + pthread_mutex_t m_handle; +#else + int m_handle; +#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 _REENTRANT + pthread_mutex_init (&(*p)->m_handle, 0); +#endif + } + NMEM_LEAVE; +} + +YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p) +{ + if (p) + { +#ifdef WIN32 + EnterCriticalSection(&p->m_handle); +#elif _REENTRANT + 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 _REENTRANT + 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; @@ -395,6 +469,7 @@ void nmem_exit (void) { if (--nmem_init_flag == 0) { + oid_exit(); while (freelist) { struct nmem_block *fl = freelist;