mirror of
https://git.kernel.org/pub/scm/utils/kernel/kmod/kmod.git
synced 2024-11-27 05:43:50 +08:00
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 <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>
This commit is contained in:
parent
f4b858c704
commit
0c2bd66e6b
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user