/**
* \file xmalloc.h
- * \brief Header for malloc interface.
+ * \brief Header for memory handling functions.
+ *
+ * This is a set of wrapper functions for the memory allocation routines
+ * from stdlib.h.. Such as malloc, free, realloc etc.
+ * These functions calls exit if memory allocation fails.
*/
#ifndef XMALLOC_H
YAZ_BEGIN_CDECL
+/** \brief utility macro which calls xrealloc_f */
#define xrealloc(o, x) xrealloc_f(o, x, __FILE__, __LINE__)
+/** \brief utility macro which calls malloc_f */
#define xmalloc(x) xmalloc_f(x, __FILE__, __LINE__)
+/** \brief utility macro which calls xcalloc_f */
#define xcalloc(x,y) xcalloc_f(x,y, __FILE__, __LINE__)
+/** \brief utility macro which calls xfree_f */
#define xfree(x) xfree_f(x, __FILE__, __LINE__)
+/** \brief utility macro which calls xstrdup_f */
#define xstrdup(s) xstrdup_f(s, __FILE__, __LINE__)
+/** \brief utility macro which calls xstrndup_f */
#define xstrndup(s, n) xstrndup_f(s, n, __FILE__, __LINE__)
+/** \brief utility macro which calls malloc_trav_f */
#define xmalloc_trav(s) xmalloc_trav_f(s, __FILE__, __LINE__)
-YAZ_EXPORT void *xrealloc_f (void *o, size_t size, const char *file, int line);
-YAZ_EXPORT void *xmalloc_f (size_t size, const char *file, int line);
-YAZ_EXPORT void *xcalloc_f (size_t nmemb, size_t size,
- const char *file, int line);
-YAZ_EXPORT char *xstrdup_f (const char *p, const char *file, int line);
+/** \brief realloc
+ \param o buffer to be reallocated
+ \param size size of buffer to be allocated
+ \param file fname location of use
+ \param line line location of use
+ \returns buffer
+
+ This function is invoked via macro xrealloc in which file and line are set
+ automatically.
+*/
+
+YAZ_EXPORT void *xrealloc_f(void *o, size_t size, const char *file, int line);
+/** \brief malloc
+ \param size size of buffer to be allocated
+ \param file fname location of use
+ \param line line location of use
+ \returns buffer
+
+ This function is invoked via macro xmalloc in which file and line are set
+ automatically.
+*/
+YAZ_EXPORT void *xmalloc_f(size_t size, const char *file, int line);
+
+/** \brief calloc
+ \param nmemb number of members
+ \param size size of member
+ \param file fname location of use
+ \param line line location of use
+ \returns buffer
+
+ This function is invoked via macro xcalloc in which file and line are set
+ automatically.
+*/
+YAZ_EXPORT void *xcalloc_f(size_t nmemb, size_t size,
+ const char *file, int line);
+/** \brief strdup
+ \param p string to be cloned
+ \param file fname location of use
+ \param line line location of use
+ \returns resulting string
+
+ This function is invoked via macro xstrdup in which file and line are set
+ automatically.
+*/
+YAZ_EXPORT char *xstrdup_f(const char *p, const char *file, int line);
+
+/** \brief strndup
+ \param p string to be cloned
+ \param n max size of resulting string (excluding 0)
+ \param file fname location of use
+ \param line line location of use
+ \returns resulting string
+
+ This function is invoked via macro xstrndup in which file and line are set
+ automatically.
+*/
YAZ_EXPORT char *xstrndup_f(const char *p, size_t n,
const char *file, int line);
-YAZ_EXPORT void xfree_f (void *p, const char *file, int line);
+
+/** \brief free
+ \param p string to be freed (might be NULL)
+ \param file fname location of use
+ \param line line location of use
+
+ This function is invoked via macro xfree in which file and line are set
+ automatically.
+*/
+YAZ_EXPORT void xfree_f(void *p, const char *file, int line);
+
+/** \brief logs all xmalloc buffers
+ \param s not used
+ \param file fname location of use
+ \param line line location of use
+
+ This function is invoked via macro xmalloc_trav in which file and line
+ are set automatically. Only if TRACE_XMALLOC > 1 this function
+ does any real work!
+*/
YAZ_EXPORT void xmalloc_trav_f(const char *s, const char *file, int line);
YAZ_END_CDECL
#define TRACE_XMALLOC 1
#endif
-static int log_level=0;
-static int log_level_initialized=0;
+static int log_level = 0;
+static int log_level_initialized = 0;
#if TRACE_XMALLOC > 1
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
dmalloc_list = dinfo->next;
if (dinfo->next)
dinfo->next->prev = dinfo->prev;
- memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
+ memcpy((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
free(dinfo);
return;
}
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!ptr)
if (!nbytes)
{
- free (dinfo);
+ free(dinfo);
return 0;
}
res = (char *)
if (!res)
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
return 0;
dinfo = (struct dmalloc_info *) res;
- strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
+ strncpy(dinfo->file, file, sizeof(dinfo->file)-1);
dinfo->file[sizeof(dinfo->file)-1] = '\0';
dinfo->line = line;
dinfo->len = nbytes;
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
- yaz_log (log_level, "malloc_trav %s:%d", file, line);
+ yaz_log(log_level, "malloc_trav %s:%d", file, line);
while (dinfo)
{
- yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
- ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
+ yaz_log(log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
+ ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
size += dinfo->len;
dinfo = dinfo->next;
}
- yaz_log (log_level, "total bytes %ld", (long) size);
+ yaz_log(log_level, "total bytes %ld", (long) size);
}
#else
{
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
xmalloc_trav_d(file, line);
exit(1);
}
-void *xrealloc_f (void *o, size_t size, const char *file, int line)
+void *xrealloc_f(void *o, size_t size, const char *file, int line)
{
- void *p = xrealloc_d (o, size, file, line);
+ void *p = xrealloc_d(o, size, file, line);
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
- if(log_level)
+ if (log_level)
yaz_log (log_level,
- "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
+ "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
if (!p)
{
yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)",
return p;
}
-void *xmalloc_f (size_t size, const char *file, int line)
+void *xmalloc_f(size_t size, const char *file, int line)
{
- void *p = xmalloc_d (size, file, line);
+ void *p = xmalloc_d(size, file, line);
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
- yaz_log (log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
- (long) size, p);
+ yaz_log(log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
+ (long) size, p);
if (!p)
{
- yaz_log (YLOG_FATAL, "Out of memory - malloc (%ld bytes)",
- (long) size);
+ yaz_log(YLOG_FATAL, "Out of memory - malloc (%ld bytes)",
+ (long) size);
xmalloc_fatal();
}
return p;
}
-void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
+void *xcalloc_f(size_t nmemb, size_t size, const char *file, int line)
{
- void *p = xcalloc_d (nmemb, size, file, line);
+ void *p = xcalloc_d(nmemb, size, file, line);
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
- yaz_log (log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
- (long) size, p);
+ yaz_log(log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
+ (long) size, p);
if (!p)
{
- yaz_log (YLOG_FATAL, "Out of memory - calloc (%ld, %ld)",
- (long) nmemb, (long) size);
+ yaz_log(YLOG_FATAL, "Out of memory - calloc (%ld, %ld)",
+ (long) nmemb, (long) size);
xmalloc_fatal();
}
return p;
}
-char *xstrdup_f (const char *s, const char *file, int line)
+char *xstrdup_f(const char *s, const char *file, int line)
{
- char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
+ char *p = (char *)xmalloc_d(strlen(s)+1, file, line);
if (!log_level_initialized)
{
- log_level=yaz_log_module_level("malloc");
- log_level_initialized=1;
+ log_level = yaz_log_module_level("malloc");
+ log_level_initialized = 1;
}
if (log_level)
- yaz_log (log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
- (long) strlen(s)+1, p);
+ yaz_log(log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
+ (long) strlen(s)+1, p);
- strcpy (p, s);
+ strcpy(p, s);
return p;
}
if (!p)
return ;
if (log_level)
- yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
+ yaz_log(log_level, "%s:%d: xfree %p", file, line, p);
xfree_d(p, file, line);
}