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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 *)calloc(1, 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 **)calloc(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. (void)memset(arr->array + arr->size, 0, (new_size - arr->size) * sizeof(void *));
  84. arr->size = new_size;
  85. return 0;
  86. }
  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 (arr->length <= idx)
  97. arr->length = idx + 1;
  98. return 0;
  99. }
  100. int array_list_add(struct array_list *arr, void *data)
  101. {
  102. return array_list_put_idx(arr, arr->length, data);
  103. }
  104. void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *))
  105. {
  106. qsort(arr->array, arr->length, sizeof(arr->array[0]), compar);
  107. }
  108. void *array_list_bsearch(const void **key, struct array_list *arr,
  109. int (*compar)(const void *, const void *))
  110. {
  111. return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar);
  112. }
  113. size_t array_list_length(struct array_list *arr)
  114. {
  115. return arr->length;
  116. }
  117. int array_list_del_idx(struct array_list *arr, size_t idx, size_t count)
  118. {
  119. size_t i, stop;
  120. /* Avoid overflow in calculation with large indices. */
  121. if (idx > SIZE_T_MAX - count)
  122. return -1;
  123. stop = idx + count;
  124. if (idx >= arr->length || stop > arr->length)
  125. return -1;
  126. for (i = idx; i < stop; ++i)
  127. {
  128. if (arr->array[i])
  129. arr->free_fn(arr->array[i]);
  130. }
  131. memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *));
  132. arr->length -= count;
  133. return 0;
  134. }