diff options
author | Frederic Culot <calcurse@culot.org> | 2009-07-12 17:48:12 +0000 |
---|---|---|
committer | Frederic Culot <calcurse@culot.org> | 2009-07-12 17:48:12 +0000 |
commit | 834a7e9aafbc8c07aad3d7f067cca42e6d2e33ed (patch) | |
tree | 5588613a3e7af3c0a5f73f552251b924d299d13a /src | |
parent | 56949550025f447691b0f32632b35af4749da358 (diff) | |
download | calcurse-834a7e9aafbc8c07aad3d7f067cca42e6d2e33ed.zip |
New wrappers around memory functions.
Diffstat (limited to 'src')
-rwxr-xr-x | src/io.c | 4 | ||||
-rw-r--r-- | src/mem.c | 92 | ||||
-rw-r--r-- | src/mem.h | 37 | ||||
-rwxr-xr-x | src/utils.c | 4 |
4 files changed, 103 insertions, 34 deletions
@@ -1,4 +1,4 @@ -/* $calcurse: io.c,v 1.67 2009/07/12 16:22:00 culot Exp $ */ +/* $calcurse: io.c,v 1.68 2009/07/12 17:48:13 culot Exp $ */ /* * Calcurse - text-based organizer @@ -1825,7 +1825,7 @@ ical_unfold_content (FILE *fd, char *line, unsigned *lineno) return NULL; } newsize = strlen (content) + strlen (tmpline) + 1; - if ((rline = mem_realloc (content, newsize)) == NULL) + if ((rline = mem_realloc (content, newsize, 1)) == NULL) { mem_free (content); mem_free (tmpline); @@ -1,4 +1,4 @@ -/* $calcurse: mem.c,v 1.4 2009/07/05 20:33:21 culot Exp $ */ +/* $calcurse: mem.c,v 1.5 2009/07/12 17:48:13 culot Exp $ */ /* * Calcurse - text-based organizer @@ -39,6 +39,7 @@ #include <string.h> #include <stdio.h> #include <stdint.h> +#include <stdlib.h> #include "i18n.h" #include "utils.h" @@ -71,7 +72,7 @@ typedef struct { static mem_stats_t mstats; -unsigned +static unsigned stats_add_blk (size_t size, const char *pos) { struct mem_blk_s *o, **i; @@ -94,7 +95,7 @@ stats_add_blk (size_t size, const char *pos) return o->id; } -void +static void stats_del_blk (unsigned id) { struct mem_blk_s *o, **i; @@ -118,16 +119,74 @@ stats_del_blk (unsigned id) } void * +xmalloc (size_t size) +{ + void *p; + + EXIT_IF (size == 0, _("xmalloc: zero size")); + p = malloc (size); + EXIT_IF (p == 0, _("xmalloc: out of memory")); + + return p; +} + +void * +xcalloc (size_t nmemb, size_t size) +{ + void *p; + + EXIT_IF (nmemb == 0 || size == 0, _("xcalloc: zero size")); + EXIT_IF (SIZE_MAX / nmemb < size, _("xcalloc: overflow")); + p = calloc (nmemb, size); + EXIT_IF (p == 0, _("xcalloc: out of memory")); + + return p; +} + +void * +xrealloc (void *ptr, size_t nmemb, size_t size) +{ + void *new_ptr; + size_t new_size; + + new_size = nmemb * size; + EXIT_IF (new_size == 0, _("xrealloc: zero size")); + EXIT_IF (SIZE_MAX / nmemb < size, _("xrealloc: overflow")); + new_ptr = realloc (ptr, new_size); + EXIT_IF (new_ptr == 0, _("xrealloc: out of memory")); + + return new_ptr; +} + +char * +xstrdup (const char *str) +{ + size_t len; + char *cp; + + len = strlen (str) + 1; + cp = xmalloc (len); + + return strncpy (cp, str, len); +} + +void +xfree (void *p) +{ + EXIT_IF (p == 0, _("xfree: null pointer")); + free (p); +} + +void * dbg_malloc (size_t size, const char *pos) { unsigned *buf; - if (size == 0) + if (size == 0) return (void *)0; size = EXTRA_SPACE + (size + sizeof (unsigned) - 1) / sizeof (unsigned); - if ((buf = (unsigned *)malloc (size * sizeof (unsigned))) == 0) - return (void *)0; + buf = xmalloc (size * sizeof (unsigned)); buf[BLK_STATE] = MAGIC_ALLOC; /* state of the block */ buf[BLK_SIZE] = size; /* size of the block */ @@ -159,18 +218,24 @@ dbg_calloc (size_t nmemb, size_t size, const char *pos) } void * -dbg_realloc (void *ptr, size_t size, const char *pos) +dbg_realloc (void *ptr, size_t nmemb, size_t size, const char *pos) { - unsigned *buf, old_size, cpy_size; + unsigned *buf, old_size, new_size, cpy_size; - if (size == 0 || ptr == 0) + if (ptr == 0) + return (void *)0; + + new_size = nmemb *size; + if (new_size == 0) return (void *)0; - if ((buf = dbg_malloc (size, pos)) == 0) + EXIT_IF (nmemb > SIZE_MAX / size, _("overflow at %s"), pos); + + if ((buf = dbg_malloc (new_size, pos)) == 0) return (void *)0; old_size = *((unsigned *)ptr - EXTRA_SPACE_START + BLK_SIZE); - cpy_size = (old_size > size) ? size : old_size; + cpy_size = (old_size > new_size) ? new_size : old_size; bcopy (ptr, buf, cpy_size); mem_free (ptr); @@ -199,8 +264,7 @@ dbg_free (void *ptr, const char *pos) { unsigned *buf, size; - if (ptr == 0) - return; + EXIT_IF (ptr == 0, _("dbg_free: null pointer at %s"), pos); buf = (unsigned *)ptr - EXTRA_SPACE_START; size = buf[BLK_SIZE]; @@ -217,7 +281,7 @@ dbg_free (void *ptr, const char *pos) stats_del_blk (buf[BLK_ID]); - free (buf); + xfree (buf); mstats.nfree += size; } @@ -1,9 +1,9 @@ -/* $calcurse: mem.h,v 1.2 2009/07/05 20:33:22 culot Exp $ */ +/* $calcurse: mem.h,v 1.3 2009/07/12 17:48:13 culot Exp $ */ /* * Calcurse - text-based organizer * - * Copyright (c) 2008 Frederic Culot <frederic@culot.org> + * Copyright (c) 2008-2009 Frederic Culot <frederic@culot.org> * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -43,32 +43,37 @@ #include "config.h" #endif /* HAVE_CONFIG_H */ -#ifdef CALCURSE_MEMORY_DEBUG +#include "utils.h" -#include <stdlib.h> -#include "utils.h" +void *xmalloc (size_t); +void *xcalloc (size_t, size_t); +void *xrealloc (void *, size_t, size_t); +char *xstrdup (const char *); +void xfree (void *); + +#ifdef CALCURSE_MEMORY_DEBUG -#define mem_malloc(s) dbg_malloc ((s), __FILE_POS__) -#define mem_calloc(n, s) dbg_calloc ((n), (s), __FILE_POS__) -#define mem_realloc(p, s) dbg_realloc ((p), (s), __FILE_POS__) -#define mem_strdup(s) dbg_strdup ((s), __FILE_POS__) -#define mem_free(p) dbg_free ((p), __FILE_POS__) +#define mem_malloc(s) dbg_malloc ((s), __FILE_POS__) +#define mem_calloc(n, s) dbg_calloc ((n), (s), __FILE_POS__) +#define mem_realloc(p, n, s) dbg_realloc ((p), (n), (s), __FILE_POS__) +#define mem_strdup(s) dbg_strdup ((s), __FILE_POS__) +#define mem_free(p) dbg_free ((p), __FILE_POS__) void *dbg_malloc (size_t, const char *); void *dbg_calloc (size_t, size_t, const char *); -void *dbg_realloc (void *, size_t, const char *); +void *dbg_realloc (void *, size_t, size_t, const char *); char *dbg_strdup (const char *, const char *); void dbg_free (void *, const char *); void mem_stats (void); #else /* !CALCURSE_MEMORY_DEBUG */ -#define mem_malloc(s) malloc ((s)) -#define mem_calloc(n, s) calloc ((n), (s)) -#define mem_realloc(p, s) realloc ((p), (s)) -#define mem_strdup(s) strdup ((s)) -#define mem_free(p) free ((p)) +#define mem_malloc(s) xmalloc ((s)) +#define mem_calloc(n, s) xcalloc ((n), (s)) +#define mem_realloc(p, n, s) xrealloc ((p), (n), (s)) +#define mem_strdup(s) xstrdup ((s)) +#define mem_free(p) xfree ((p)) #define mem_stats() #endif /* CALCURSE_MEMORY_DEBUG */ diff --git a/src/utils.c b/src/utils.c index 66ee6d5..e041bef 100755 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $calcurse: utils.c,v 1.75 2009/07/12 16:22:01 culot Exp $ */ +/* $calcurse: utils.c,v 1.76 2009/07/12 17:48:14 culot Exp $ */ /* * Calcurse - text-based organizer @@ -431,7 +431,7 @@ updatestring (WINDOW *win, char **str, int x, int y) if (!escape) { len = strlen (newstr) + 1; - *str = mem_realloc (*str, len); + *str = mem_realloc (*str, len, 1); EXIT_IF (*str == 0, _("out of memory")); (void) memcpy (*str, newstr, len); } |