From: Karthik Nayak Date: Sat, 06 Jun 2015 13:48:10 GMT Subject: [PATCH v6 05/11] for-each-ref: introduce 'ref_array_clear()' Message-ID: <1433598496-31287-5-git-send-email-karthik.188@gmail.com> In-Reply-To: <1433598496-31287-1-git-send-email-karthik.188@gmail.com> Introduce and implement 'ref_array_clear()' which will free all allocated memory for 'ref_array'. Mentored-by: Christian Couder Mentored-by: Matthieu Moy Signed-off-by: Karthik Nayak --- builtin/for-each-ref.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/builtin/for-each-ref.c b/builtin/for-each-ref.c index 42a89cd..a65a4db 100644 --- a/builtin/for-each-ref.c +++ b/builtin/for-each-ref.c @@ -926,6 +926,26 @@ static int grab_single_ref(const char *refname, const struct object_id *oid, return 0; } +/* Free memory allocated for a ref_array_item */ +static void free_array_item(struct ref_array_item *item) +{ + free(item->symref); + free(item->refname); + free(item); +} + +/* Free all memory allocated for ref_array */ +void ref_array_clear(struct ref_array *array) +{ + int i; + + for (i = 0; i < array->nr; i++) + free_array_item(array->items[i]); + free(array->items); + array->items = NULL; + array->nr = array->alloc = 0; +} + static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b) { struct atom_value *va, *vb; @@ -1156,5 +1176,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix) maxcount = ref_cbdata.array.nr; for (i = 0; i < maxcount; i++) show_ref(ref_cbdata.array.items[i], format, quote_style); + ref_array_clear(&ref_cbdata.array); return 0; } -- 2.4.2