diff options
author | Steven Schubiger <stsc@refcnt.org> | 2013-01-24 23:59:34 +0100 |
---|---|---|
committer | Steven Schubiger <stsc@refcnt.org> | 2013-01-24 23:59:34 +0100 |
commit | ca8ab7e39af7b3b6d263329bb605925f693d23b8 (patch) | |
tree | d1cbb346ddd23ec44215573ebf44656b78f4b3cf | |
parent | eaa100485541ae65959f78f6ae27e8c5cc1c644d (diff) | |
download | colorize-ca8ab7e39af7b3b6d263329bb605925f693d23b8.tar.gz colorize-ca8ab7e39af7b3b6d263329bb605925f693d23b8.tar.bz2 |
Wrap memory allocation functions
This covers currently calls to malloc(), realloc() and the custom
strdup() (which uses malloc, too). If allocation of memory fails
(i.e., NULL is returned from one of former two calls), a fatal
diagnostic is emitted (accompanied by source filename and line
number if compiled with DEBUG=1).
-rw-r--r-- | colorize.c | 51 |
1 files changed, 45 insertions, 6 deletions
@@ -34,11 +34,17 @@ #include <time.h> #include <unistd.h> +#define DEBUG 0 + #define str(arg) #arg #define to_str(arg) str(arg) #define streq(s1, s2) (strcmp (s1, s2) == 0) +#define xmalloc(size) malloc_wrap(size, __FILE__, __LINE__) +#define xrealloc(ptr, size) realloc_wrap(ptr, size, __FILE__, __LINE__) +#define xstrdup(str) strdup_wrap(str, __FILE__, __LINE__) + #if !defined BUF_SIZE || BUF_SIZE <= 0 # undef BUF_SIZE # define BUF_SIZE 4096 + 1 @@ -57,6 +63,18 @@ release_var (vars_list, stacked_vars, (void **)&ptr); \ } while (false); +#if DEBUG +# define MEM_ALLOC_FAIL(file, line) do { \ + fprintf (stderr, "memory allocation failure in source file %s, line %d\n", file, line); \ + exit (2); \ +} while (false); +#else +# define MEM_ALLOC_FAIL(file, line) do { \ + fprintf (stderr, "%s: memory allocation failure\n", program_name); \ + exit (2); \ +} while (false); +#endif + #define ABORT_TRACE() \ fprintf (stderr, "aborting in source file %s, line %d\n", __FILE__, __LINE__); \ abort (); \ @@ -166,7 +184,9 @@ static void read_print_stream (bool, const struct color **, const char *, FILE * static void find_color_entries (struct color_name **, const struct color **); static void find_color_entry (const char *const, unsigned int, const struct color **); static void print_line (const struct color **, bool, const char * const, unsigned int); -static char *xstrdup (const char *); +static void *malloc_wrap (size_t, const char *, unsigned int); +static void *realloc_wrap (void *, size_t, const char *, unsigned int); +static char *strdup_wrap (const char *, const char *, unsigned int); static void vfprintf_fail (const char *, ...); static void stack_var (void ***, unsigned int *, unsigned int, void *); static void release_var (void **, unsigned int, void **); @@ -436,7 +456,7 @@ process_options (unsigned int arg_cnt, char **option_strings, bool *bold, const } } - color_names[index] = malloc (sizeof (struct color_name)); + color_names[index] = xmalloc (sizeof (struct color_name)); color_names[index]->orig = xstrdup (color); @@ -667,12 +687,31 @@ print_line (const struct color **colors, bool bold, const char *const line, unsi putchar ('\n'); } +static void * +malloc_wrap (size_t size, const char *file, unsigned int line) +{ + void *p = malloc (size); + if (!p) + MEM_ALLOC_FAIL (file, line); + return p; +} + +static void * +realloc_wrap (void *ptr, size_t size, const char *file, unsigned int line) +{ + void *p = realloc (ptr, size); + if (!p) + MEM_ALLOC_FAIL (file, line); + return p; +} + static char * -xstrdup (const char *str) +strdup_wrap (const char *str, const char *file, unsigned int line) { const unsigned int len = strlen (str) + 1; char *p = malloc (len); - assert (p != NULL); + if (!p) + MEM_ALLOC_FAIL (file, line); strncpy (p, str, len); return p; } @@ -696,7 +735,7 @@ stack_var (void ***list, unsigned int *stacked, unsigned int index, void *ptr) if (ptr == NULL) return; if (!*list) - *list = malloc (sizeof (void *)); + *list = xmalloc (sizeof (void *)); else { unsigned int i; @@ -706,7 +745,7 @@ stack_var (void ***list, unsigned int *stacked, unsigned int index, void *ptr) (*list)[i] = ptr; return; /* reused */ } - *list = realloc (*list, (*stacked + 1) * sizeof (void *)); + *list = xrealloc (*list, (*stacked + 1) * sizeof (void *)); } (*list)[index] = ptr; (*stacked)++; |