-#ifdef WIN32
-static CRITICAL_SECTION critical_section;
-#define NMEM_ENTER EnterCriticalSection(&critical_section)
-#define NMEM_LEAVE LeaveCriticalSection(&critical_section)
-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
-
-size_t nmem_memory_in_use = 0;
-size_t nmem_memory_free = 0;
-
-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;
- if (!log_level_initialized)
- {
- log_level_initialized = 1;
- log_level = yaz_log_module_level("nmem");
- }
-
-}
-
-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)
-{
- if (*p)
- {
-#ifdef WIN32
- DeleteCriticalSection(&(*p)->m_handle);
-#endif
- free(*p);
- *p = 0;
- }
-}
-
-/** \brief free NMEM memory blocks . Reused in get_block */
-static struct nmem_block *freelist = NULL;
-
-/** \brief free NMEM control blocks. Reused in nmem_create */
-static struct nmem_control *cfreelist = NULL;
-
-/** \brief number NMEM's in use (number of nmem_controls not in free list) */
-static int nmem_active_no = 0;
-
-/** \brief NMEM usage counter */
-static int nmem_init_flag = 0;
-
-/** \brief whether nmem blocks should be reassigned to heap */
-static int nmem_release_in_heap = 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
-