Line | Count | Source |
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 | 62.3k | #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 | 13.8k | { |
46 | 13.8k | struct array_list *arr; |
47 | | |
48 | 13.8k | if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *)) |
49 | 0 | return NULL; |
50 | 13.8k | arr = (struct array_list *)malloc(sizeof(struct array_list)); |
51 | 13.8k | if (!arr) |
52 | 0 | return NULL; |
53 | 13.8k | arr->size = initial_size; |
54 | 13.8k | arr->length = 0; |
55 | 13.8k | arr->free_fn = free_fn; |
56 | 13.8k | if (!(arr->array = (void **)malloc(arr->size * sizeof(void *)))) |
57 | 0 | { |
58 | 0 | free(arr); |
59 | 0 | return NULL; |
60 | 0 | } |
61 | 13.8k | return arr; |
62 | 13.8k | } |
63 | | |
64 | | extern void array_list_free(struct array_list *arr) |
65 | 13.8k | { |
66 | 13.8k | size_t i; |
67 | 51.7k | for (i = 0; i < arr->length; i++) |
68 | 37.8k | if (arr->array[i]) |
69 | 0 | arr->free_fn(arr->array[i]); |
70 | 13.8k | free(arr->array); |
71 | 13.8k | free(arr); |
72 | 13.8k | } |
73 | | |
74 | | void *array_list_get_idx(struct array_list *arr, size_t i) |
75 | 115k | { |
76 | 115k | if (i >= arr->length) |
77 | 0 | return NULL; |
78 | 115k | return arr->array[i]; |
79 | 115k | } |
80 | | |
81 | | static int array_list_expand_internal(struct array_list *arr, size_t max) |
82 | 38.0k | { |
83 | 38.0k | void *t; |
84 | 38.0k | size_t new_size; |
85 | | |
86 | | /* The array can already hold max entries when max == arr->size, since |
87 | | * the caller only ever indexes up to max - 1. |
88 | | */ |
89 | 38.0k | if (max <= arr->size) |
90 | 37.8k | return 0; |
91 | | /* Avoid undefined behaviour on size_t overflow */ |
92 | 167 | if (arr->size >= SIZE_T_MAX / 2) |
93 | 0 | new_size = max; |
94 | 167 | else |
95 | 167 | { |
96 | 167 | new_size = arr->size << 1; |
97 | 167 | if (new_size < max) |
98 | 0 | new_size = max; |
99 | 167 | } |
100 | 167 | if (new_size > (~((size_t)0)) / sizeof(void *)) |
101 | 0 | return -1; |
102 | 167 | if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
103 | 0 | return -1; |
104 | 167 | arr->array = (void **)t; |
105 | 167 | arr->size = new_size; |
106 | 167 | return 0; |
107 | 167 | } |
108 | | |
109 | | int array_list_shrink(struct array_list *arr, size_t empty_slots) |
110 | 10.3k | { |
111 | 10.3k | void *t; |
112 | 10.3k | size_t new_size; |
113 | | |
114 | 10.3k | if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length) |
115 | 0 | return -1; |
116 | 10.3k | new_size = arr->length + empty_slots; |
117 | 10.3k | if (new_size == arr->size) |
118 | 4 | return 0; |
119 | 10.3k | if (new_size > arr->size) |
120 | 0 | return array_list_expand_internal(arr, new_size); |
121 | 10.3k | if (new_size == 0) |
122 | 403 | new_size = 1; |
123 | | |
124 | 10.3k | if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
125 | 0 | return -1; |
126 | 10.3k | arr->array = (void **)t; |
127 | 10.3k | arr->size = new_size; |
128 | 10.3k | return 0; |
129 | 10.3k | } |
130 | | |
131 | | int array_list_insert_idx(struct array_list *arr, size_t idx, void *data) |
132 | 0 | { |
133 | 0 | size_t move_amount; |
134 | |
|
135 | 0 | if (idx >= arr->length) |
136 | 0 | return array_list_put_idx(arr, idx, data); |
137 | | |
138 | | /* we're at full size, what size_t can support */ |
139 | 0 | if (arr->length == SIZE_T_MAX) |
140 | 0 | return -1; |
141 | | |
142 | 0 | if (array_list_expand_internal(arr, arr->length + 1)) |
143 | 0 | return -1; |
144 | | |
145 | 0 | move_amount = (arr->length - idx) * sizeof(void *); |
146 | 0 | memmove(arr->array + idx + 1, arr->array + idx, move_amount); |
147 | 0 | arr->array[idx] = data; |
148 | 0 | arr->length++; |
149 | 0 | return 0; |
150 | 0 | } |
151 | | |
152 | | //static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
153 | | int array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
154 | 0 | { |
155 | 0 | if (idx > SIZE_T_MAX - 1) |
156 | 0 | return -1; |
157 | 0 | if (array_list_expand_internal(arr, idx + 1)) |
158 | 0 | return -1; |
159 | 0 | if (idx < arr->length && arr->array[idx]) |
160 | 0 | arr->free_fn(arr->array[idx]); |
161 | 0 | arr->array[idx] = data; |
162 | 0 | if (idx > arr->length) |
163 | 0 | { |
164 | | /* Zero out the arraylist slots in between the old length |
165 | | and the newly added entry so we know those entries are |
166 | | empty. |
167 | | e.g. when setting array[7] in an array that used to be |
168 | | only 5 elements longs, array[5] and array[6] need to be |
169 | | set to 0. |
170 | | */ |
171 | 0 | memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *)); |
172 | 0 | } |
173 | 0 | if (arr->length <= idx) |
174 | 0 | arr->length = idx + 1; |
175 | 0 | return 0; |
176 | 0 | } |
177 | | |
178 | | int array_list_set_idx(struct array_list *arr, size_t idx, void *data) |
179 | 38.0k | { |
180 | 38.0k | if (idx >= arr->length) |
181 | 0 | return -1; |
182 | 38.0k | arr->array[idx] = data; |
183 | 38.0k | return 0; |
184 | 38.0k | } |
185 | | |
186 | | int array_list_add(struct array_list *arr, void *data) |
187 | 38.0k | { |
188 | | /* Repeat some of array_list_put_idx() so we can skip several |
189 | | checks that we know are unnecessary when appending at the end |
190 | | */ |
191 | 38.0k | size_t idx = arr->length; |
192 | 38.0k | if (idx > SIZE_T_MAX - 1) |
193 | 0 | return -1; |
194 | 38.0k | if (array_list_expand_internal(arr, idx + 1)) |
195 | 0 | return -1; |
196 | 38.0k | arr->array[idx] = data; |
197 | 38.0k | arr->length++; |
198 | 38.0k | return 0; |
199 | 38.0k | } |
200 | | |
201 | | void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *)) |
202 | 0 | { |
203 | 0 | qsort(arr->array, arr->length, sizeof(arr->array[0]), compar); |
204 | 0 | } |
205 | | |
206 | | void *array_list_bsearch(const void **key, struct array_list *arr, |
207 | | int (*compar)(const void *, const void *)) |
208 | 0 | { |
209 | 0 | return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar); |
210 | 0 | } |
211 | | |
212 | | size_t array_list_length(struct array_list *arr) |
213 | 108k | { |
214 | 108k | return arr->length; |
215 | 108k | } |
216 | | |
217 | | int array_list_del_idx(struct array_list *arr, size_t idx, size_t count) |
218 | 48 | { |
219 | 48 | size_t i, stop; |
220 | | |
221 | | /* Avoid overflow in calculation with large indices. */ |
222 | 48 | if (idx > SIZE_T_MAX - count) |
223 | 0 | return -1; |
224 | 48 | stop = idx + count; |
225 | 48 | if (idx >= arr->length || stop > arr->length) |
226 | 0 | return -1; |
227 | 164 | for (i = idx; i < stop; ++i) |
228 | 116 | { |
229 | | // Because put_idx can skip entries, we need to check if |
230 | | // there's actually anything in each slot we're erasing. |
231 | 116 | if (arr->array[i]) |
232 | 0 | arr->free_fn(arr->array[i]); |
233 | 116 | } |
234 | 48 | memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *)); |
235 | 48 | arr->length -= count; |
236 | 48 | return 0; |
237 | 48 | } |