object_array: add a "clear" function
authorJeff King <peff@peff.net>
Wed, 15 Oct 2014 22:34:34 +0000 (18:34 -0400)
committerJunio C Hamano <gitster@pobox.com>
Thu, 16 Oct 2014 17:10:37 +0000 (10:10 -0700)
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>
list-objects.c
object.c
object.h
index 3595ee7a22c4d6099ea9b995ffe1c61c60bb77cd..fad6808aad8dd601e0c2e7724a955d64ee73f4e9 100644 (file)
@@ -228,11 +228,6 @@ void traverse_commit_list(struct rev_info *revs,
                die("unknown pending object %s (%s)",
                    sha1_to_hex(obj->sha1), name);
        }
-       if (revs->pending.nr) {
-               free(revs->pending.objects);
-               revs->pending.nr = 0;
-               revs->pending.alloc = 0;
-               revs->pending.objects = NULL;
-       }
+       object_array_clear(&revs->pending);
        strbuf_release(&base);
 }
index 60f4864632870a413da989c6380b3fb1316def67..6aeb1bbbe35b79f834c5506f523beeb4e92d8790 100644 (file)
--- a/object.c
+++ b/object.c
@@ -383,6 +383,16 @@ void object_array_filter(struct object_array *array,
        array->nr = dst;
 }
 
+void object_array_clear(struct object_array *array)
+{
+       int i;
+       for (i = 0; i < array->nr; i++)
+               object_array_release_entry(&array->objects[i]);
+       free(array->objects);
+       array->objects = NULL;
+       array->nr = array->alloc = 0;
+}
+
 /*
  * Return true iff array already contains an entry with name.
  */
index e028ced74c6c50459376d778c8bb9bae1d2808c3..2a755a23734ca8dc8188ce4507259e49bbfdee7b 100644 (file)
--- a/object.h
+++ b/object.h
@@ -133,6 +133,12 @@ void object_array_filter(struct object_array *array,
  */
 void object_array_remove_duplicates(struct object_array *array);
 
+/*
+ * Remove any objects from the array, freeing all used memory; afterwards
+ * the array is ready to store more objects with add_object_array().
+ */
+void object_array_clear(struct object_array *array);
+
 void clear_object_flags(unsigned flags);
 
 #endif /* OBJECT_H */