Skip to content

Commit 68f4923

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

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

object.c

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,16 @@ void add_object_array_with_context(struct object *obj, const char *name, struct
355355
add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
356356
}
357357

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+
358368
void object_array_filter(struct object_array *array,
359369
object_array_each_func_t want, void *cb_data)
360370
{
@@ -367,8 +377,7 @@ void object_array_filter(struct object_array *array,
367377
objects[dst] = objects[src];
368378
dst++;
369379
} else {
370-
if (objects[src].name != object_array_slopbuf)
371-
free(objects[src].name);
380+
object_array_release_entry(&objects[src]);
372381
}
373382
}
374383
array->nr = dst;
@@ -400,8 +409,7 @@ void object_array_remove_duplicates(struct object_array *array)
400409
objects[array->nr] = objects[src];
401410
array->nr++;
402411
} else {
403-
if (objects[src].name != object_array_slopbuf)
404-
free(objects[src].name);
412+
object_array_release_entry(&objects[src]);
405413
}
406414
}
407415
}

0 commit comments

Comments
 (0)