object_array: factor out slopbuf-freeing logic
This is not a lot of code, but it's a logical construct that should not need to be repeated (and we are about to add a third repetition). 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
68f492359e29bbdf633201406d0646deee2b298c
1 file changed
+12
-4
object.c
+12
-4
@@ -355,6 +355,16 @@ void add_object_array_with_context(struct object *obj, const char *name, struct
355
add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
356
}
357
358
+/*
359
+ * Free all memory associated with an entry; the result is
360
+ * in an unspecified state and should not be examined.
361
+ */
362
+static void object_array_release_entry(struct object_array_entry *ent)
363
+{
364
+ if (ent->name != object_array_slopbuf)
365
+ free(ent->name);
366
+}
367
+
368
void object_array_filter(struct object_array *array,
369
object_array_each_func_t want, void *cb_data)
370
{
@@ -367,8 +377,7 @@ void object_array_filter(struct object_array *array,
377
objects[dst] = objects[src];
378
dst++;
379
} else {
370
- if (objects[src].name != object_array_slopbuf)
371
- free(objects[src].name);
380
+ object_array_release_entry(&objects[src]);
381
}
382
}
383
array->nr = dst;
@@ -400,8 +409,7 @@ void object_array_remove_duplicates(struct object_array *array)
409
objects[array->nr] = objects[src];
410
array->nr++;
411
} else {
403
- if (objects[src].name != object_array_slopbuf)
404
- free(objects[src].name);
412
+ object_array_release_entry(&objects[src]);
413
}
414
}
415
}