From: Fabian Keil Date: Mon, 23 Jul 2012 12:41:59 +0000 (+0000) Subject: Add malloc_or_die() which will allow to simplify code paths were malloc() failures... X-Git-Tag: v_3_0_20~315 X-Git-Url: http://www.privoxy.org/gitweb/config.html?a=commitdiff_plain;h=12b699958743ed8cd1a226a75bc42d11669280f7;p=privoxy.git Add malloc_or_die() which will allow to simplify code paths were malloc() failures don't need to be handled gracefully --- diff --git a/miscutil.c b/miscutil.c index 61ef5831..bbfba611 100644 --- a/miscutil.c +++ b/miscutil.c @@ -1,4 +1,4 @@ -const char miscutil_rcs[] = "$Id: miscutil.c,v 1.75 2012/03/09 16:24:36 fabiankeil Exp $"; +const char miscutil_rcs[] = "$Id: miscutil.c,v 1.76 2012/03/09 17:55:50 fabiankeil Exp $"; /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.c,v $ @@ -8,7 +8,7 @@ const char miscutil_rcs[] = "$Id: miscutil.c,v 1.75 2012/03/09 16:24:36 fabianke * to deserve their own file but don't really fit in * any other file. * - * Copyright : Written by and Copyright (C) 2001-2011 the + * Copyright : Written by and Copyright (C) 2001-2012 the * Privoxy team. http://www.privoxy.org/ * * Based on the Internet Junkbuster originally written @@ -129,6 +129,42 @@ char *strdup_or_die(const char *str) } +/********************************************************************* + * + * Function : malloc_or_die + * + * Description : malloc wrapper that either succeeds or causes + * program termination. + * + * Useful in situations were the buffer size is "small" + * and malloc() failures couldn't be handled better + * anyway. In case of debug builds, failures trigger + * an assert(). + * + * Parameters : + * 1 : buffer_size = Size of the space to allocate + * + * Returns : Pointer to newly malloc'd memory + * + *********************************************************************/ +void *malloc_or_die(size_t buffer_size) +{ + char *new_buf; + + new_buf = malloc(buffer_size); + + if (new_buf == NULL) + { + assert(new_buf != NULL); + log_error(LOG_LEVEL_FATAL, "Out of memory in malloc_or_die()."); + exit(1); + } + + return(new_buf); + +} + + #if defined(unix) /********************************************************************* * diff --git a/miscutil.h b/miscutil.h index efb6c23f..bf35238c 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.34 2011/09/04 11:10:56 fabiankeil Exp $" +#define MISCUTIL_H_VERSION "$Id: miscutil.h,v 1.35 2012/03/04 11:52:45 fabiankeil Exp $" /********************************************************************* * * File : $Source: /cvsroot/ijbswa/current/miscutil.h,v $ @@ -47,6 +47,7 @@ extern "C" { extern const char *basedir; extern void *zalloc(size_t size); extern char *strdup_or_die(const char *str); +extern void *malloc_or_die(size_t buffer_size); #if defined(unix) extern void write_pid_file(void);