1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements Nibble Memory
10 * This is a simple and fairly wasteful little module for nibble memory
11 * allocation. Evemtually we'll put in something better.
23 #include <yaz/xmalloc.h>
27 #define NMEM_CHUNK (4*1024)
31 char *buf; /* memory allocated in this block */
32 size_t size; /* size of buf */
33 size_t top; /* top of buffer */
34 struct nmem_block *next;
40 struct nmem_block *blocks;
41 struct nmem_control *next;
59 #define NMEM_ALIGN (offsetof(struct align, u))
61 static int log_level = 0;
62 static int log_level_initialized = 0;
64 static void free_block(struct nmem_block *p)
69 yaz_log (log_level, "nmem free_block p=%p", p);
73 * acquire a block with a minimum of size free bytes.
75 static struct nmem_block *get_block(size_t size)
78 size_t get = NMEM_CHUNK;
81 yaz_log (log_level, "nmem get_block size=%ld", (long) size);
87 yaz_log (log_level, "nmem get_block alloc new block size=%ld",
90 r = (struct nmem_block *) xmalloc(sizeof(*r));
91 r->buf = (char *)xmalloc(r->size = get);
96 void nmem_reset(NMEM n)
100 yaz_log (log_level, "nmem_reset p=%p", n);
106 n->blocks = n->blocks->next;
112 void *nmem_malloc(NMEM n, size_t size)
114 struct nmem_block *p;
119 yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer");
123 if (!p || p->size < size + p->top)
131 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
136 size_t nmem_total(NMEM n)
141 NMEM nmem_create(void)
144 if (!log_level_initialized)
146 log_level = yaz_log_module_level("nmem");
147 log_level_initialized = 1;
150 r = (struct nmem_control *)xmalloc(sizeof(*r));
159 void nmem_destroy(NMEM n)
168 void nmem_transfer (NMEM dst, NMEM src)
170 struct nmem_block *t;
171 while ((t = src->blocks))
173 src->blocks = t->next;
174 t->next = dst->blocks;
177 dst->total += src->total;
184 * c-file-style: "Stroustrup"
185 * indent-tabs-mode: nil
187 * vim: shiftwidth=4 tabstop=8 expandtab