/*
- * 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,
* 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 $
*/
/**
#include <stddef.h>
#include <yaz/yconfig.h>
+#if YAZ_HAVE_XML2
+#include <libxml/parser.h>
+#endif
+
#define NMEM_DEBUG 0
#ifndef NMEM_DEBUG
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
#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
/*
- * 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 $
*/
/**
struct nmem_control
{
int total;
- nmem_block *blocks;
+ struct nmem_block *blocks;
struct nmem_control *next;
};
};
#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;
}
}
-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 */
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);
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);
/*
* 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);
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;
void nmem_reset(NMEM n)
{
- nmem_block *t;
+ struct nmem_block *t;
yaz_log (log_level, "nmem_reset p=%p", n);
if (!n)
if (r)
cfreelist = cfreelist->next;
else
- r = (nmem_control *)xmalloc(sizeof(*r));
+ r = (struct nmem_control *)xmalloc(sizeof(*r));
NMEM_LEAVE;
#if NMEM_DEBUG
void nmem_transfer (NMEM dst, NMEM src)
{
- nmem_block *t;
+ struct nmem_block *t;
while ((t = src->blocks))
{
src->blocks = t->next;
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;
void nmem_init (void)
{
-
if (++nmem_init_flag == 1)
{
#ifdef WIN32
while (freelist)
{
struct nmem_block *fl = freelist;
+ nmem_memory_free -= fl->size;
freelist = freelist->next;
xfree (fl->buf);
xfree (fl);