From: Adam Dickmeiss Date: Thu, 30 Apr 2009 08:02:39 +0000 (+0200) Subject: Doxygen comments for xmalloc utils. Reformat X-Git-Tag: v3.0.45.rhel53~6 X-Git-Url: http://sru.miketaylor.org.uk/?a=commitdiff_plain;h=29b3b3213c4c8ad38bab054b56f8b70f1b14d67a;p=yaz-moved-to-github.git Doxygen comments for xmalloc utils. Reformat --- diff --git a/include/yaz/xmalloc.h b/include/yaz/xmalloc.h index 2c3ff9f..4aa6d9a 100644 --- a/include/yaz/xmalloc.h +++ b/include/yaz/xmalloc.h @@ -27,7 +27,11 @@ /** * \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 @@ -39,22 +43,99 @@ 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 diff --git a/src/xmalloc.c b/src/xmalloc.c index 4aa922c..7042079 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -22,8 +22,8 @@ #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 @@ -49,14 +49,14 @@ void *xmalloc_d(size_t nbytes, const char *file, int 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 (!(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; @@ -97,7 +97,7 @@ void xfree_d(void *ptr, const char *file, int line) 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; } @@ -110,8 +110,8 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int 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 (!ptr) @@ -144,7 +144,7 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) if (!nbytes) { - free (dinfo); + free(dinfo); return 0; } res = (char *) @@ -153,7 +153,7 @@ void *xrealloc_d(void *p, size_t nbytes, const char *file, int line) 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; @@ -178,14 +178,14 @@ void *xcalloc_d(size_t nmemb, size_t size, const char *file, int 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 (!(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; @@ -209,19 +209,19 @@ void xmalloc_trav_d(const char *file, int 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; } - 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 @@ -237,8 +237,8 @@ void xmalloc_trav_f(const char *s, const char *file, int 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; } xmalloc_trav_d(file, line); @@ -249,19 +249,19 @@ void xmalloc_fatal(void) 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)", @@ -271,65 +271,65 @@ void *xrealloc_f (void *o, size_t size, const char *file, int line) 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; } @@ -338,7 +338,7 @@ void xfree_f(void *p, const char *file, int line) 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); }