From a2849240106686f7e745809baaf63a3674e93971 Mon Sep 17 00:00:00 2001 From: remoitnane Date: Sat, 7 Aug 2010 17:58:47 -0700 Subject: Tweak memory wrappers and add one for strdup() --- src/common/malloc.c | 55 ++++++++++++++++++++++++++++++++++------------------- src/common/malloc.h | 3 +-- 2 files changed, 36 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/common/malloc.c b/src/common/malloc.c index 3b302b7..8d68bee 100644 --- a/src/common/malloc.c +++ b/src/common/malloc.c @@ -1,53 +1,68 @@ #include #include +#include #include "malloc.h" +#define SHOW_MEMORY_ERROR(file, line, func, type, msg) \ + fprintf (stderr, "%s:%d: in func %s(): %s() error, %s\n", \ + file, line, func, type, msg) + void *aMalloc_ (size_t size, const char *file, int line, const char *func) { - void *ret; + void *ret = malloc (size); -// printf("%s:%d: in func %s: malloc %d\n",file,line,func,size); - ret = malloc (size); + /* printf ("%s:%d: in func %s(): malloc(%d)\n", file, line, func, size); */ if (ret == NULL) { - printf ("%s:%d: in func %s: malloc error out of memory!\n", file, - line, func); - exit (1); - + SHOW_MEMORY_ERROR (file, line, func, "malloc", "out of memory"); + exit (EXIT_FAILURE); } + return ret; } void *aCalloc_ (size_t num, size_t size, const char *file, int line, const char *func) { - void *ret; + void *ret = calloc (num, size); -// printf("%s:%d: in func %s: calloc %d %d\n",file,line,func,num,size); - ret = calloc (num, size); + /* printf ("%s:%d: in func %s(): calloc(%d, %d)\n", file, line, func, num, + size); */ if (ret == NULL) { - printf ("%s:%d: in func %s: calloc error out of memory!\n", file, - line, func); - exit (1); - + SHOW_MEMORY_ERROR (file, line, func, "calloc", "out of memory"); + exit (EXIT_FAILURE); } + return ret; } void *aRealloc_ (void *p, size_t size, const char *file, int line, const char *func) { - void *ret; + void *ret = realloc (p, size); -// printf("%s:%d: in func %s: realloc %p %d\n",file,line,func,p,size); - ret = realloc (p, size); + /* printf ("%s:%d: in func %s(): realloc(%p, %d)\n", file, line, func, p, + size); */ if (ret == NULL) { - printf ("%s:%d: in func %s: realloc error out of memory!\n", file, - line, func); - exit (1); + SHOW_MEMORY_ERROR (file, line, func, "realloc", "out of memory"); + exit (EXIT_FAILURE); + } + + return ret; +} + +char *aStrdup_ (const char *p, const char *file, int line, const char *func) +{ + char *ret = strdup (p); + /* printf ("%s:%d: in func %s(): strdup(%p)\n", file, line, func, p); */ + if (ret == NULL) + { + SHOW_MEMORY_ERROR (file, line, func, "strdup", "out of memory"); + exit (EXIT_FAILURE); } + return ret; } diff --git a/src/common/malloc.h b/src/common/malloc.h index 3195773..5fc8cbe 100644 --- a/src/common/malloc.h +++ b/src/common/malloc.h @@ -1,8 +1,6 @@ #ifndef _MALLOC_H_ #define _MALLOC_H_ -#include - #if __STDC_VERSION__ < 199901L # if __GNUC__ >= 2 # define __func__ __FUNCTION__ @@ -16,6 +14,7 @@ void *aMalloc_ (size_t, const char *, int, const char *); void *aCalloc_ (size_t, size_t, const char *, int, const char *); void *aRealloc_ (void *, size_t, const char *, int, const char *); +char *aStrdup_ (const char *, const char *, int, const char *); #define aMalloc(n) aMalloc_(n,ALC_MARK) #define aMallocA(n) aMalloc_(n,ALC_MARK) -- cgit v1.2.3-70-g09d2