From ccedd08802f62ed896f69d778e6a106d00f9ab58 Mon Sep 17 00:00:00 2001 From: Mans Rullgard Date: Tue, 8 Dec 2015 22:52:41 +0000 Subject: [PATCH] Clean up lsx_malloc() and friends --- src/Makefile.am | 2 +- src/xmalloc.c | 30 +++++++++++++++++++++++++----- src/xmalloc.h | 7 ++++--- 3 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index cd44ae4d..42573ec5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -95,7 +95,7 @@ libsox_la_LIBADD += @GOMP_LIBS@ libsox_la_CFLAGS = @WARN_CFLAGS@ libsox_la_LDFLAGS = @APP_LDFLAGS@ -version-info @SHLIB_VERSION@ \ - -export-symbols-regex '^(sox_.*|lsx_(check_read_params|(close|open)_dllibrary|(debug(_more|_most)?|fail|report|warn)_impl|eof|error|fail_errno|filelength|find_(enum_(text|value)|file_extension)|flush|getopt(_init)?|lpc10_(create_(de|en)coder_state|(de|en)code)|raw(read|write)|read(_b_buf|buf|chars)|realloc|rewind|seeki|sigfigs3p?|strcasecmp|tell|unreadb|write(b|_b_buf|buf|s)))$$' + -export-symbols-regex '^(sox_.*|lsx_(([cm]|re)alloc|check_read_params|(close|open)_dllibrary|(debug(_more|_most)?|fail|report|warn)_impl|eof|error|fail_errno|filelength|find_(enum_(text|value)|file_extension)|flush|getopt(_init)?|lpc10_(create_(de|en)coder_state|(de|en)code)|raw(read|write)|read(_b_buf|buf|chars)|rewind|seeki|sigfigs3p?|strcasecmp|strdup|tell|unreadb|write(b|_b_buf|buf|s)))$$' if HAVE_WIN32_LTDL libsox_la_SOURCES += win32-ltdl.c win32-ltdl.h diff --git a/src/xmalloc.c b/src/xmalloc.c index 9bf15969..56fe6944 100644 --- a/src/xmalloc.c +++ b/src/xmalloc.c @@ -20,6 +20,16 @@ #include "sox_i.h" #include +static void *lsx_checkptr(void *ptr) +{ + if (!ptr) { + lsx_fail("out of memory"); + exit(2); + } + + return ptr; +} + /* Resize an allocated memory area; abort if not possible. * * For malloc, `If the size of the space requested is zero, the behavior is @@ -34,10 +44,20 @@ void *lsx_realloc(void *ptr, size_t newsize) return NULL; } - if ((ptr = realloc(ptr, newsize)) == NULL) { - lsx_fail("out of memory"); - exit(2); - } + return lsx_checkptr(realloc(ptr, newsize)); +} + +void *lsx_malloc(size_t size) +{ + return lsx_checkptr(malloc(size + !size)); +} + +void *lsx_calloc(size_t n, size_t size) +{ + return lsx_checkptr(calloc(n + !n, size + !size)); +} - return ptr; +char *lsx_strdup(const char *s) +{ + return lsx_checkptr(strdup(s)); } diff --git a/src/xmalloc.h b/src/xmalloc.h index 9ee77f63..92ac64d9 100644 --- a/src/xmalloc.h +++ b/src/xmalloc.h @@ -23,10 +23,11 @@ #include #include -#define lsx_malloc(size) lsx_realloc(NULL, (size)) -#define lsx_calloc(n,s) (((n)*(s))? memset(lsx_malloc((n)*(s)),0,(n)*(s)) : NULL) +LSX_RETURN_VALID void *lsx_malloc(size_t size); +LSX_RETURN_VALID void *lsx_calloc(size_t n, size_t size); +LSX_RETURN_VALID char *lsx_strdup(const char *s); + #define lsx_Calloc(v,n) v = lsx_calloc(n,sizeof(*(v))) -#define lsx_strdup(p) ((p)? strcpy((char *)lsx_malloc(strlen(p) + 1), p) : NULL) #define lsx_memdup(p,s) ((p)? memcpy(lsx_malloc(s), p, s) : NULL) #define lsx_valloc(v,n) v = lsx_malloc((n)*sizeof(*(v))) #define lsx_revalloc(v,n) v = lsx_realloc(v, (n)*sizeof(*(v))) -- 2.22.0