From: Gustavo Sverzut Barbieri Date: Tue, 3 Jan 2012 17:58:24 +0000 (-0200) Subject: utils/array: add array_remove_at() X-Git-Tag: v3~14 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=79b656faeb38fe79c4d0fa258b88a35e90392761;p=thirdparty%2Fkmod.git utils/array: add array_remove_at() remove array element at given position, will be used by depmod. --- diff --git a/libkmod/libkmod-array.c b/libkmod/libkmod-array.c index 8e95692d..5b430b61 100644 --- a/libkmod/libkmod-array.c +++ b/libkmod/libkmod-array.c @@ -90,3 +90,26 @@ void array_sort(struct array *array, int (*cmp)(const void *a, const void *b)) { qsort(array->array, array->count, sizeof(void *), cmp); } + +int array_remove_at(struct array *array, unsigned int pos) +{ + if (array->count <= pos) + return -ENOENT; + + array->count--; + if (pos < array->count) + memmove(array->array + pos, array->array + pos + 1, + sizeof(void *) * (array->count - pos)); + + if (array->count + array->step < array->total) { + size_t new_total = array->total - array->step; + void *tmp = realloc(array->array, sizeof(void *) * new_total); + assert(array->step > 0); + if (tmp == NULL) + return 0; + array->array = tmp; + array->total = new_total; + } + + return 0; +} diff --git a/libkmod/libkmod-array.h b/libkmod/libkmod-array.h index 0cbf65b3..6400993b 100644 --- a/libkmod/libkmod-array.h +++ b/libkmod/libkmod-array.h @@ -18,5 +18,6 @@ int array_append_unique(struct array *array, const void *element); void array_pop(struct array *array); void array_free_array(struct array *array); void array_sort(struct array *array, int (*cmp)(const void *a, const void *b)); +int array_remove_at(struct array *array, unsigned int pos); #endif