1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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
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);
86 yaz_log(log_level, "nmem get_block alloc new block size=%ld",
89 r = (struct nmem_block *) xmalloc(sizeof(*r));
90 r->buf = (char *)xmalloc(r->size = get);
95 void nmem_reset(NMEM n)
99 yaz_log(log_level, "nmem_reset p=%p", n);
105 n->blocks = n->blocks->next;
111 void *nmem_malloc(NMEM n, size_t size)
113 struct nmem_block *p;
118 yaz_log(YLOG_FATAL, "calling nmem_malloc with an null pointer");
122 if (!p || p->size < size + p->top)
130 p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1);
135 size_t nmem_total(NMEM n)
140 NMEM nmem_create(void)
143 if (!log_level_initialized)
145 log_level = yaz_log_module_level("nmem");
146 log_level_initialized = 1;
149 r = (struct nmem_control *)xmalloc(sizeof(*r));
158 void nmem_destroy(NMEM n)
167 void nmem_transfer(NMEM dst, NMEM src)
169 struct nmem_block *t;
170 while ((t = src->blocks))
172 src->blocks = t->next;
173 t->next = dst->blocks;
176 dst->total += src->total;
183 * c-file-style: "Stroustrup"
184 * indent-tabs-mode: nil
186 * vim: shiftwidth=4 tabstop=8 expandtab