]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
utils/array: add array_remove_at()
authorGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Tue, 3 Jan 2012 17:58:24 +0000 (15:58 -0200)
committerGustavo Sverzut Barbieri <barbieri@profusion.mobi>
Tue, 3 Jan 2012 17:58:24 +0000 (15:58 -0200)
remove array element at given position, will be used by depmod.

libkmod/libkmod-array.c
libkmod/libkmod-array.h

index 8e95692d82676e38ac34a6b56bda6c80a04b6f59..5b430b61dc23be217eaa0ed0c3c9d0b7945236a8 100644 (file)
@@ -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;
+}
index 0cbf65b351a443ba9b4a9532b7f65230f5b3548a..6400993b4ed90c7c8670a64903e84882db791746 100644 (file)
@@ -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