list-objects-filter-options: clean up use of ALLOC_GROW

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>

Matthew DeVore committed Jun 27, 2019 at 15:54 UTC 5a133e8a7f7c28c57f7a0a85e57692b8b781d896
2 files changed +29 -10
cache.h
+22
@@ -660,6 +660,9 @@ int daemonize(void);
660 * at least 'nr' entries; the number of entries currently allocated
661 * is 'alloc', using the standard growing factor alloc_nr() macro.
662 *
663 + * Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
664 + * added niceties.
665 + *
666 * DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
667 */
668 #define ALLOC_GROW(x, nr, alloc) \
@@ -673,6 +676,25 @@ int daemonize(void);
676 } \
677 } while (0)
678
679 +/*
680 + * Similar to ALLOC_GROW but handles updating of the nr value and
681 + * zeroing the bytes of the newly-grown array elements.
682 + *
683 + * DO NOT USE any expression with side-effect for any of the
684 + * arguments.
685 + */
686 +#define ALLOC_GROW_BY(x, nr, increase, alloc) \
687 + do { \
688 + if (increase) { \
689 + size_t new_nr = nr + (increase); \
690 + if (new_nr < nr) \
691 + BUG("negative growth in ALLOC_GROW_BY"); \
692 + ALLOC_GROW(x, new_nr, alloc); \
693 + memset((x) + nr, 0, sizeof(*(x)) * (increase)); \
694 + nr = new_nr; \
695 + } \
696 + } while (0)
697 +
698 /* Initialize and use the cache information */
699 struct lock_file;
700 void preload_index(struct index_state *index,
list-objects-filter-options.c
+7 -10
@@ -120,14 +120,12 @@ static int parse_combine_subfilter(
120 struct strbuf *subspec,
121 struct strbuf *errbuf)
122 {
123 - size_t new_index = filter_options->sub_nr++;
123 + size_t new_index = filter_options->sub_nr;
124 char *decoded;
125 int result;
126
127 - ALLOC_GROW(filter_options->sub, filter_options->sub_nr,
128 - filter_options->sub_alloc);
129 - memset(&filter_options->sub[new_index], 0,
130 - sizeof(*filter_options->sub));
127 + ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
128 + filter_options->sub_alloc);
129
130 decoded = url_percent_decode(subspec->buf);
131
@@ -255,13 +253,12 @@ int parse_list_objects_filter(
253
254 string_list_append(&filter_options->filter_spec, xstrdup("+"));
255 filter_spec_append_urlencode(filter_options, arg);
258 - ALLOC_GROW(filter_options->sub, filter_options->sub_nr + 1,
259 - filter_options->sub_alloc);
260 - filter_options = &filter_options->sub[filter_options->sub_nr++];
261 - memset(filter_options, 0, sizeof(*filter_options));
256 + ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
257 + filter_options->sub_alloc);
258
259 parse_error = gently_parse_list_objects_filter(
264 - filter_options, arg, &errbuf);
260 + &filter_options->sub[filter_options->sub_nr - 1], arg,
261 + &errbuf);
262 }
263 if (parse_error)
264 die("%s", errbuf.buf);