array.c 6.3 KB

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