/*
- * Copyright (c) 1995-2000, Index Data.
+ * Copyright (c) 1995-2003, 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
- * Public nmem_print_list (for debugging).
- *
- * Revision 1.22 2000/05/03 22:00:00 adam
- * Reference counter (if multiple modules are init/freeing nmem).
- *
- * Revision 1.21 2000/02/29 13:44:55 adam
- * Check for config.h (currently not generated).
- *
- * Revision 1.20 2000/01/06 14:59:13 adam
- * Added oid_init/oid_exit. Changed oid_exit.
- *
- * Revision 1.19 1999/11/30 13:47:12 adam
- * Improved installation. Moved header files to include/yaz.
- *
- * Revision 1.18 1999/08/27 09:40:32 adam
- * Renamed logf function to yaz_log. Removed VC++ project files.
- *
- * Revision 1.17 1999/07/13 13:28:25 adam
- * Better debugging for NMEM routines.
- *
- * Revision 1.16 1999/03/31 11:18:25 adam
- * Implemented odr_strdup. Added Reference ID to backend server API.
- *
- * Revision 1.15 1999/02/11 09:10:26 adam
- * Function nmem_init only mandatory on Windows.
- *
- * Revision 1.14 1999/02/02 13:57:40 adam
- * Uses preprocessor define WIN32 instead of WINDOWS to build code
- * for Microsoft WIN32.
- *
- * Revision 1.13 1998/10/19 15:24:21 adam
- * New nmem utility, nmem_transfer, that transfer blocks from one
- * NMEM to another.
- *
- * Revision 1.12 1998/10/13 16:00:18 adam
- * Implemented nmem_critical_{enter,leave}.
- *
- * Revision 1.11 1998/08/21 14:13:36 adam
- * Added GNU Configure script to build Makefiles.
- *
- * 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.41 2003-02-22 00:08:44 adam Exp $
*/
/*
#include <assert.h>
#include <string.h>
+#include <errno.h>
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
#include <yaz/log.h>
+#include <yaz/oid.h>
+
#ifdef WIN32
#include <windows.h>
-#elif _REENTRANT
+#endif
-#if HAVE_PTHREAD_H
+#if YAZ_POSIX_THREADS
#include <pthread.h>
-#elif HAVE_THREAD_H
-#include <thread.h>
#endif
+#if YAZ_GNU_THREADS
+#include <pth.h>
#endif
#define NMEM_CHUNK (4*1024)
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;
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;
}
#endif
if (!n)
{
+ yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
abort ();
- return xmalloc(size);
}
#ifdef WIN32
assert (nmem_init_flag);
{
#ifdef WIN32
InitializeCriticalSection(&critical_section);
+#elif YAZ_GNU_THREADS
+ yaz_log (LOG_LOG, "pth_init");
+ pth_init ();
#endif
nmem_active_no = 0;
freelist = NULL;
{
if (--nmem_init_flag == 0)
{
+ oid_exit();
while (freelist)
{
struct nmem_block *fl = freelist;
}
}
+
+#ifdef WIN32
+BOOL WINAPI DllMain (HINSTANCE hinstDLL,
+ DWORD reason,
+ LPVOID reserved)
+{
+ switch (reason)
+ {
+ case DLL_PROCESS_ATTACH:
+ nmem_init ();
+ break;
+ case DLL_PROCESS_DETACH:
+ nmem_exit ();
+ }
+ 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
+/* UNIX */
+#if HAVE_STRERROR_R
+#if YAZ_POSIX_THREADS
+ *buf = '\0';
+ strerror_r(errno, buf, max);
+ /* if buffer is unset - use strerror anyway (GLIBC bug) */
+ if (*buf == '\0')
+ strcpy(buf, strerror(yaz_errno()));
+#else
+ strcpy(buf, strerror(yaz_errno()));
+#endif
+#else
+ strcpy(buf, strerror(yaz_errno()));
+#endif
+/* UNIX */
+#endif
+ if ((cp=strrchr(buf, '\n')))
+ *cp = '\0';
+ if ((cp=strrchr(buf, '\r')))
+ *cp = '\0';
+}