for-each-ref: introduce 'ref_array_clear()'

Introduce and implement 'ref_array_clear()' which will free all allocated memory for 'ref_array'. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Matthieu Moy <matthieu.moy@grenoble-inp.fr> Signed-off-by: Karthik Nayak <karthik.188@gmail.com> Reviewed-by: Matthieu Moy <Matthieu.Moy@imag.fr> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Karthik Nayak committed Jun 14, 2015 at 01:07 UTC 8e33678a265011f74a0158134234a41d0f944284
1 file changed +21
builtin/for-each-ref.c
+21
@@ -927,6 +927,26 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
927 return 0;
928 }
929
930 +/* Free memory allocated for a ref_array_item */
931 +static void free_array_item(struct ref_array_item *item)
932 +{
933 + free((char *)item->symref);
934 + free(item->refname);
935 + free(item);
936 +}
937 +
938 +/* Free all memory allocated for ref_array */
939 +void ref_array_clear(struct ref_array *array)
940 +{
941 + int i;
942 +
943 + for (i = 0; i < array->nr; i++)
944 + free_array_item(array->items[i]);
945 + free(array->items);
946 + array->items = NULL;
947 + array->nr = array->alloc = 0;
948 +}
949 +
950 static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b)
951 {
952 struct atom_value *va, *vb;
@@ -1157,5 +1177,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
1177 maxcount = ref_cbdata.array.nr;
1178 for (i = 0; i < maxcount; i++)
1179 show_ref(ref_cbdata.array.items[i], format, quote_style);
1180 + ref_array_clear(&ref_cbdata.array);
1181 return 0;
1182 }