]> git.ipfire.org Git - thirdparty/git.git/commitdiff
list-objects-filter-options: clean up use of ALLOC_GROW
authorMatthew DeVore <matvore@google.com>
Thu, 27 Jun 2019 22:54:13 +0000 (15:54 -0700)
committerJunio C Hamano <gitster@pobox.com>
Fri, 28 Jun 2019 15:41:53 +0000 (08:41 -0700)
Introduce a new macro ALLOC_GROW_BY which automatically zeros the added
array elements and takes care of updating the nr value. Use the macro in
code introduced earlier in this patchset.

Signed-off-by: Matthew DeVore <matvore@google.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
cache.h
list-objects-filter-options.c

diff --git a/cache.h b/cache.h
index bf20337ef435234acf171f76137e683e66fb22d2..cf5d70c19622c6087be53f569c6ab8e9720fdb67 100644 (file)
--- a/cache.h
+++ b/cache.h
@@ -660,6 +660,9 @@ int daemonize(void);
  * at least 'nr' entries; the number of entries currently allocated
  * is 'alloc', using the standard growing factor alloc_nr() macro.
  *
+ * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
+ * added niceties.
+ *
  * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
  */
 #define ALLOC_GROW(x, nr, alloc) \
@@ -673,6 +676,25 @@ int daemonize(void);
                } \
        } while (0)
 
+/*
+ * Similar to ALLOC_GROW but handles updating of the nr value and
+ * zeroing the bytes of the newly-grown array elements.
+ *
+ * DO NOT USE any expression with side-effect for any of the
+ * arguments.
+ */
+#define ALLOC_GROW_BY(x, nr, increase, alloc) \
+       do { \
+               if (increase) { \
+                       size_t new_nr = nr + (increase); \
+                       if (new_nr < nr) \
+                               BUG("negative growth in ALLOC_GROW_BY"); \
+                       ALLOC_GROW(x, new_nr, alloc); \
+                       memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
+                       nr = new_nr; \
+               } \
+       } while (0)
+
 /* Initialize and use the cache information */
 struct lock_file;
 void preload_index(struct index_state *index,
index 2506dc8327482f37622b39dfe76ca8555a97214e..44bc1153d121b5bd91c02a1958286042f8256e3f 100644 (file)
@@ -120,14 +120,12 @@ static int parse_combine_subfilter(
        struct strbuf *subspec,
        struct strbuf *errbuf)
 {
-       size_t new_index = filter_options->sub_nr++;
+       size_t new_index = filter_options->sub_nr;
        char *decoded;
        int result;
 
-       ALLOC_GROW(filter_options->sub, filter_options->sub_nr,
-                  filter_options->sub_alloc);
-       memset(&filter_options->sub[new_index], 0,
-              sizeof(*filter_options->sub));
+       ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
+                     filter_options->sub_alloc);
 
        decoded = url_percent_decode(subspec->buf);
 
@@ -255,13 +253,12 @@ int parse_list_objects_filter(
 
                string_list_append(&filter_options->filter_spec, xstrdup("+"));
                filter_spec_append_urlencode(filter_options, arg);
-               ALLOC_GROW(filter_options->sub, filter_options->sub_nr + 1,
-                          filter_options->sub_alloc);
-               filter_options = &filter_options->sub[filter_options->sub_nr++];
-               memset(filter_options, 0, sizeof(*filter_options));
+               ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
+                             filter_options->sub_alloc);
 
                parse_error = gently_parse_list_objects_filter(
-                       filter_options, arg, &errbuf);
+                       &filter_options->sub[filter_options->sub_nr - 1], arg,
+                       &errbuf);
        }
        if (parse_error)
                die("%s", errbuf.buf);