Skip to content

Commit 46be823

Browse files
peffgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 68f4923 commit 46be823

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

list-objects.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,6 @@ void traverse_commit_list(struct rev_info *revs,
228228
die("unknown pending object %s (%s)",
229229
sha1_to_hex(obj->sha1), name);
230230
}
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);
237232
strbuf_release(&base);
238233
}

object.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,16 @@ void object_array_filter(struct object_array *array,
383383
array->nr = dst;
384384
}
385385

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+
386396
/*
387397
* Return true iff array already contains an entry with name.
388398
*/

object.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,12 @@ void object_array_filter(struct object_array *array,
133133
*/
134134
void object_array_remove_duplicates(struct object_array *array);
135135

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+
136142
void clear_object_flags(unsigned flags);
137143

138144
#endif /* OBJECT_H */

0 commit comments

Comments
 (0)