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 4.9 kB

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