2 * Copyright (c) 1995-2004, Index Data.
3 * See the file LICENSE for details.
5 * $Id: nmem.c,v 1.4 2004-10-15 00:19:00 adam Exp $
10 * \brief Implements Nibble Memory
12 * This is a simple and fairly wasteful little module for nibble memory
13 * allocation. Evemtually we'll put in something better.
22 #include <yaz/xmalloc.h>
39 #define NMEM_CHUNK (4*1024)
42 static CRITICAL_SECTION critical_section;
43 #define NMEM_ENTER EnterCriticalSection(&critical_section)
44 #define NMEM_LEAVE LeaveCriticalSection(&critical_section)
46 CRITICAL_SECTION m_handle;
48 #elif YAZ_POSIX_THREADS
49 static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER;
50 #define NMEM_ENTER pthread_mutex_lock(&nmem_mutex);
51 #define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex);
53 pthread_mutex_t m_handle;
56 static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT;
57 #define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0)
58 #define NMEM_LEAVE pth_mutex_release(&nmem_mutex)
70 YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p)
75 *p = (NMEM_MUTEX) malloc (sizeof(**p));
77 InitializeCriticalSection(&(*p)->m_handle);
78 #elif YAZ_POSIX_THREADS
79 pthread_mutex_init (&(*p)->m_handle, 0);
81 pth_mutex_init (&(*p)->m_handle);
87 YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p)
92 EnterCriticalSection(&p->m_handle);
93 #elif YAZ_POSIX_THREADS
94 pthread_mutex_lock(&p->m_handle);
99 YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p)
104 LeaveCriticalSection(&p->m_handle);
105 #elif YAZ_POSIX_THREADS
106 pthread_mutex_unlock(&p->m_handle);
111 YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p)
117 DeleteCriticalSection(&(*p)->m_handle);
125 static nmem_block *freelist = NULL; /* "global" freelists */
126 static nmem_control *cfreelist = NULL;
127 static int nmem_active_no = 0;
128 static int nmem_init_flag = 0;
131 struct nmem_debug_info {
135 struct nmem_debug_info *next;
138 struct nmem_debug_info *nmem_debug_list = 0;
141 static void free_block(nmem_block *p)
143 memset(p->buf, 'Y', p->size);
147 yaz_log (LOG_DEBUG, "nmem free_block p=%p", p);
152 void nmem_print_list (void)
154 nmem_print_list_l(LOG_DEBUG);
157 void nmem_print_list_l (int level)
159 struct nmem_debug_info *p;
161 yaz_log (level, "nmem print list");
163 for (p = nmem_debug_list; p; p = p->next)
164 yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p,
170 * acquire a block with a minimum of size free bytes.
172 static nmem_block *get_block(int size)
177 yaz_log (LOG_DEBUG, "nmem get_block size=%d", size);
179 for (r = freelist, l = 0; r; l = r, r = r->next)
185 yaz_log (LOG_DEBUG, "nmem get_block found free block p=%p", r);
194 int get = NMEM_CHUNK;
199 yaz_log (LOG_DEBUG, "nmem get_block alloc new block size=%d", get);
201 r = (nmem_block *)xmalloc(sizeof(*r));
202 r->buf = (char *)xmalloc(r->size = get);
208 void nmem_reset(NMEM n)
213 yaz_log (LOG_DEBUG, "nmem_reset p=%p", n);
221 n->blocks = n->blocks->next;
229 void *nmem_malloc_f (const char *file, int line, NMEM n, int size)
231 void *nmem_malloc(NMEM n, int size)
234 struct nmem_block *p;
238 yaz_log (LOG_DEBUG, "%s:%d: nmem_malloc p=%p size=%d", file, line,
243 yaz_log (LOG_FATAL, "calling nmem_malloc with an null pointer");
247 assert (nmem_init_flag);
251 if (!p || p->size - p->top < size)
259 p->top += (size + (sizeof(long) - 1)) & ~(sizeof(long) - 1);
265 int nmem_total(NMEM n)
271 NMEM nmem_create_f(const char *file, int line)
273 NMEM nmem_create(void)
278 struct nmem_debug_info *debug_p;
285 cfreelist = cfreelist->next;
287 r = (nmem_control *)xmalloc(sizeof(*r));
291 yaz_log (LOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line,
299 for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next)
302 yaz_log (LOG_FATAL, "multi used block in nmem");
305 debug_p = xmalloc (sizeof(*debug_p));
306 strncpy (debug_p->file, file, sizeof(debug_p->file)-1);
307 debug_p->file[sizeof(debug_p->file)-1] = '\0';
308 debug_p->line = line;
310 debug_p->next = nmem_debug_list;
311 nmem_debug_list = debug_p;
319 void nmem_destroy_f(const char *file, int line, NMEM n)
321 void nmem_destroy(NMEM n)
325 struct nmem_debug_info **debug_p;
332 yaz_log (LOG_DEBUG, "%s:%d: nmem_destroy %d p=%p", file, line,
333 nmem_active_no-1, n);
335 for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next)
336 if ((*debug_p)->p == n)
338 struct nmem_debug_info *debug_save = *debug_p;
339 *debug_p = (*debug_p)->next;
348 yaz_log (LOG_WARN, "%s:%d destroying unallocated nmem block p=%p",
361 void nmem_transfer (NMEM dst, NMEM src)
364 while ((t=src->blocks))
366 src->blocks = t->next;
367 t->next = dst->blocks;
370 dst->total += src->total;
374 void nmem_critical_enter (void)
379 void nmem_critical_leave (void)
384 void nmem_init (void)
386 if (++nmem_init_flag == 1)
389 InitializeCriticalSection(&critical_section);
390 #elif YAZ_GNU_THREADS
391 yaz_log (LOG_LOG, "pth_init");
400 void nmem_exit (void)
402 if (--nmem_init_flag == 0)
407 struct nmem_block *fl = freelist;
408 freelist = freelist->next;
414 struct nmem_control *cfl = cfreelist;
415 cfreelist = cfreelist->next;
419 DeleteCriticalSection(&critical_section);
426 BOOL WINAPI DllMain (HINSTANCE hinstDLL,
432 case DLL_PROCESS_ATTACH:
435 case DLL_PROCESS_DETACH:
447 void yaz_set_errno(int v)
452 void yaz_strerror(char *buf, int max)
456 DWORD err = GetLastError();
460 FORMAT_MESSAGE_FROM_SYSTEM,
463 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* Default lang */
473 #if YAZ_POSIX_THREADS
475 strerror_r(errno, buf, max);
476 /* if buffer is unset - use strerror anyway (GLIBC bug) */
478 strcpy(buf, strerror(yaz_errno()));
480 strcpy(buf, strerror(yaz_errno()));
483 strcpy(buf, strerror(yaz_errno()));
487 if ((cp=strrchr(buf, '\n')))
489 if ((cp=strrchr(buf, '\r')))