/*
- * Copyright (c) 1995-2003, Index Data.
+ * Copyright (c) 1995-2004, Index Data.
* See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
*
- * $Id: nmem.c,v 1.1 2003-10-27 12:21:32 adam Exp $
+ * $Id: nmem.c,v 1.6 2004-11-18 15:18:13 heikki Exp $
*/
-/*
+/**
+ * \file nmem.c
+ * \brief Implements Nibble Memory
+ *
* 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>
#include <errno.h>
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
-#include <yaz/log.h>
+#include <yaz/ylog.h>
#include <yaz/oid.h>
#ifdef WIN32
#define NMEM_CHUNK (4*1024)
+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)
{
+ if (!log_level_initialized)
+ {
+ log_level=yaz_log_module_level("nmem");
+ log_level_initialized=1;
+ }
+
NMEM_ENTER;
if (!*p)
{
static void free_block(nmem_block *p)
{
+ 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)
{
+ if(log_level)
+ nmem_print_list_l(log_level);
+}
+
+void nmem_print_list_l (int level)
+{
struct nmem_debug_info *p;
- yaz_log (LOG_DEBUG, "nmem print list");
+ yaz_log (level, "nmem print list");
NMEM_ENTER;
for (p = nmem_debug_list; p; p = p->next)
- yaz_log (LOG_DEBUG, " %s:%d p=%p size=%d", p->file, p->line, p->p,
+ yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
nmem_total(p->p));
NMEM_LEAVE;
}
{
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
#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_transfer (NMEM dst, NMEM src)
{
nmem_block *t;
- while ((t=src->blocks))
+ while ((t = src->blocks))
{
src->blocks = t->next;
t->next = dst->blocks;
void nmem_init (void)
{
+ if (!log_level_initialized)
+ {
+ log_level=yaz_log_module_level("nmem");
+ log_level_initialized=1;
+ }
+
if (++nmem_init_flag == 1)
{
#ifdef WIN32
InitializeCriticalSection(&critical_section);
#elif YAZ_GNU_THREADS
- yaz_log (LOG_LOG, "pth_init");
+ yaz_log (log_level, "pth_init"); /* ??? */
pth_init ();
#endif
nmem_active_no = 0;
void yaz_strerror(char *buf, int max)
{
char *cp;
+ if (!log_level_initialized)
+ {
+ log_level=yaz_log_module_level("nmem");
+ log_level_initialized=1;
+ }
+
#ifdef WIN32
DWORD err = GetLastError();
if (err)
#endif
/* UNIX */
#endif
- if ((cp=strrchr(buf, '\n')))
+ if ((cp = strrchr(buf, '\n')))
*cp = '\0';
- if ((cp=strrchr(buf, '\r')))
+ if ((cp = strrchr(buf, '\r')))
*cp = '\0';
}