]> git.ipfire.org Git - thirdparty/kmod.git/blob - shared/array.c
865b3e8258e33f0969a867aa3087e59558fa2360
[thirdparty/kmod.git] / shared / array.c
1 /*
2 * libkmod - interface to kernel module operations
3 *
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <assert.h>
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <shared/array.h>
27
28 #include <libkmod.h>
29
30 /* basic pointer array growing in steps */
31
32
33 static int array_realloc(struct array *array, size_t new_total)
34 {
35 void *tmp = realloc(array->array, sizeof(void *) * new_total);
36 if (tmp == NULL)
37 return -ENOMEM;
38 array->array = tmp;
39 array->total = new_total;
40 return 0;
41 }
42
43 void array_init(struct array *array, size_t step)
44 {
45 assert(step > 0);
46 array->array = NULL;
47 array->count = 0;
48 array->total = 0;
49 array->step = step;
50 }
51
52 int array_append(struct array *array, const void *element)
53 {
54 size_t idx;
55
56 if (array->count + 1 >= array->total) {
57 int r = array_realloc(array, array->total + array->step);
58 if (r < 0)
59 return r;
60 }
61 idx = array->count;
62 array->array[idx] = (void *)element;
63 array->count++;
64 return idx;
65 }
66
67 int array_append_unique(struct array *array, const void *element)
68 {
69 void **itr = array->array;
70 void **itr_end = itr + array->count;
71 for (; itr < itr_end; itr++)
72 if (*itr == element)
73 return -EEXIST;
74 return array_append(array, element);
75 }
76
77 void array_pop(struct array *array) {
78 array->count--;
79 if (array->count + array->step < array->total) {
80 int r = array_realloc(array, array->total - array->step);
81 if (r < 0)
82 return;
83 }
84 }
85
86 void array_free_array(struct array *array) {
87 free(array->array);
88 array->count = 0;
89 array->total = 0;
90 }
91
92
93 void array_sort(struct array *array, int (*cmp)(const void *a, const void *b))
94 {
95 qsort(array->array, array->count, sizeof(void *), cmp);
96 }
97
98 int array_remove_at(struct array *array, unsigned int pos)
99 {
100 if (array->count <= pos)
101 return -ENOENT;
102
103 array->count--;
104 if (pos < array->count)
105 memmove(array->array + pos, array->array + pos + 1,
106 sizeof(void *) * (array->count - pos));
107
108 if (array->count + array->step < array->total) {
109 int r = array_realloc(array, array->total - array->step);
110 /* ignore error */
111 if (r < 0)
112 return 0;
113 }
114
115 return 0;
116 }