Add zalloc_or_die()
authorFabian Keil <fk@fabiankeil.de>
Fri, 26 Feb 2016 12:29:17 +0000 (12:29 +0000)
committerFabian Keil <fk@fabiankeil.de>
Fri, 26 Feb 2016 12:29:17 +0000 (12:29 +0000)
... which will allow to simplify code paths were zalloc()
failures don't need to be handled gracefully.

miscutil.c
miscutil.h

index aebd55f..f5e647c 100644 (file)
@@ -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
index 6c51ee9..e65a8a2 100644 (file)
@@ -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);