array.c 5.7 KB

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