You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

arraylist.c 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. #include "config.h"
  12. #include <limits.h>
  13. #ifdef STDC_HEADERS
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #endif /* STDC_HEADERS */
  17. #if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD)
  18. #include <strings.h>
  19. #endif /* HAVE_STRINGS_H */
  20. #ifndef SIZE_T_MAX
  21. #if SIZEOF_SIZE_T == SIZEOF_INT
  22. #define SIZE_T_MAX UINT_MAX
  23. #elif SIZEOF_SIZE_T == SIZEOF_LONG
  24. #define SIZE_T_MAX ULONG_MAX
  25. #elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG
  26. #define SIZE_T_MAX ULLONG_MAX
  27. #else
  28. #error Unable to determine size of size_t
  29. #endif
  30. #endif
  31. #include "arraylist.h"
  32. struct array_list *array_list_new(array_list_free_fn *free_fn)
  33. {
  34. return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE);
  35. }
  36. struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size)
  37. {
  38. struct array_list *arr;
  39. if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *))
  40. return NULL;
  41. arr = (struct array_list *)malloc(sizeof(struct array_list));
  42. if (!arr)
  43. return NULL;
  44. arr->size = initial_size;
  45. arr->length = 0;
  46. arr->free_fn = free_fn;
  47. if (!(arr->array = (void **)malloc(arr->size * sizeof(void *))))
  48. {
  49. free(arr);
  50. return NULL;
  51. }
  52. return arr;
  53. }
  54. extern void array_list_free(struct array_list *arr)
  55. {
  56. size_t i;
  57. for (i = 0; i < arr->length; i++)
  58. if (arr->array[i])
  59. arr->free_fn(arr->array[i]);
  60. free(arr->array);
  61. free(arr);
  62. }
  63. void *array_list_get_idx(struct array_list *arr, size_t i)
  64. {
  65. if (i >= arr->length)
  66. return NULL;
  67. return arr->array[i];
  68. }
  69. static int array_list_expand_internal(struct array_list *arr, size_t max)
  70. {
  71. void *t;
  72. size_t new_size;
  73. if (max < arr->size)
  74. return 0;
  75. /* Avoid undefined behaviour on size_t overflow */
  76. if (arr->size >= SIZE_T_MAX / 2)
  77. new_size = max;
  78. else
  79. {
  80. new_size = arr->size << 1;
  81. if (new_size < max)
  82. new_size = max;
  83. }
  84. if (new_size > (~((size_t)0)) / sizeof(void *))
  85. return -1;
  86. if (!(t = realloc(arr->array, new_size * sizeof(void *))))
  87. return -1;
  88. arr->array = (void **)t;
  89. arr->size = new_size;
  90. return 0;
  91. }
  92. int array_list_shrink(struct array_list *arr, size_t empty_slots)
  93. {
  94. void *t;
  95. size_t new_size;
  96. if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length)
  97. return -1;
  98. new_size = arr->length + empty_slots;
  99. if (new_size == arr->size)
  100. return 0;
  101. if (new_size > arr->size)
  102. return array_list_expand_internal(arr, new_size);
  103. if (new_size == 0)
  104. new_size = 1;
  105. if (!(t = realloc(arr->array, new_size * sizeof(void *))))
  106. return -1;
  107. arr->array = (void **)t;
  108. arr->size = new_size;
  109. return 0;
  110. }
  111. int array_list_insert_idx(struct array_list *arr, size_t idx, void *data)
  112. {
  113. size_t move_amount;
  114. if (idx >= arr->length)
  115. return array_list_put_idx(arr, idx, data);
  116. /* we're at full size, what size_t can support */
  117. if (arr->length == SIZE_T_MAX)
  118. return -1;
  119. if (array_list_expand_internal(arr, arr->length + 1))
  120. return -1;
  121. move_amount = (arr->length - idx) * sizeof(void *);
  122. memmove(arr->array + idx + 1, arr->array + idx, move_amount);
  123. arr->array[idx] = data;
  124. arr->length++;
  125. return 0;
  126. }
  127. //static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  128. int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  129. {
  130. if (idx > SIZE_T_MAX - 1)
  131. return -1;
  132. if (array_list_expand_internal(arr, idx + 1))
  133. return -1;
  134. if (idx < arr->length && arr->array[idx])
  135. arr->free_fn(arr->array[idx]);
  136. arr->array[idx] = data;
  137. if (idx > arr->length)
  138. {
  139. /* Zero out the arraylist slots in between the old length
  140. and the newly added entry so we know those entries are
  141. empty.
  142. e.g. when setting array[7] in an array that used to be
  143. only 5 elements longs, array[5] and array[6] need to be
  144. set to 0.
  145. */
  146. memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
  147. }
  148. if (arr->length <= idx)
  149. arr->length = idx + 1;
  150. return 0;
  151. }
  152. int array_list_add(struct array_list *arr, void *data)
  153. {
  154. /* Repeat some of array_list_put_idx() so we can skip several
  155. checks that we know are unnecessary when appending at the end
  156. */
  157. size_t idx = arr->length;
  158. if (idx > SIZE_T_MAX - 1)
  159. return -1;
  160. if (array_list_expand_internal(arr, idx + 1))
  161. return -1;
  162. arr->array[idx] = data;
  163. arr->length++;
  164. return 0;
  165. }
  166. void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
  167. {
  168. qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
  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. size_t array_list_length(struct array_list *arr)
  176. {
  177. return arr->length;
  178. }
  179. int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
  180. {
  181. size_t i, stop;
  182. /* Avoid overflow in calculation with large indices. */
  183. if (idx > SIZE_T_MAX - count)
  184. return -1;
  185. stop = idx + count;
  186. if (idx >= arr->length || stop > arr->length)
  187. return -1;
  188. for (i = idx; i < stop; ++i)
  189. {
  190. // Because put_idx can skip entries, we need to check if
  191. // there's actually anything in each slot we're erasing.
  192. if (arr->array[i])
  193. arr->free_fn(arr->array[i]);
  194. }
  195. memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
  196. arr->length -= count;
  197. return 0;
  198. }