On Tue, Jun 11, 2024 at 2:25 PM Victoria Dye via GitGitGadget <gitgitgadget@xxxxxxxxx> wrote: > Replace the static 'struct tree_entry **entries' with a non-static 'struct > tree_entry_array' instance. In later commits, we'll want to be able to > create additional 'struct tree_entry_array' instances utilizing common > functionality (create, push, clear, free). To avoid code duplication, create > the 'struct tree_entry_array' type and add functions that perform those > basic operations. > > Signed-off-by: Victoria Dye <vdye@xxxxxxxxxx> > --- > diff --git a/builtin/mktree.c b/builtin/mktree.c > @@ -12,15 +12,39 @@ > +struct tree_entry_array { > + size_t nr, alloc; > + struct tree_entry **entries; > +}; > > +static void clear_tree_entry_array(struct tree_entry_array *arr) > +{ > + for (size_t i = 0; i < arr->nr; i++) > + FREE_AND_NULL(arr->entries[i]); > + arr->nr = 0; > +} > + > +static void release_tree_entry_array(struct tree_entry_array *arr) > +{ > + FREE_AND_NULL(arr->entries); > + arr->nr = arr->alloc = 0; > +} For robustness, to make it less likely for future code to leak the items pointed to by `arr->entries`, it might make sense for release_tree_entry_array() to call clear_tree_entry_array() before calling FREE_AND_NULL(). > - ALLOC_GROW(entries, used + 1, alloc); > - entries[used++] = ent; > + /* Append the update */ > + tree_entry_array_push(arr, ent); Nit: the new comment seems superfluous