/*
- * Copyright (c) 1995-1998, Index Data.
+ * Copyright (c) 1995-1999, Index Data.
* See the file LICENSE for details.
* Sebastian Hammer, Adam Dickmeiss
*
* $Log: nmem.c,v $
- * Revision 1.9 1998-07-07 15:49:01 adam
+ * 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
* allocation. Evemtually we'll put in something better.
*/
+#include <assert.h>
#include <xmalloc.h>
#include <nmem.h>
#include <log.h>
-#ifdef WINDOWS
+#ifdef WIN32
#include <windows.h>
#elif _REENTRANT
+
+#if HAVE_PTHREAD_H
#include <pthread.h>
+#elif HAVE_THREAD_H
+#include <thread.h>
+#endif
+
#endif
#define NMEM_CHUNK (4*1024)
-#ifdef WINDOWS
+#ifdef WIN32
static CRITICAL_SECTION critical_section;
#define NMEM_ENTER EnterCriticalSection(&critical_section)
#define NMEM_LEAVE LeaveCriticalSection(&critical_section)
#elif _REENTRANT
-static pthread_mutex_t nmem_mutex;
+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);
#else
static nmem_block *freelist = NULL; /* "global" freelists */
static nmem_control *cfreelist = NULL;
static int nmem_active_no = 0;
+static int nmem_init_flag = 0;
static void free_block(nmem_block *p)
{
p->next = freelist;
freelist = p;
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "nmem free_block p=%p", p);
+#endif
}
/*
{
nmem_block *r, *l;
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "nmem get_block size=%d", size);
+#endif
for (r = freelist, l = 0; r; l = r, r = r->next)
if (r->size >= size)
break;
if (r)
{
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "nmem get_block found free block p=%p", r);
+#endif
if (l)
l->next = r->next;
else
if (get < size)
get = size;
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
+#endif
r = (nmem_block *)xmalloc(sizeof(*r));
r->buf = (char *)xmalloc(r->size = get);
}
{
nmem_block *t;
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "nmem_reset p=%p", n);
+#endif
if (!n)
return;
NMEM_ENTER;
n->blocks = n->blocks->next;
free_block(t);
}
- NMEM_LEAVE;
n->total = 0;
+ NMEM_LEAVE;
}
+#if NMEM_DEBUG
+void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
+#else
void *nmem_malloc(NMEM n, int size)
+#endif
{
struct nmem_block *p;
char *r;
+#if NMEM_DEBUG
+ logf (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
+ n, size);
+#endif
if (!n)
+ {
+ abort ();
return xmalloc(size);
+ }
+#ifdef WIN32
+ assert (nmem_init_flag);
+#endif
NMEM_ENTER;
p = n->blocks;
if (!p || p->size - p->top < size)
#endif
}
+void nmem_transfer (NMEM dst, NMEM src)
+{
+ nmem_block *t;
+ while ((t=src->blocks))
+ {
+ src->blocks = t->next;
+ t->next = dst->blocks;
+ dst->blocks = t;
+ }
+ dst->total += src->total;
+ src->total = 0;
+}
+
+void nmem_critical_enter (void)
+{
+ NMEM_ENTER;
+}
+
+void nmem_critical_leave (void)
+{
+ NMEM_LEAVE;
+}
+
void nmem_init (void)
{
-#ifdef WINDOWS
+ nmem_init_flag = 1;
+#ifdef WIN32
InitializeCriticalSection(&critical_section);
#endif
nmem_active_no = 0;
cfreelist = cfreelist->next;
xfree (cfl);
}
-#ifdef WINDOWS
+#ifdef WIN32
DeleteCriticalSection(&critical_section);
#endif
}