Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
3 | | * |
4 | | * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
5 | | * Michael Clark <michael@metaparadigm.com> |
6 | | * |
7 | | * This library is free software; you can redistribute it and/or modify |
8 | | * it under the terms of the MIT license. See COPYING for details. |
9 | | * |
10 | | */ |
11 | | |
12 | | #include "config.h" |
13 | | |
14 | | #include <limits.h> |
15 | | |
16 | | #ifdef STDC_HEADERS |
17 | | #include <stdlib.h> |
18 | | #include <string.h> |
19 | | #endif /* STDC_HEADERS */ |
20 | | |
21 | | #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD) |
22 | | #include <strings.h> |
23 | | #endif /* HAVE_STRINGS_H */ |
24 | | |
25 | | #ifndef SIZE_T_MAX |
26 | | #if SIZEOF_SIZE_T == SIZEOF_INT |
27 | | #define SIZE_T_MAX UINT_MAX |
28 | | #elif SIZEOF_SIZE_T == SIZEOF_LONG |
29 | 74.7k | #define SIZE_T_MAX ULONG_MAX |
30 | | #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG |
31 | | #define SIZE_T_MAX ULLONG_MAX |
32 | | #else |
33 | | #error Unable to determine size of size_t |
34 | | #endif |
35 | | #endif |
36 | | |
37 | | #include "arraylist.h" |
38 | | |
39 | | struct array_list *array_list_new(array_list_free_fn *free_fn) |
40 | 0 | { |
41 | 0 | return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE); |
42 | 0 | } |
43 | | |
44 | | struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size) |
45 | 15.0k | { |
46 | 15.0k | struct array_list *arr; |
47 | | |
48 | 15.0k | if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *)) |
49 | 0 | return NULL; |
50 | 15.0k | arr = (struct array_list *)malloc(sizeof(struct array_list)); |
51 | 15.0k | if (!arr) |
52 | 0 | return NULL; |
53 | 15.0k | arr->size = initial_size; |
54 | 15.0k | arr->length = 0; |
55 | 15.0k | arr->free_fn = free_fn; |
56 | 15.0k | if (!(arr->array = (void **)malloc(arr->size * sizeof(void *)))) |
57 | 0 | { |
58 | 0 | free(arr); |
59 | 0 | return NULL; |
60 | 0 | } |
61 | 15.0k | return arr; |
62 | 15.0k | } |
63 | | |
64 | | extern void array_list_free(struct array_list *arr) |
65 | 15.0k | { |
66 | 15.0k | size_t i; |
67 | 63.0k | for (i = 0; i < arr->length; i++) |
68 | 48.0k | if (arr->array[i]) |
69 | 47.9k | arr->free_fn(arr->array[i]); |
70 | 15.0k | free(arr->array); |
71 | 15.0k | free(arr); |
72 | 15.0k | } |
73 | | |
74 | | void *array_list_get_idx(struct array_list *arr, size_t i) |
75 | 93.3k | { |
76 | 93.3k | if (i >= arr->length) |
77 | 0 | return NULL; |
78 | 93.3k | return arr->array[i]; |
79 | 93.3k | } |
80 | | |
81 | | static int array_list_expand_internal(struct array_list *arr, size_t max) |
82 | 48.0k | { |
83 | 48.0k | void *t; |
84 | 48.0k | size_t new_size; |
85 | | |
86 | 48.0k | if (max < arr->size) |
87 | 47.7k | return 0; |
88 | | /* Avoid undefined behaviour on size_t overflow */ |
89 | 266 | if (arr->size >= SIZE_T_MAX / 2) |
90 | 0 | new_size = max; |
91 | 266 | else |
92 | 266 | { |
93 | 266 | new_size = arr->size << 1; |
94 | 266 | if (new_size < max) |
95 | 0 | new_size = max; |
96 | 266 | } |
97 | 266 | if (new_size > (~((size_t)0)) / sizeof(void *)) |
98 | 0 | return -1; |
99 | 266 | if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
100 | 0 | return -1; |
101 | 266 | arr->array = (void **)t; |
102 | 266 | arr->size = new_size; |
103 | 266 | return 0; |
104 | 266 | } |
105 | | |
106 | | int array_list_shrink(struct array_list *arr, size_t empty_slots) |
107 | 11.4k | { |
108 | 11.4k | void *t; |
109 | 11.4k | size_t new_size; |
110 | | |
111 | 11.4k | if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length) |
112 | 0 | return -1; |
113 | 11.4k | new_size = arr->length + empty_slots; |
114 | 11.4k | if (new_size == arr->size) |
115 | 0 | return 0; |
116 | 11.4k | if (new_size > arr->size) |
117 | 0 | return array_list_expand_internal(arr, new_size); |
118 | 11.4k | if (new_size == 0) |
119 | 298 | new_size = 1; |
120 | | |
121 | 11.4k | if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
122 | 0 | return -1; |
123 | 11.4k | arr->array = (void **)t; |
124 | 11.4k | arr->size = new_size; |
125 | 11.4k | return 0; |
126 | 11.4k | } |
127 | | |
128 | | int array_list_insert_idx(struct array_list *arr, size_t idx, void *data) |
129 | 0 | { |
130 | 0 | size_t move_amount; |
131 | |
|
132 | 0 | if (idx >= arr->length) |
133 | 0 | return array_list_put_idx(arr, idx, data); |
134 | | |
135 | | /* we're at full size, what size_t can support */ |
136 | 0 | if (arr->length == SIZE_T_MAX) |
137 | 0 | return -1; |
138 | | |
139 | 0 | if (array_list_expand_internal(arr, arr->length + 1)) |
140 | 0 | return -1; |
141 | | |
142 | 0 | move_amount = (arr->length - idx) * sizeof(void *); |
143 | 0 | memmove(arr->array + idx + 1, arr->array + idx, move_amount); |
144 | 0 | arr->array[idx] = data; |
145 | 0 | arr->length++; |
146 | 0 | return 0; |
147 | 0 | } |
148 | | |
149 | | //static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
150 | | int array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
151 | 0 | { |
152 | 0 | if (idx > SIZE_T_MAX - 1) |
153 | 0 | return -1; |
154 | 0 | if (array_list_expand_internal(arr, idx + 1)) |
155 | 0 | return -1; |
156 | 0 | if (idx < arr->length && arr->array[idx]) |
157 | 0 | arr->free_fn(arr->array[idx]); |
158 | 0 | arr->array[idx] = data; |
159 | 0 | if (idx > arr->length) |
160 | 0 | { |
161 | | /* Zero out the arraylist slots in between the old length |
162 | | and the newly added entry so we know those entries are |
163 | | empty. |
164 | | e.g. when setting array[7] in an array that used to be |
165 | | only 5 elements longs, array[5] and array[6] need to be |
166 | | set to 0. |
167 | | */ |
168 | 0 | memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *)); |
169 | 0 | } |
170 | 0 | if (arr->length <= idx) |
171 | 0 | arr->length = idx + 1; |
172 | 0 | return 0; |
173 | 0 | } |
174 | | |
175 | | int array_list_add(struct array_list *arr, void *data) |
176 | 48.0k | { |
177 | | /* Repeat some of array_list_put_idx() so we can skip several |
178 | | checks that we know are unnecessary when appending at the end |
179 | | */ |
180 | 48.0k | size_t idx = arr->length; |
181 | 48.0k | if (idx > SIZE_T_MAX - 1) |
182 | 0 | return -1; |
183 | 48.0k | if (array_list_expand_internal(arr, idx + 1)) |
184 | 0 | return -1; |
185 | 48.0k | arr->array[idx] = data; |
186 | 48.0k | arr->length++; |
187 | 48.0k | return 0; |
188 | 48.0k | } |
189 | | |
190 | | void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *)) |
191 | 0 | { |
192 | 0 | qsort(arr->array, arr->length, sizeof(arr->array[0]), compar); |
193 | 0 | } |
194 | | |
195 | | void *array_list_bsearch(const void **key, struct array_list *arr, |
196 | | int (*compar)(const void *, const void *)) |
197 | 0 | { |
198 | 0 | return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar); |
199 | 0 | } |
200 | | |
201 | | size_t array_list_length(struct array_list *arr) |
202 | 116k | { |
203 | 116k | return arr->length; |
204 | 116k | } |
205 | | |
206 | | int array_list_del_idx(struct array_list *arr, size_t idx, size_t count) |
207 | 0 | { |
208 | 0 | size_t i, stop; |
209 | | |
210 | | /* Avoid overflow in calculation with large indices. */ |
211 | 0 | if (idx > SIZE_T_MAX - count) |
212 | 0 | return -1; |
213 | 0 | stop = idx + count; |
214 | 0 | if (idx >= arr->length || stop > arr->length) |
215 | 0 | return -1; |
216 | 0 | for (i = idx; i < stop; ++i) |
217 | 0 | { |
218 | | // Because put_idx can skip entries, we need to check if |
219 | | // there's actually anything in each slot we're erasing. |
220 | 0 | if (arr->array[i]) |
221 | 0 | arr->free_fn(arr->array[i]); |
222 | 0 | } |
223 | 0 | memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *)); |
224 | 0 | arr->length -= count; |
225 | 0 | return 0; |
226 | 0 | } |