/*
- * Copyright (c) 1995-2004, Index Data.
+ * Copyright (C) 1995-2005, Index Data ApS
* See the file LICENSE for details.
*
- * $Id: nmem.c,v 1.5 2004-11-03 22:33:17 adam Exp $
+ * $Id: nmem.c,v 1.15 2005-04-29 10:36:05 heikki Exp $
*/
/**
*
* This is a simple and fairly wasteful little module for nibble memory
* allocation. Evemtually we'll put in something better.
+ *
+ * FIXME - it also has some semaphore stuff, and stuff to handle errno.
+ * These should be moved to some other place!
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <assert.h>
+#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <stddef.h>
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
#include <yaz/log.h>
#define NMEM_CHUNK (4*1024)
+struct align {
+ char x;
+ union {
+ char c;
+ short s;
+ int i;
+ long l;
+#if HAVE_LONG_LONG
+ long long ll;
+#endif
+ float f;
+ double d;
+ } u;
+};
+
+#define NMEM_ALIGN (offsetof(struct align, u))
+
+static int log_level = 0;
+static int log_level_initialized = 0;
+
#ifdef WIN32
static CRITICAL_SECTION critical_section;
#define NMEM_ENTER EnterCriticalSection(&critical_section)
YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
{
- NMEM_ENTER;
if (!*p)
{
*p = (NMEM_MUTEX) malloc (sizeof(**p));
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)
YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
{
- NMEM_ENTER;
if (*p)
{
#ifdef WIN32
free (*p);
*p = 0;
}
- NMEM_LEAVE;
}
static nmem_block *freelist = NULL; /* "global" freelists */
memset(p->buf, 'Y', p->size);
p->next = freelist;
freelist = p;
-#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
-#endif
+ if (log_level)
+ yaz_log (log_level, "nmem free_block p=%p", p);
}
#if NMEM_DEBUG
void nmem_print_list (void)
{
- nmem_print_list_l(LOG_DEBUG);
+ if(log_level)
+ nmem_print_list_l(log_level);
}
void nmem_print_list_l (int level)
{
nmem_block *r, *l;
-#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
-#endif
+ if (log_level)
+ yaz_log (log_level, "nmem get_block size=%d", size);
+
for (r = freelist, l = 0; r; l = r, r = r->next)
if (r->size >= size)
break;
if (r)
{
-#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
-#endif
+ if (log_level)
+ yaz_log (log_level, "nmem get_block found free block p=%p", r);
if (l)
l->next = r->next;
else
if (get < size)
get = size;
-#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
-#endif
+ if(log_level)
+ yaz_log (log_level, "nmem get_block alloc new block size=%d", get);
+
r = (nmem_block *)xmalloc(sizeof(*r));
r->buf = (char *)xmalloc(r->size = get);
}
void nmem_reset(NMEM n)
{
nmem_block *t;
-
-#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
-#endif
+
+ yaz_log (log_level, "nmem_reset p=%p", n);
if (!n)
return;
NMEM_ENTER;
char *r;
#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
- n, size);
+ if (log_level)
+ yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d",
+ file, line, n, size);
#endif
if (!n)
{
- yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
+ yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
abort ();
}
#ifdef WIN32
}
r = p->buf + p->top;
/* align size */
- p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
+ p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
n->total += size;
NMEM_LEAVE;
return r;
#if NMEM_DEBUG
struct nmem_debug_info *debug_p;
#endif
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("nmem");
+ log_level_initialized = 1;
+ }
NMEM_ENTER;
nmem_active_no++;
NMEM_LEAVE;
#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
+ yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
nmem_active_no, r);
#endif
r->blocks = 0;
for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
if (debug_p->p == r)
{
- yaz_log (LOG_FATAL, "multi used block in nmem");
+ yaz_log (YLOG_FATAL, "multi used block in nmem");
abort ();
}
debug_p = xmalloc (sizeof(*debug_p));
return;
#if NMEM_DEBUG
- yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
+ yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line,
nmem_active_no-1, n);
NMEM_ENTER;
for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
nmem_print_list();
if (!ok)
{
- yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
+ yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
file, line, n);
return;
}
void nmem_init (void)
{
+
if (++nmem_init_flag == 1)
{
#ifdef WIN32
InitializeCriticalSection(&critical_section);
#elif YAZ_GNU_THREADS
- yaz_log (LOG_LOG, "pth_init");
pth_init ();
#endif
nmem_active_no = 0;
freelist = NULL;
cfreelist = NULL;
}
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("nmem");
+ log_level_initialized = 1;
+ }
}
void nmem_exit (void)
void yaz_strerror(char *buf, int max)
{
+#ifdef WIN32
+ DWORD err;
+#endif
char *cp;
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("nmem");
+ log_level_initialized = 1;
+ }
+
#ifdef WIN32
- DWORD err = GetLastError();
+ err = GetLastError();
if (err)
{
FormatMessage(