• source navigation  • diff markup  • identifier search  • freetext search  • 

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

This page was automatically generated by LXR 0.3.1.  •  OpenWrt