projects
/
yaz-moved-to-github.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Ignore generated file tstlog
[yaz-moved-to-github.git]
/
src
/
nmem.c
diff --git
a/src/nmem.c
b/src/nmem.c
index
35426da
..
947a492
100644
(file)
--- a/
src/nmem.c
+++ b/
src/nmem.c
@@
-1,14
+1,19
@@
/*
/*
- * Copyright (c) 1995-2003, Index Data.
+ * Copyright (c) 1995-2004, Index Data.
* See the file LICENSE for details.
* See the file LICENSE for details.
- * Sebastian Hammer, Adam Dickmeiss
*
*
- * $Id: nmem.c,v 1.3 2004-01-15 10:16:27 adam Exp $
+ * $Id: nmem.c,v 1.10 2005-01-04 21:27:54 mike 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.
* 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>
*/
#if HAVE_CONFIG_H
#include <config.h>
@@
-17,6
+22,7
@@
#include <assert.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <string.h>
#include <errno.h>
+#include <stddef.h>
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
#include <yaz/log.h>
#include <yaz/xmalloc.h>
#include <yaz/nmem.h>
#include <yaz/log.h>
@@
-36,6
+42,24
@@
#define NMEM_CHUNK (4*1024)
#define NMEM_CHUNK (4*1024)
+struct align {
+ char x;
+ union {
+ char c;
+ short s;
+ int i;
+ long l;
+ long long ll;
+ 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)
#ifdef WIN32
static CRITICAL_SECTION critical_section;
#define NMEM_ENTER EnterCriticalSection(&critical_section)
@@
-67,6
+91,12
@@
struct nmem_mutex {
YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
{
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)
{
NMEM_ENTER;
if (!*p)
{
@@
-141,15
+171,15
@@
static void free_block(nmem_block *p)
memset(p->buf, 'Y', p->size);
p->next = freelist;
freelist = 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 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)
}
void nmem_print_list_l (int level)
@@
-171,17
+201,16
@@
static nmem_block *get_block(int size)
{
nmem_block *r, *l;
{
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)
{
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 (l)
l->next = r->next;
else
@@
-193,9
+222,9
@@
static nmem_block *get_block(int size)
if (get < size)
get = size;
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);
}
r = (nmem_block *)xmalloc(sizeof(*r));
r->buf = (char *)xmalloc(r->size = get);
}
@@
-206,10
+235,8
@@
static nmem_block *get_block(int size)
void nmem_reset(NMEM n)
{
nmem_block *t;
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;
if (!n)
return;
NMEM_ENTER;
@@
-233,12
+260,13
@@
void *nmem_malloc(NMEM n, int size)
char *r;
#if NMEM_DEBUG
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)
{
#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
abort ();
}
#ifdef WIN32
@@
-254,7
+282,7
@@
void *nmem_malloc(NMEM n, int size)
}
r = p->buf + p->top;
/* align size */
}
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;
n->total += size;
NMEM_LEAVE;
return r;
@@
-275,6
+303,11
@@
NMEM nmem_create(void)
#if NMEM_DEBUG
struct nmem_debug_info *debug_p;
#endif
#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_ENTER;
nmem_active_no++;
@@
-286,7
+319,7
@@
NMEM nmem_create(void)
NMEM_LEAVE;
#if NMEM_DEBUG
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;
nmem_active_no, r);
#endif
r->blocks = 0;
@@
-297,7
+330,7
@@
NMEM nmem_create(void)
for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
if (debug_p->p == r)
{
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));
abort ();
}
debug_p = xmalloc (sizeof(*debug_p));
@@
-327,7
+360,7
@@
void nmem_destroy(NMEM n)
return;
#if NMEM_DEBUG
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_active_no-1, n);
NMEM_ENTER;
for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
@@
-343,7
+376,7
@@
void nmem_destroy(NMEM n)
nmem_print_list();
if (!ok)
{
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;
}
file, line, n);
return;
}
@@
-359,7
+392,7
@@
void nmem_destroy(NMEM n)
void nmem_transfer (NMEM dst, NMEM src)
{
nmem_block *t;
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;
{
src->blocks = t->next;
t->next = dst->blocks;
@@
-381,12
+414,18
@@
void nmem_critical_leave (void)
void nmem_init (void)
{
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
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;
pth_init ();
#endif
nmem_active_no = 0;
@@
-449,9
+488,18
@@
void yaz_set_errno(int v)
void yaz_strerror(char *buf, int max)
{
void yaz_strerror(char *buf, int max)
{
+#ifdef WIN32
+ DWORD err;
+#endif
char *cp;
char *cp;
+ if (!log_level_initialized)
+ {
+ log_level = yaz_log_module_level("nmem");
+ log_level_initialized = 1;
+ }
+
#ifdef WIN32
#ifdef WIN32
- DWORD err = GetLastError();
+ err = GetLastError();
if (err)
{
FormatMessage(
if (err)
{
FormatMessage(
@@
-482,8
+530,8
@@
void yaz_strerror(char *buf, int max)
#endif
/* UNIX */
#endif
#endif
/* UNIX */
#endif
- if ((cp=strrchr(buf, '\n')))
+ if ((cp = strrchr(buf, '\n')))
*cp = '\0';
*cp = '\0';
- if ((cp=strrchr(buf, '\r')))
+ if ((cp = strrchr(buf, '\r')))
*cp = '\0';
}
*cp = '\0';
}