array.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2 of the License, or
  5. * (at your option) any later version.
  6. * Please read the file COPYING, README and AUTHORS for more information.
  7. *
  8. * libarray - dynamically allocate arrays.
  9. * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
  10. */
  11. /**
  12. * @file
  13. * Functions to dynamically allocate arrays.
  14. */
  15. /* Additionan debug messages related to array handling: 0=off / 1=on */
  16. #define DEBUG_ARRAY 0
  17. #include "array.h"
  18. #include <assert.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #if DEBUG_ARRAY
  23. # include "log.h"
  24. #endif
  25. #define array_UNUSABLE(x) ( !(x)->mem )
  26. static bool
  27. safemult_sizet(size_t a, size_t b, size_t *res)
  28. {
  29. size_t tmp = a * b;
  30. if (b && (tmp / b != a))
  31. return false;
  32. *res = tmp;
  33. return true;
  34. }
  35. void
  36. array_init(array *a)
  37. {
  38. assert(a != NULL);
  39. a->mem = NULL;
  40. a->allocated = 0;
  41. a->used = 0;
  42. }
  43. /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
  44. void *
  45. array_alloc(array * a, size_t size, size_t pos)
  46. {
  47. size_t alloc, pos_plus1 = pos + 1;
  48. char *tmp;
  49. assert(size > 0);
  50. if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
  51. return NULL;
  52. if (a->allocated < alloc) {
  53. #if DEBUG_ARRAY
  54. Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
  55. a->allocated, alloc);
  56. #endif
  57. tmp = realloc(a->mem, alloc);
  58. if (!tmp)
  59. return NULL;
  60. a->mem = tmp;
  61. a->allocated = alloc;
  62. memset(a->mem + a->used, 0, a->allocated - a->used);
  63. a->used = alloc;
  64. }
  65. assert(a->allocated >= a->used);
  66. return a->mem + (pos * size);
  67. }
  68. /*return number of initialized ELEMS in a. */
  69. size_t
  70. array_length(const array * const a, size_t membersize)
  71. {
  72. assert(a != NULL);
  73. assert(membersize > 0);
  74. if (array_UNUSABLE(a))
  75. return 0;
  76. assert(a->allocated);
  77. return membersize ? a->used / membersize : 0;
  78. }
  79. /* copy array src to array dest */
  80. bool
  81. array_copy(array * dest, const array * const src)
  82. {
  83. if (array_UNUSABLE(src))
  84. return false;
  85. assert(src->allocated);
  86. return array_copyb(dest, src->mem, src->used);
  87. }
  88. /* return false on failure (realloc failure, invalid src/dest array) */
  89. bool
  90. array_copyb(array * dest, const char *src, size_t len)
  91. {
  92. assert(dest != NULL);
  93. assert(src != NULL );
  94. if (!src || !dest)
  95. return false;
  96. array_trunc(dest);
  97. return array_catb(dest, src, len);
  98. }
  99. /* copy string to dest */
  100. bool
  101. array_copys(array * dest, const char *src)
  102. {
  103. return array_copyb(dest, src, strlen(src));
  104. }
  105. /* append len bytes from src to the array dest.
  106. return false if we could not append all bytes (realloc failure, invalid src/dest array) */
  107. bool
  108. array_catb(array * dest, const char *src, size_t len)
  109. {
  110. size_t tmp;
  111. size_t used;
  112. char *ptr;
  113. assert(dest != NULL);
  114. assert(src != NULL);
  115. if (!len)
  116. return true;
  117. if (!src || !dest)
  118. return false;
  119. used = dest->used;
  120. tmp = used + len;
  121. if (tmp < used || tmp < len) /* integer overflow */
  122. return false;
  123. if (!array_alloc(dest, 1, tmp))
  124. return false;
  125. ptr = dest->mem;
  126. assert(ptr != NULL);
  127. #if DEBUG_ARRAY
  128. Log(LOG_DEBUG,
  129. "array_catb(): appending %u bytes to array (now %u bytes in array).",
  130. len, tmp);
  131. #endif
  132. memcpy(ptr + used, src, len);
  133. dest->used = tmp;
  134. return true;
  135. }
  136. /* append string to dest */
  137. bool
  138. array_cats(array * dest, const char *src)
  139. {
  140. return array_catb(dest, src, strlen(src));
  141. }
  142. /* append trailing NUL byte to array */
  143. bool
  144. array_cat0(array * a)
  145. {
  146. return array_catb(a, "", 1);
  147. }
  148. /* append trailing NUL byte to array, but do not count it. */
  149. bool
  150. array_cat0_temporary(array * a)
  151. {
  152. char *endpos = array_alloc(a, 1, array_bytes(a));
  153. if (!endpos)
  154. return false;
  155. *endpos = '\0';
  156. return true;
  157. }
  158. /* add contents of array src to array dest. */
  159. bool
  160. array_cat(array * dest, const array * const src)
  161. {
  162. if (array_UNUSABLE(src))
  163. return false;
  164. return array_catb(dest, src->mem, src->used);
  165. }
  166. /* return pointer to the element at pos.
  167. return NULL if the array is unallocated, or if pos is larger than
  168. the number of elements stored int the array. */
  169. void *
  170. array_get(array * a, size_t membersize, size_t pos)
  171. {
  172. size_t totalsize;
  173. size_t posplus1 = pos + 1;
  174. assert(membersize > 0);
  175. assert(a != NULL);
  176. if (!posplus1 || array_UNUSABLE(a))
  177. return NULL;
  178. if (!safemult_sizet(posplus1, membersize, &totalsize))
  179. return NULL;
  180. if (a->allocated < totalsize)
  181. return NULL;
  182. totalsize = pos * membersize;
  183. return a->mem + totalsize;
  184. }
  185. void
  186. array_free(array * a)
  187. {
  188. assert(a != NULL);
  189. #if DEBUG_ARRAY
  190. Log(LOG_DEBUG,
  191. "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
  192. a->allocated, a->used);
  193. #endif
  194. free(a->mem);
  195. a->mem = NULL;
  196. a->allocated = 0;
  197. a->used = 0;
  198. }
  199. void
  200. array_free_wipe(array *a)
  201. {
  202. size_t bytes = a->allocated;
  203. if (bytes)
  204. memset(a->mem, 0, bytes);
  205. array_free(a);
  206. }
  207. void *
  208. array_start(const array * const a)
  209. {
  210. assert(a != NULL);
  211. return a->mem;
  212. }
  213. void
  214. array_trunc(array * a)
  215. {
  216. assert(a != NULL);
  217. a->used = 0;
  218. }
  219. void
  220. array_truncate(array * a, size_t membersize, size_t len)
  221. {
  222. size_t newlen;
  223. assert(a != NULL);
  224. if (!safemult_sizet(membersize, len, &newlen))
  225. return;
  226. if (newlen <= a->allocated)
  227. a->used = newlen;
  228. }
  229. /* move elements starting at pos to beginning of array */
  230. void
  231. array_moveleft(array * a, size_t membersize, size_t pos)
  232. {
  233. size_t bytepos;
  234. assert(a != NULL);
  235. assert(membersize > 0);
  236. if (!safemult_sizet(membersize, pos, &bytepos)) {
  237. a->used = 0;
  238. return;
  239. }
  240. if (!bytepos)
  241. return; /* nothing to do */
  242. #if DEBUG_ARRAY
  243. Log(LOG_DEBUG,
  244. "array_moveleft(): %u bytes used in array, starting at position %u.",
  245. a->used, bytepos);
  246. #endif
  247. if (a->used <= bytepos) {
  248. a->used = 0;
  249. return;
  250. }
  251. a->used -= bytepos;
  252. memmove(a->mem, a->mem + bytepos, a->used);
  253. }
  254. /* -eof- */