diff options
author | 2008-12-31 07:13:17 -0500 | |
---|---|---|
committer | 2008-12-31 09:52:26 -0500 | |
commit | c9f9ef7dc32c851b44f51b67912cf2d9a48b108f (patch) | |
tree | 4dc4420892900cd2799918055ccd517fb28eb039 /libsbutil/sb_memory.c | |
parent | libsandbox/libsbutil: link with -no-undefined (diff) | |
download | sandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.tar.gz sandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.tar.bz2 sandbox-c9f9ef7dc32c851b44f51b67912cf2d9a48b108f.zip |
libsbutil: cleanup x* memory functions
Pull the x* memory functions out of rcscripts and into libsbutil and change
their style to match the rest of sbutil. Also add xzalloc() and xstrdup(),
and convert pointless strndup() usage to strdup().
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
Diffstat (limited to 'libsbutil/sb_memory.c')
-rw-r--r-- | libsbutil/sb_memory.c | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/libsbutil/sb_memory.c b/libsbutil/sb_memory.c new file mode 100644 index 0000000..23d74af --- /dev/null +++ b/libsbutil/sb_memory.c @@ -0,0 +1,88 @@ +/* + * debug.c + * + * Simle debugging/logging macro's and functions. + * + * Copyright 1999-2008 Gentoo Foundation + * Copyright 2004-2007 Martin Schlemmer <azarah@nosferatu.za.org> + * Licensed under the GPL-2 + */ + +#include "headers.h" +#include "sbutil.h" + +void * +__xcalloc(size_t nmemb, size_t size, const char *file, const char *func, size_t line) +{ + void *ret = calloc(nmemb, size); + + if (ret == NULL) { + SB_EERROR("calloc()", " %s:%s():%zu: calloc(%zu, %zu) failed: %s\n", + file, func, line, nmemb, size, strerror(errno)); + abort(); + } + + return ret; +} + +void * +__xmalloc(size_t size, const char *file, const char *func, size_t line) +{ + void *ret = malloc(size); + + if (ret == NULL) { + SB_EERROR("malloc()", " %s:%s():%zu: malloc(%zu) failed: %s\n", + file, func, line, size, strerror(errno)); + abort(); + } + + return ret; +} + +void * +__xzalloc(size_t size /*, const char *file, const char *func, size_t line */) +{ + return memset(xmalloc(size), 0x00, size); +} + +void * +__xrealloc(void *ptr, size_t size, const char *file, const char *func, size_t line) +{ + void *ret = realloc(ptr, size); + + if (ret == NULL) { + SB_EERROR("realloc()", " %s:%s():%zu: realloc(%p, %zu) failed: %s\n", + file, func, line, ptr, size, strerror(errno)); + abort(); + } + + return ret; +} + +char * +__xstrdup(const char *str, const char *file, const char *func, size_t line) +{ + char *ret = strdup(str); + + if (ret == NULL) { + SB_EERROR("strdup()", " %s:%s():%zu: strdup(%p) failed: %s\n", + file, func, line, str, strerror(errno)); + abort(); + } + + return ret; +} + +char * +__xstrndup(const char *str, size_t size, const char *file, const char *func, size_t line) +{ + char *ret = strndup(str, size); + + if (ret == NULL) { + SB_EERROR("strndup()", " %s:%s():%zu: strndup(%p, %zu) failed: %s\n", + file, func, line, str, size, strerror(errno)); + abort(); + } + + return ret; +} |