From 5d932e4191a616b758903e3d252cdab2a8e00ce6 Mon Sep 17 00:00:00 2001 From: moritz Date: Sat, 24 Feb 2007 20:49:25 +0000 Subject: [PATCH] Add new utility functions, for memory management. These do The Right Thing and never return NULL. git-svn-id: https://svn.xiph.org/trunk/ezstream@12524 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- src/util.c | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/util.h | 26 ++++++++++ 2 files changed, 166 insertions(+) create mode 100644 src/util.c create mode 100644 src/util.h diff --git a/src/util.c b/src/util.c new file mode 100644 index 0000000..d90b58f --- /dev/null +++ b/src/util.c @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2007 Moritz Grimm + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_SYS_TYPES_H +# include +#endif +#include +#include +#include +#include +#include + +#include "util.h" + +#ifndef SIZE_T_MAX +# define SIZE_T_MAX UINT_MAX +#endif + +extern int errno; +extern char *__progname; + +void * +xmalloc(size_t size) +{ + void *ret; + + if (size == 0) { + printf("%s: xmalloc(): Internal error: zero size\n", + __progname); + abort(); + } + + if ((ret = malloc(size)) == NULL) { + printf("%s: xmalloc(): Allocating %lu bytes: %s\n", + __progname, (unsigned long)(size), strerror(errno)); + exit(1); + } + + return (ret); +} + +void * +xcalloc(size_t nmemb, size_t size) +{ + void *ret; + + if (nmemb == 0 || size == 0) { + printf("%s: xcalloc(): Internal error: zero size\n", + __progname); + abort(); + } + + if (SIZE_T_MAX / nmemb < size) { + printf("%s: xcalloc(): Integer overflow, nmemb * size > SIZE_T_MAX\n", + __progname); + exit(1); + } + + if ((ret = calloc(nmemb, size)) == NULL) { + printf("%s: xcalloc(): Allocating %lu bytes: %s\n", + __progname, (unsigned long)(nmemb * size), strerror(errno)); + exit(1); + } + + return (ret); +} + +void * +xrealloc(void *ptr, size_t nmemb, size_t size) +{ + void *ret; + size_t nsiz = nmemb * size; + + if (nmemb == 0 || size == 0) { + printf("%s: xrealloc(): Internal error: zero size\n", + __progname); + abort(); + } + + if (SIZE_T_MAX / nmemb < size) { + printf("%s: xrealloc(): Integer overflow, nmemb * size > SIZE_T_MAX\n", + __progname); + exit(1); + } + + if (ptr == NULL) + ret = malloc(nsiz); + else + ret = realloc(ptr, nsiz); + + if (ret == NULL) { + printf("%s: xrealloc(): (Re)allocating %lu bytes: %s\n", + __progname, (unsigned long)(nmemb * size), strerror(errno)); + exit(1); + } + + return (ret); +} + +void +xfree(void *ptr) +{ + if (ptr == NULL) { + printf("%s: xfree(): Internal error: NULL argument\n", + __progname); + abort(); + } + + free(ptr); + ptr = NULL; +} + +char * +xstrdup(const char *str) +{ + size_t len; + char *nstr; + + len = strlen(str) + 1; + nstr = xcalloc(1, len); + memcpy(nstr, str, len); + return (nstr); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..25b1298 --- /dev/null +++ b/src/util.h @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2007 Moritz Grimm + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef __UTIL_H__ +#define __UTIL_H__ + +void * xmalloc(size_t /* size */); +void * xcalloc(size_t /* nmemb */, size_t /* size */); +void * xrealloc(void *, size_t /* nmemb */, size_t /* size */); +void xfree(void *); +char * xstrdup(const char *); + +#endif /* __UTIL_H__ */