Bump to 0.17
[platform/upstream/json-c.git] / arraylist.c
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 #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 {
41         return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE);
42 }
43
44 struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size)
45 {
46         struct array_list *arr;
47
48         if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *))
49                 return NULL;
50         arr = (struct array_list *)malloc(sizeof(struct array_list));
51         if (!arr)
52                 return NULL;
53         arr->size = initial_size;
54         arr->length = 0;
55         arr->free_fn = free_fn;
56         if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
57         {
58                 free(arr);
59                 return NULL;
60         }
61         return arr;
62 }
63
64 extern void array_list_free(struct array_list *arr)
65 {
66         size_t i;
67         for (i = 0; i < arr->length; i++)
68                 if (arr->array[i])
69                         arr->free_fn(arr->array[i]);
70         free(arr->array);
71         free(arr);
72 }
73
74 void *array_list_get_idx(struct array_list *arr, size_t i)
75 {
76         if (i >= arr->length)
77                 return NULL;
78         return arr->array[i];
79 }
80
81 static int array_list_expand_internal(struct array_list *arr, size_t max)
82 {
83         void *t;
84         size_t new_size;
85
86         if (max < arr->size)
87                 return 0;
88         /* Avoid undefined behaviour on size_t overflow */
89         if (arr->size >= SIZE_T_MAX / 2)
90                 new_size = max;
91         else
92         {
93                 new_size = arr->size << 1;
94                 if (new_size < max)
95                         new_size = max;
96         }
97         if (new_size > (~((size_t)0)) / sizeof(void *))
98                 return -1;
99         if (!(t = realloc(arr->array, new_size * sizeof(void *))))
100                 return -1;
101         arr->array = (void **)t;
102         arr->size = new_size;
103         return 0;
104 }
105
106 int array_list_shrink(struct array_list *arr, size_t empty_slots)
107 {
108         void *t;
109         size_t new_size;
110
111         if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length)
112                 return -1;
113         new_size = arr->length + empty_slots;
114         if (new_size == arr->size)
115                 return 0;
116         if (new_size > arr->size)
117                 return array_list_expand_internal(arr, new_size);
118         if (new_size == 0)
119                 new_size = 1;
120
121         if (!(t = realloc(arr->array, new_size * sizeof(void *))))
122                 return -1;
123         arr->array = (void **)t;
124         arr->size = new_size;
125         return 0;
126 }
127
128 int array_list_insert_idx(struct array_list *arr, size_t idx, void *data)
129 {
130         size_t move_amount;
131
132         if (idx >= arr->length)
133                 return array_list_put_idx(arr, idx, data);
134
135         /* we're at full size, what size_t can support */
136         if (arr->length == SIZE_T_MAX)
137                 return -1;
138
139         if (array_list_expand_internal(arr, arr->length + 1))
140                 return -1;
141
142         move_amount = (arr->length - idx) * sizeof(void *);
143         memmove(arr->array + idx + 1, arr->array + idx, move_amount);
144         arr->array[idx] = data;
145         arr->length++;
146         return 0;
147 }
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 {
152         if (idx > SIZE_T_MAX - 1)
153                 return -1;
154         if (array_list_expand_internal(arr, idx + 1))
155                 return -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)
160         {
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                 memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
169         }
170         if (arr->length <= idx)
171                 arr->length = idx + 1;
172         return 0;
173 }
174
175 int array_list_add(struct array_list *arr, void *data)
176 {
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         size_t idx = arr->length;
181         if (idx > SIZE_T_MAX - 1)
182                 return -1;
183         if (array_list_expand_internal(arr, idx + 1))
184                 return -1;
185         arr->array[idx] = data;
186         arr->length++;
187         return 0;
188 }
189
190 void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
191 {
192         qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
193 }
194
195 void *array_list_bsearch(const void **key, struct array_list *arr,
196                          int (*compar)(const void *, const void *))
197 {
198         return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
199 }
200
201 size_t array_list_length(struct array_list *arr)
202 {
203         return arr->length;
204 }
205
206 int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
207 {
208         size_t i, stop;
209
210         /* Avoid overflow in calculation with large indices. */
211         if (idx > SIZE_T_MAX - count)
212                 return -1;
213         stop = idx + count;
214         if (idx >= arr->length || stop > arr->length)
215                 return -1;
216         for (i = idx; i < stop; ++i)
217         {
218                 // Because put_idx can skip entries, we need to check if
219                 // there's actually anything in each slot we're erasing.
220                 if (arr->array[i])
221                         arr->free_fn(arr->array[i]);
222         }
223         memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
224         arr->length -= count;
225         return 0;
226 }