From: Fabian Keil Date: Fri, 26 Feb 2016 12:29:17 +0000 (+0000) Subject: Add zalloc_or_die() X-Git-Tag: v_3_0_25~146 X-Git-Url: http://www.privoxy.org/gitweb/?p=privoxy.git;a=commitdiff_plain;h=d2170158936d131cf7ec7ce95795d6347ceb93f9 Add zalloc_or_die() ... which will allow to simplify code paths were zalloc() failures don't need to be handled gracefully. --- diff --git a/miscutil.c b/miscutil.c index aebd55f3..f5e647ca 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.79 2015/08/12 10:34:38 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.80 2016/01/16 12:33:36 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -93,6 +93,40 @@ void *zalloc(size_t size) } +/********************************************************************* + * + * Function : zalloc_or_die + * + * Description : zalloc wrapper that either succeeds or causes + * program termination. + * + * Useful in situations were the string length is + * "small" and zalloc() failures couldn't be handled + * better anyway. In case of debug builds, failures + * trigger an assert(). + * + * Parameters : + * 1 : size = Size of memory chunk to return. + * + * Returns : Pointer to newly malloc'd memory chunk. + * + *********************************************************************/ +void *zalloc_or_die(size_t size) +{ + void *buffer; + + buffer = zalloc(size); + if (buffer == NULL) + { + assert(buffer != NULL); + log_error(LOG_LEVEL_FATAL, "Out of memory in zalloc_or_die()."); + exit(1); + } + + return(buffer); + +} + /********************************************************************* * * Function : strdup_or_die diff --git a/miscutil.h b/miscutil.h index 6c51ee95..e65a8a2e 100644 --- a/miscutil.h +++ b/miscutil.h @@ -1,6 +1,6 @@ #ifndef MISCUTIL_H_INCLUDED #define MISCUTIL_H_INCLUDED -#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.36 2012/07/23 12:41:59 fabiankeil Exp $" +#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.37 2012/11/24 13:58:17 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.h,v $ @@ -46,6 +46,7 @@ extern "C" { extern const char *basedir; extern void *zalloc(size_t size); +extern void *zalloc_or_die(size_t size); extern char *strdup_or_die(const char *str); extern void *malloc_or_die(size_t buffer_size);