+ 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)
+{