]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
shared: unify array trim handling
authorTobias Stoeckmann <tobias@stoeckmann.org>
Tue, 13 Aug 2024 17:51:24 +0000 (19:51 +0200)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 16 Aug 2024 05:00:26 +0000 (00:00 -0500)
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 <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/68
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/array.c

index ee776a3a6443dd721489faad33a6c4bdff7d3cdd..299021fcb804e67d784998d8137b39eb3053c142 100644 (file)
@@ -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;
 }