From 0c2bd66e6b2528e8deee3dd8a53a1360bd3e4773 Mon Sep 17 00:00:00 2001 From: Tobias Stoeckmann Date: Tue, 13 Aug 2024 19:51:24 +0200 Subject: [PATCH] shared: unify array trim handling An error during array_realloc is always ignored if the idea was to save memory by trimming an array. Move two occurrences of same code into a single function for easier reviewing. Signed-off-by: Tobias Stoeckmann Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/68 Signed-off-by: Lucas De Marchi --- shared/array.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/shared/array.c b/shared/array.c index ee776a3a..299021fc 100644 --- a/shared/array.c +++ b/shared/array.c @@ -28,6 +28,14 @@ static int array_realloc(struct array *array, size_t new_total) return 0; } +static void array_trim(struct array *array) +{ + if (array->count + array->step < array->total) { + /* ignore error */ + array_realloc(array, array->total - array->step); + } +} + void array_init(struct array *array, size_t step) { assert(step > 0); @@ -70,11 +78,7 @@ void array_pop(struct array *array) { if (array->count == 0) return; array->count--; - if (array->count + array->step < array->total) { - int r = array_realloc(array, array->total - array->step); - if (r < 0) - return; - } + array_trim(array); } void array_free_array(struct array *array) { @@ -99,13 +103,7 @@ int array_remove_at(struct array *array, size_t pos) if (pos < array->count) memmove(array->array + pos, array->array + pos + 1, sizeof(void *) * (array->count - pos)); - - if (array->count + array->step < array->total) { - int r = array_realloc(array, array->total - array->step); - /* ignore error */ - if (r < 0) - return 0; - } + array_trim(array); return 0; } -- 2.47.3