object_array: add a "clear" function

There's currently no easy way to free the memory associated with an object_array (and in most cases, we simply leak the memory in a rev_info's pending array). Let's provide a helper to make this easier to handle. We can make use of it in list-objects.c, which does the same thing by hand (but fails to free the "name" field of each entry, potentially leaking memory). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Oct 15, 2014 at 18:34 UTC 46be823124bb6a6ff0e06dc19c327b599ed97c72
3 files changed +17 -6
list-objects.c
+1 -6
@@ -228,11 +228,6 @@ void traverse_commit_list(struct rev_info *revs,
228 die("unknown pending object %s (%s)",
229 sha1_to_hex(obj->sha1), name);
230 }
231 - if (revs->pending.nr) {
232 - free(revs->pending.objects);
233 - revs->pending.nr = 0;
234 - revs->pending.alloc = 0;
235 - revs->pending.objects = NULL;
236 - }
231 + object_array_clear(&revs->pending);
232 strbuf_release(&base);
233 }
object.c
+10
@@ -383,6 +383,16 @@ void object_array_filter(struct object_array *array,
383 array->nr = dst;
384 }
385
386 +void object_array_clear(struct object_array *array)
387 +{
388 + int i;
389 + for (i = 0; i < array->nr; i++)
390 + object_array_release_entry(&array->objects[i]);
391 + free(array->objects);
392 + array->objects = NULL;
393 + array->nr = array->alloc = 0;
394 +}
395 +
396 /*
397 * Return true iff array already contains an entry with name.
398 */
object.h
+6
@@ -133,6 +133,12 @@ void object_array_filter(struct object_array *array,
133 */
134 void object_array_remove_duplicates(struct object_array *array);
135
136 +/*
137 + * Remove any objects from the array, freeing all used memory; afterwards
138 + * the array is ready to store more objects with add_object_array().
139 + */
140 +void object_array_clear(struct object_array *array);
141 +
142 void clear_object_flags(unsigned flags);
143
144 #endif /* OBJECT_H */