Skip to content

Commit 8e33678

Browse files
KarthikNayakgitster
authored andcommitted
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 <[email protected]> Mentored-by: Matthieu Moy <[email protected]> Signed-off-by: Karthik Nayak <[email protected]> Reviewed-by: Matthieu Moy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 215b565 commit 8e33678

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

builtin/for-each-ref.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,6 +927,26 @@ static int grab_single_ref(const char *refname, const struct object_id *oid,
927927
return 0;
928928
}
929929

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+
930950
static int cmp_ref_sort(struct ref_sort *s, struct ref_array_item *a, struct ref_array_item *b)
931951
{
932952
struct atom_value *va, *vb;
@@ -1157,5 +1177,6 @@ int cmd_for_each_ref(int argc, const char **argv, const char *prefix)
11571177
maxcount = ref_cbdata.array.nr;
11581178
for (i = 0; i < maxcount; i++)
11591179
show_ref(ref_cbdata.array.items[i], format, quote_style);
1180+
ref_array_clear(&ref_cbdata.array);
11601181
return 0;
11611182
}

0 commit comments

Comments
 (0)