From c168cffc46e3b2b66e66e48f94f8e87b6566702b Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Fri, 11 Aug 2006 12:50:23 +0000 Subject: [PATCH] Changed prototype for nmem_text_node_cdata so that ptr is using Libxml2 type rather than void pointer. Added functions nmem_get_memory_in_use and nmem_get_memory_free which returns size of block memory in use and in free lists respectively. Added doxygen friendly comments for nmem.h. --- include/yaz/nmem.h | 73 +++++++++++++++++++++++++++++++++++++++++++++------- src/nmem.c | 53 ++++++++++++++++++++++++++++---------- src/nmemsdup.c | 8 +++--- 3 files changed, 108 insertions(+), 26 deletions(-) diff --git a/include/yaz/nmem.h b/include/yaz/nmem.h index 2f8fb6f..189b25d 100644 --- a/include/yaz/nmem.h +++ b/include/yaz/nmem.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 1995-2005, Index Data ApS + * Copyright (C) 1995-2006, Index Data ApS * * Permission to use, copy, modify, distribute, and sell this software and * its documentation, in whole or in part, for any purpose, is hereby granted, @@ -23,7 +23,7 @@ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE * OF THIS SOFTWARE. * - * $Id: nmem.h,v 1.19 2006-05-03 13:04:46 adam Exp $ + * $Id: nmem.h,v 1.20 2006-08-11 12:50:23 adam Exp $ */ /** @@ -39,6 +39,10 @@ #include #include +#if YAZ_HAVE_XML2 +#include +#endif + #define NMEM_DEBUG 0 #ifndef NMEM_DEBUG @@ -47,34 +51,64 @@ YAZ_BEGIN_CDECL -typedef struct nmem_block nmem_block; - -typedef struct nmem_control nmem_control; - +/** \brief NMEM/YAZ MUTEX opaque pointer */ typedef struct nmem_mutex *NMEM_MUTEX; +/** \brief create Mutex */ YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *); +/** \brief enter critical section / AKA lock */ YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX); +/** \brief leave critical section / AKA unlock */ YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX); +/** \brief destroy MUTEX */ YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *); +/** \brief NMEM handle (an opaque pointer to memory) */ typedef struct nmem_control *NMEM; +/** \brief release all memory associaged with an NMEM handle */ YAZ_EXPORT void nmem_reset(NMEM n); +/** \brief returns size in bytes of memory for NMEM handle */ YAZ_EXPORT int nmem_total(NMEM n); + +/** \brief allocates string on NMEM handle (similar strdup) */ YAZ_EXPORT char *nmem_strdup (NMEM mem, const char *src); +/** \brief allocates string on NMEM handle - allows NULL ptr buffer */ YAZ_EXPORT char *nmem_strdup_null (NMEM mem, const char *src); +/** \brief allocates string of certain size on NMEM handle */ YAZ_EXPORT char *nmem_strdupn (NMEM mem, const char *src, size_t n); -YAZ_EXPORT void nmem_strsplit_blank(NMEM nmem, const char *dstr, - char ***darray, int *num); + +/** \brief allocates sub strings out of string using certain delimitors + \param nmem NMEM handle + \param delim delimitor chars (splits on each char in there) + \param dstr string to be split + \param darray result string array for each sub string + \param num number of result strings +*/ YAZ_EXPORT void nmem_strsplit(NMEM nmem, const char *delim, const char *dstr, char ***darray, int *num); -YAZ_EXPORT char *nmem_text_node_cdata(const void *ptr_cdata, NMEM nmem); +/** \brief splits string into sub strings delimited by blanks + \param nmem NMEM handle + \param dstr string to be split + \param darray result string array for each sub string + \param num number of result strings +*/ +YAZ_EXPORT void nmem_strsplit_blank(NMEM nmem, const char *dstr, + char ***darray, int *num); + +/** \brief copies TEXT Libxml2 node data to NMEM */ +YAZ_EXPORT char *nmem_text_node_cdata(const xmlNode *ptr, NMEM nmem); + +/** \brief creates and allocates integer for NMEM */ YAZ_EXPORT int *nmem_intdup (NMEM mem, int v); + +/** \brief transfers memory from one NMEM handle to another */ YAZ_EXPORT void nmem_transfer (NMEM dst, NMEM src); +/** \brief internal (do not use) */ YAZ_EXPORT void nmem_critical_enter (void); +/** \brief internal (do not use) */ YAZ_EXPORT void nmem_critical_leave (void); #if NMEM_DEBUG @@ -91,20 +125,41 @@ YAZ_EXPORT void nmem_print_list_l (int level); #else +/** \brief returns new NMEM handle */ YAZ_EXPORT NMEM nmem_create(void); + +/** \brief destroys NMEM handle and memory associated with it */ YAZ_EXPORT void nmem_destroy(NMEM n); + +/** \brief allocate memory block on NMEM handle */ YAZ_EXPORT void *nmem_malloc(NMEM n, int size); #define nmem_print_list() #endif +/** \brief initializes NMEM system + This function increments a usage counter for NMEM.. Only + on first usage the system is initialized.. The \fn nmem_exit + decrements the counter. So these must be called in pairs +*/ YAZ_EXPORT void nmem_init (void); + +/** \brief destroys NMEM system */ YAZ_EXPORT void nmem_exit (void); + YAZ_EXPORT int yaz_errno (void); YAZ_EXPORT void yaz_set_errno (int v); YAZ_EXPORT void yaz_strerror(char *buf, int max); +/** \brief returns memory in use (by application) + \param p pointer to size (in bytes) + */ +YAZ_EXPORT void nmem_get_memory_in_use(size_t *p); +/** \brief returns memory in free (for later reuse) + */ +YAZ_EXPORT void nmem_get_memory_free(size_t *p); + YAZ_END_CDECL #endif diff --git a/src/nmem.c b/src/nmem.c index 233abcc..2c7e202 100644 --- a/src/nmem.c +++ b/src/nmem.c @@ -1,8 +1,8 @@ /* - * Copyright (C) 1995-2005, Index Data ApS + * Copyright (C) 1995-2006, Index Data ApS * See the file LICENSE for details. * - * $Id: nmem.c,v 1.23 2006-08-09 14:00:18 adam Exp $ + * $Id: nmem.c,v 1.24 2006-08-11 12:50:23 adam Exp $ */ /** @@ -54,7 +54,7 @@ struct nmem_block struct nmem_control { int total; - nmem_block *blocks; + struct nmem_block *blocks; struct nmem_control *next; }; @@ -107,6 +107,9 @@ struct nmem_mutex { }; #endif +size_t nmem_memory_in_use = 0; +size_t nmem_memory_free = 0; + YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p) { NMEM_ENTER; @@ -166,9 +169,16 @@ YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p) } } -static nmem_block *freelist = NULL; /* "global" freelists */ -static nmem_control *cfreelist = NULL; +/** \brief free NMEM memory blocks . Reused in get_block */ +static struct nmem_block *freelist = NULL; + +/** \brief free NMEM control blocks. Reused in nmem_create */ +static struct nmem_control *cfreelist = NULL; + +/** \brief number NMEM's in use (number of nmem_controls not in free list) */ static int nmem_active_no = 0; + +/** \brief NMEM usage counter */ static int nmem_init_flag = 0; /** \brief whether nmem blocks should be reassigned to heap */ @@ -185,8 +195,9 @@ struct nmem_debug_info { struct nmem_debug_info *nmem_debug_list = 0; #endif -static void free_block(nmem_block *p) +static void free_block(struct nmem_block *p) { + nmem_memory_in_use -= p->size; if (nmem_release_in_heap) { xfree(p->buf); @@ -197,6 +208,7 @@ static void free_block(nmem_block *p) memset(p->buf, 'Y', p->size); p->next = freelist; freelist = p; + nmem_memory_free += p->size; } if (log_level) yaz_log (log_level, "nmem free_block p=%p", p); @@ -224,9 +236,9 @@ void nmem_print_list_l (int level) /* * acquire a block with a minimum of size free bytes. */ -static nmem_block *get_block(size_t size) +static struct nmem_block *get_block(size_t size) { - nmem_block *r, *l; + struct nmem_block *r, *l; if (log_level) yaz_log (log_level, "nmem get_block size=%ld", (long) size); @@ -253,8 +265,9 @@ static nmem_block *get_block(size_t size) yaz_log (log_level, "nmem get_block alloc new block size=%ld", (long) get); - r = (nmem_block *)xmalloc(sizeof(*r)); + r = (struct nmem_block *) xmalloc(sizeof(*r)); r->buf = (char *)xmalloc(r->size = get); + nmem_memory_in_use += r->size; } r->top = 0; return r; @@ -262,7 +275,7 @@ static nmem_block *get_block(size_t size) void nmem_reset(NMEM n) { - nmem_block *t; + struct nmem_block *t; yaz_log (log_level, "nmem_reset p=%p", n); if (!n) @@ -343,7 +356,7 @@ NMEM nmem_create(void) if (r) cfreelist = cfreelist->next; else - r = (nmem_control *)xmalloc(sizeof(*r)); + r = (struct nmem_control *)xmalloc(sizeof(*r)); NMEM_LEAVE; #if NMEM_DEBUG @@ -426,7 +439,7 @@ void nmem_destroy(NMEM n) void nmem_transfer (NMEM dst, NMEM src) { - nmem_block *t; + struct nmem_block *t; while ((t = src->blocks)) { src->blocks = t->next; @@ -437,6 +450,20 @@ void nmem_transfer (NMEM dst, NMEM src) src->total = 0; } +void nmem_get_memory_in_use(size_t *p) +{ + NMEM_ENTER; + *p = nmem_memory_in_use; + NMEM_LEAVE; +} + +void nmem_get_memory_free(size_t *p) +{ + NMEM_ENTER; + *p = nmem_memory_free; + NMEM_LEAVE; +} + void nmem_critical_enter (void) { NMEM_ENTER; @@ -449,7 +476,6 @@ void nmem_critical_leave (void) void nmem_init (void) { - if (++nmem_init_flag == 1) { #ifdef WIN32 @@ -476,6 +502,7 @@ void nmem_exit (void) while (freelist) { struct nmem_block *fl = freelist; + nmem_memory_free -= fl->size; freelist = freelist->next; xfree (fl->buf); xfree (fl); diff --git a/src/nmemsdup.c b/src/nmemsdup.c index a0856ec..4ba527d 100644 --- a/src/nmemsdup.c +++ b/src/nmemsdup.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2006, Index Data ApS * See the file LICENSE for details. * - * $Id: nmemsdup.c,v 1.8 2006-07-06 10:17:53 adam Exp $ + * $Id: nmemsdup.c,v 1.9 2006-08-11 12:50:23 adam Exp $ */ /** @@ -90,18 +90,18 @@ void nmem_strsplit(NMEM nmem, const char *delim, const char *dstr, } #if YAZ_HAVE_XML2 -char *nmem_text_node_cdata(const void *ptr_cdata, NMEM nmem) +char *nmem_text_node_cdata(const xmlNode *ptr_cdata, NMEM nmem) { char *cdata; int len = 0; const xmlNode *ptr; - for (ptr = (const xmlNode *) ptr_cdata; ptr; ptr = ptr->next) + for (ptr = ptr_cdata; ptr; ptr = ptr->next) if (ptr->type == XML_TEXT_NODE) len += xmlStrlen(ptr->content); cdata = (char *) nmem_malloc(nmem, len+1); *cdata = '\0'; - for (ptr = (const xmlNode *) ptr_cdata; ptr; ptr = ptr->next) + for (ptr = ptr_cdata; ptr; ptr = ptr->next) if (ptr->type == XML_TEXT_NODE) strcat(cdata, (const char *) ptr->content); return cdata; -- 1.7.10.4