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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. //static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  112. int array_list_put_idx(struct array_list *arr, size_t idx, void *data)
  113. {
  114. if (idx > SIZE_T_MAX - 1)
  115. return -1;
  116. if (array_list_expand_internal(arr, idx + 1))
  117. return -1;
  118. if (idx < arr->length && arr->array[idx])
  119. arr->free_fn(arr->array[idx]);
  120. arr->array[idx] = data;
  121. if (idx > arr->length)
  122. {
  123. /* Zero out the arraylist slots in between the old length
  124. and the newly added entry so we know those entries are
  125. empty.
  126. e.g. when setting array[7] in an array that used to be
  127. only 5 elements longs, array[5] and array[6] need to be
  128. set to 0.
  129. */
  130. memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *));
  131. }
  132. if (arr->length <= idx)
  133. arr->length = idx + 1;
  134. return 0;
  135. }
  136. int array_list_add(struct array_list *arr, void *data)
  137. {
  138. /* Repeat some of array_list_put_idx() so we can skip several
  139. checks that we know are unnecessary when appending at the end
  140. */
  141. size_t idx = arr->length;
  142. if (idx > SIZE_T_MAX - 1)
  143. return -1;
  144. if (array_list_expand_internal(arr, idx + 1))
  145. return -1;
  146. arr->array[idx] = data;
  147. arr->length++;
  148. return 0;
  149. }
  150. void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
  151. {
  152. qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
  153. }
  154. void *array_list_bsearch(const void **key, struct array_list *arr,
  155. int (*compar)(const void *, const void *))
  156. {
  157. return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
  158. }
  159. size_t array_list_length(struct array_list *arr)
  160. {
  161. return arr->length;
  162. }
  163. int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
  164. {
  165. size_t i, stop;
  166. /* Avoid overflow in calculation with large indices. */
  167. if (idx > SIZE_T_MAX - count)
  168. return -1;
  169. stop = idx + count;
  170. if (idx >= arr->length || stop > arr->length)
  171. return -1;
  172. for (i = idx; i < stop; ++i)
  173. {
  174. // Because put_idx can skip entries, we need to check if
  175. // there's actually anything in each slot we're erasing.
  176. if (arr->array[i])
  177. arr->free_fn(arr->array[i]);
  178. }
  179. memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
  180. arr->length -= count;
  181. return 0;
  182. }