2 * $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $
4 * Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd.
5 * Michael Clark <michael@metaparadigm.com>
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.
19 #endif /* STDC_HEADERS */
21 #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
23 #endif /* HAVE_STRINGS_H */
26 #if SIZEOF_SIZE_T == SIZEOF_INT
27 #define SIZE_T_MAX UINT_MAX
28 #elif SIZEOF_SIZE_T == SIZEOF_LONG
29 #define SIZE_T_MAX ULONG_MAX
30 #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
31 #define SIZE_T_MAX ULLONG_MAX
33 #error Unable to determine size of size_t
37 #include "arraylist.h"
39 struct array_list *array_list_new(array_list_free_fn *free_fn)
41 return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE);
44 struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size)
46 struct array_list *arr;
48 if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *))
50 arr = (struct array_list *)malloc(sizeof(struct array_list));
53 arr->size = initial_size;
55 arr->free_fn = free_fn;
56 if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
64 extern void array_list_free(struct array_list *arr)
67 for (i = 0; i < arr->length; i++)
69 arr->free_fn(arr->array[i]);
74 void *array_list_get_idx(struct array_list *arr, size_t i)
81 static int array_list_expand_internal(struct array_list *arr, size_t max)
88 /* Avoid undefined behaviour on size_t overflow */
89 if (arr->size >= SIZE_T_MAX / 2)
93 new_size = arr->size << 1;
97 if (new_size > (~((size_t)0)) / sizeof(void *))
99 if (!(t = realloc(arr->array, new_size * sizeof(void *))))
101 arr->array = (void **)t;
102 arr->size = new_size;
106 int array_list_shrink(struct array_list *arr, size_t empty_slots)
111 if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length)
113 new_size = arr->length + empty_slots;
114 if (new_size == arr->size)
116 if (new_size > arr->size)
117 return array_list_expand_internal(arr, new_size);
121 if (!(t = realloc(arr->array, new_size * sizeof(void *))))
123 arr->array = (void **)t;
124 arr->size = new_size;
128 int array_list_insert_idx(struct array_list *arr, size_t idx, void *data)
132 if (idx >= arr->length)
133 return array_list_put_idx(arr, idx, data);
135 /* we're at full size, what size_t can support */
136 if (arr->length == SIZE_T_MAX)
139 if (array_list_expand_internal(arr, arr->length + 1))
142 move_amount = (arr->length - idx) * sizeof(void *);
143 memmove(arr->array + idx + 1, arr->array + idx, move_amount);
144 arr->array[idx] = data;
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)
152 if (idx > SIZE_T_MAX - 1)
154 if (array_list_expand_internal(arr, idx + 1))
156 if (idx < arr->length && arr->array[idx])
157 arr->free_fn(arr->array[idx]);
158 arr->array[idx] = data;
159 if (idx > arr->length)
161 /* Zero out the arraylist slots in between the old length
162 and the newly added entry so we know those entries are
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
168 memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
170 if (arr->length <= idx)
171 arr->length = idx + 1;
175 int array_list_add(struct array_list *arr, void *data)
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
180 size_t idx = arr->length;
181 if (idx > SIZE_T_MAX - 1)
183 if (array_list_expand_internal(arr, idx + 1))
185 arr->array[idx] = data;
190 void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
192 qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
195 void *array_list_bsearch(const void **key, struct array_list *arr,
196 int (*compar)(const void *, const void *))
198 return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
201 size_t array_list_length(struct array_list *arr)
206 int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
210 /* Avoid overflow in calculation with large indices. */
211 if (idx > SIZE_T_MAX - count)
214 if (idx >= arr->length || stop > arr->length)
216 for (i = idx; i < stop; ++i)
218 // Because put_idx can skip entries, we need to check if
219 // there's actually anything in each slot we're erasing.
221 arr->free_fn(arr->array[i]);
223 memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
224 arr->length -= count;