Skip to content

Commit d82be52

Browse files
matvoregitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0c2309 commit d82be52

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

cache.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,9 @@ int daemonize(void);
660660
* at least 'nr' entries; the number of entries currently allocated
661661
* is 'alloc', using the standard growing factor alloc_nr() macro.
662662
*
663+
* Consider using ALLOC_GROW_BY instead of ALLOC_GROW as it has some
664+
* added niceties.
665+
*
663666
* DO NOT USE any expression with side-effect for 'x', 'nr', or 'alloc'.
664667
*/
665668
#define ALLOC_GROW(x, nr, alloc) \
@@ -673,6 +676,25 @@ int daemonize(void);
673676
} \
674677
} while (0)
675678

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+
676698
/* Initialize and use the cache information */
677699
struct lock_file;
678700
void preload_index(struct index_state *index,

list-objects-filter-options.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,14 +119,12 @@ static int parse_combine_subfilter(
119119
struct strbuf *subspec,
120120
struct strbuf *errbuf)
121121
{
122-
size_t new_index = filter_options->sub_nr++;
122+
size_t new_index = filter_options->sub_nr;
123123
char *decoded;
124124
int result;
125125

126-
ALLOC_GROW(filter_options->sub, filter_options->sub_nr,
127-
filter_options->sub_alloc);
128-
memset(&filter_options->sub[new_index], 0,
129-
sizeof(*filter_options->sub));
126+
ALLOC_GROW_BY(filter_options->sub, filter_options->sub_nr, 1,
127+
filter_options->sub_alloc);
130128

131129
decoded = url_percent_decode(subspec->buf);
132130

@@ -255,13 +253,12 @@ int parse_list_objects_filter(
255253

256254
string_list_append(&filter_options->filter_spec, xstrdup("+"));
257255
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);
262258

263259
parse_error = gently_parse_list_objects_filter(
264-
filter_options, arg, &errbuf);
260+
&filter_options->sub[filter_options->sub_nr - 1], arg,
261+
&errbuf);
265262
}
266263
if (parse_error)
267264
die("%s", errbuf.buf);

0 commit comments

Comments
 (0)