ref-filter: factor ref_array pushing into its own function

In preparation for callers constructing their own ref_array structs, let's move our own internal push operation into its own function. While we're at it, we can replace REALLOC_ARRAY() with ALLOC_GROW(), which should give the growth operation amortized linear complexity (as opposed to growing by one, which is potentially quadratic, though in-place realloc growth often makes this faster in practice). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Apr 6, 2018 at 14:59 UTC 427cbc9dbfeb7c96bb1d5d9ace722353e2a5438e
2 files changed +21 -3
ref-filter.c
+13 -3
@@ -1840,6 +1840,18 @@ static struct ref_array_item *new_ref_array_item(const char *refname,
1840 return ref;
1841 }
1842
1843 +struct ref_array_item *ref_array_push(struct ref_array *array,
1844 + const char *refname,
1845 + const struct object_id *oid)
1846 +{
1847 + struct ref_array_item *ref = new_ref_array_item(refname, oid);
1848 +
1849 + ALLOC_GROW(array->items, array->nr + 1, array->alloc);
1850 + array->items[array->nr++] = ref;
1851 +
1852 + return ref;
1853 +}
1854 +
1855 static int ref_kind_from_refname(const char *refname)
1856 {
1857 unsigned int i;
@@ -1930,13 +1942,11 @@ static int ref_filter_handler(const char *refname, const struct object_id *oid,
1942 * to do its job and the resulting list may yet to be pruned
1943 * by maxcount logic.
1944 */
1933 - ref = new_ref_array_item(refname, oid);
1945 + ref = ref_array_push(ref_cbdata->array, refname, oid);
1946 ref->commit = commit;
1947 ref->flag = flag;
1948 ref->kind = kind;
1949
1938 - REALLOC_ARRAY(ref_cbdata->array->items, ref_cbdata->array->nr + 1);
1939 - ref_cbdata->array->items[ref_cbdata->array->nr++] = ref;
1950 return 0;
1951 }
1952
ref-filter.h
+8
@@ -135,4 +135,12 @@ void setup_ref_filter_porcelain_msg(void);
135 void pretty_print_ref(const char *name, const struct object_id *oid,
136 const struct ref_format *format);
137
138 +/*
139 + * Push a single ref onto the array; this can be used to construct your own
140 + * ref_array without using filter_refs().
141 + */
142 +struct ref_array_item *ref_array_push(struct ref_array *array,
143 + const char *refname,
144 + const struct object_id *oid);
145 +
146 #endif /* REF_FILTER_H */