]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - string.c
string: Introduce arr_zalloc().
[paraslash.git] / string.c
index f01c83929850d7873516f38dbed6864c458406de..7c8b2f0073d2b66d918cdb07908306ee61596246 100644 (file)
--- a/string.c
+++ b/string.c
@@ -62,6 +62,43 @@ __must_check __malloc void *arr_alloc(size_t nmemb, size_t size)
        return arr_realloc(NULL, nmemb, size);
 }
 
+/**
+ * Allocate and initialize an array, abort on failure or bugs.
+ *
+ * \param nmemb See \ref arr_realloc().
+ * \param size See \ref arr_realloc().
+ *
+ * This calls \ref arr_alloc() and zeroes-out the array.
+ *
+ * \return See \ref arr_alloc().
+ */
+__must_check __malloc void *arr_zalloc(size_t nmemb, size_t size)
+{
+       void *ptr = arr_alloc(nmemb, size);
+
+       /*
+        * This multiplication can not overflow because the above call to \ref
+        * arr_alloc() aborts on overflow.
+        */
+       memset(ptr, 0, nmemb * size);
+       return ptr;
+}
+
+/**
+ * Allocate and initialize memory.
+ *
+ * \param size The desired new size.
+ *
+ * \return A pointer to the allocated and zeroed-out memory, which is suitably
+ * aligned for any kind of variable.
+ *
+ * \sa \ref alloc(), calloc(3).
+ */
+__must_check void *zalloc(size_t size)
+{
+       return arr_zalloc(1, size);
+}
+
 /**
  * Paraslash's version of realloc().
  *
@@ -98,24 +135,6 @@ __must_check __malloc void *alloc(size_t size)
        return arr_alloc(1, size);
 }
 
-/**
- * Allocate and initialize memory.
- *
- * \param size The desired new size.
- *
- * \return A pointer to the allocated and zeroed-out memory, which is suitably
- * aligned for any kind of variable.
- *
- * \sa \ref alloc(), calloc(3).
- */
-__must_check __malloc void *zalloc(size_t size)
-{
-       void *ret = alloc(size);
-
-       memset(ret, 0, size);
-       return ret;
-}
-
 /**
  * Paraslash's version of strdup().
  *