diff options
author | remoitnane <remoit(DOT)nane(AT)gmail(DOT)com> | 2010-08-07 17:58:47 -0700 |
---|---|---|
committer | remoitnane <remoit(DOT)nane(AT)gmail(DOT)com> | 2010-08-07 17:58:47 -0700 |
commit | a2849240106686f7e745809baaf63a3674e93971 (patch) | |
tree | 1ded41dc5ab54ba37a4ba5c0eb003d5cca033003 /src/common/malloc.c | |
parent | fb9e8532b45b9b9aaa66842715db7d57b5374c20 (diff) | |
download | tmwa-a2849240106686f7e745809baaf63a3674e93971.tar.gz tmwa-a2849240106686f7e745809baaf63a3674e93971.tar.bz2 tmwa-a2849240106686f7e745809baaf63a3674e93971.tar.xz tmwa-a2849240106686f7e745809baaf63a3674e93971.zip |
Tweak memory wrappers and add one for strdup()
Diffstat (limited to 'src/common/malloc.c')
-rw-r--r-- | src/common/malloc.c | 55 |
1 files changed, 35 insertions, 20 deletions
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 <stdio.h> #include <stdlib.h> +#include <string.h> #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; } |