]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
string: Introduce arr_zalloc().
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 27 Oct 2021 18:40:07 +0000 (20:40 +0200)
committerAndre Noll <maan@tuebingen.mpg.de>
Fri, 29 Jul 2022 17:22:09 +0000 (19:22 +0200)
Adjust all callers which pass a product of two integers to zalloc()
to call the new function instead and reduce zalloc() to a one-line
wrapper.

fecdec_filter.c
play.c
string.c
string.h
write.c

index a720a7a76e13498ef1b0699985968325db10e6ec..a3498e02a08f7fd94f70f516c3b045e7e02d5a0d 100644 (file)
@@ -226,7 +226,7 @@ static int add_slice(char *buf, struct fecdec_group *fg)
        if (fg->num_slices == 0) {
                fg->num_slices = fg->h.slices_per_group;
                fg->idx = arr_alloc(fg->num_slices, sizeof(int));
-               fg->data = zalloc(fg->num_slices * sizeof(unsigned char *));
+               fg->data = arr_zalloc(fg->num_slices, sizeof(unsigned char *));
        }
        r = fg->num_received_slices;
        /* Check if we already have this slice. */
diff --git a/play.c b/play.c
index 926850134cda899268501b7c90f408f7b2b80cb5..69b463ac326e5b1341448bc14b438d6b02b3ce29 100644 (file)
--- a/play.c
+++ b/play.c
@@ -1259,7 +1259,7 @@ int main(int argc, char *argv[])
        session_open();
        num_inputs = lls_num_inputs(play_lpr);
        init_shuffle_map();
-       pt->invalid = zalloc(sizeof(*pt->invalid) * num_inputs);
+       pt->invalid = arr_zalloc(num_inputs, sizeof(*pt->invalid));
        pt->rq = CRT_FILE_CHANGE;
        pt->playing = true;
        pt->task = task_register(&(struct task_info){
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().
  *
index 3efe6878da98906beeda36a8946cb2c886790c88..ef7c3ee860b3f000c1c2936eaa4458c6ec4d9a7d 100644 (file)
--- a/string.h
+++ b/string.h
@@ -72,6 +72,7 @@ __must_check void *para_realloc(void *p, size_t size);
 __must_check __malloc void *alloc(size_t size);
 __must_check __malloc void *zalloc(size_t size);
 __must_check __malloc void *arr_alloc(size_t nmemb, size_t size);
+__must_check __malloc void *arr_zalloc(size_t nmemb, size_t size);
 __must_check __malloc char *para_strdup(const char *s);
 
 __printf_2_0 unsigned xvasprintf(char **result, const char *fmt, va_list ap);
diff --git a/write.c b/write.c
index 9e0f22bdafae79d502ae2628fa1a07d6a1ccaf34..356f35d46c04114881cb2b5516920e2d213b4e0d 100644 (file)
--- a/write.c
+++ b/write.c
@@ -89,7 +89,7 @@ static int setup_and_schedule(struct lls_parse_result *lpr)
        }, &s);
 
        n = writer_given? writer_given : 1;
-       wns = zalloc(n * sizeof(*wns));
+       wns = arr_zalloc(n, sizeof(*wns));
        for (i = 0; i < n; i++) {
                const char *arg = i < writer_given?
                        lls_string_val(i, OPT_RESULT(WRITER, lpr)) : NULL;