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

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