array.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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.2.3 2007/04/03 22:08:52 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 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
  50. return NULL;
  51. if (a->allocated < alloc) {
  52. if (alloc < 128) {
  53. aligned = ALIGN_32U(alloc);
  54. } else {
  55. if (alloc < 4096) {
  56. aligned = ALIGN_1024U(alloc);
  57. } else {
  58. aligned = ALIGN_4096U(alloc);
  59. }
  60. }
  61. #ifdef DEBUG_ARRAY
  62. Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned);
  63. #endif
  64. assert(aligned >= alloc);
  65. if (aligned < alloc) /* rounding overflow */
  66. return NULL;
  67. alloc = aligned;
  68. #ifdef DEBUG_ARRAY
  69. Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
  70. a->allocated, aligned);
  71. #endif
  72. tmp = realloc(a->mem, alloc);
  73. if (!tmp)
  74. return NULL;
  75. a->mem = tmp;
  76. a->allocated = alloc;
  77. assert(a->allocated > a->used);
  78. memset(a->mem + a->used, 0, a->allocated - a->used);
  79. a->used = alloc;
  80. }
  81. return a->mem + (pos * size);
  82. }
  83. /*return number of initialized ELEMS in a. */
  84. size_t
  85. array_length(const array * const a, size_t membersize)
  86. {
  87. assert(a != NULL);
  88. assert(membersize > 0);
  89. if (array_UNUSABLE(a))
  90. return 0;
  91. return membersize ? a->used / membersize : 0;
  92. }
  93. /* copy array src to array dest */
  94. bool
  95. array_copy(array * dest, const array * const src)
  96. {
  97. if (array_UNUSABLE(src))
  98. return false;
  99. return array_copyb(dest, src->mem, src->used);
  100. }
  101. /* return false on failure (realloc failure, invalid src/dest array) */
  102. bool
  103. array_copyb(array * dest, const char *src, size_t len)
  104. {
  105. assert(dest != NULL);
  106. assert(src != NULL );
  107. if (!src || !dest)
  108. return false;
  109. array_trunc(dest);
  110. return array_catb(dest, src, len);
  111. }
  112. /* copy string to dest */
  113. bool
  114. array_copys(array * dest, const char *src)
  115. {
  116. return array_copyb(dest, src, strlen(src));
  117. }
  118. /* append len bytes from src to the array dest.
  119. return false if we could not append all bytes (realloc failure, invalid src/dest array) */
  120. bool
  121. array_catb(array * dest, const char *src, size_t len)
  122. {
  123. size_t tmp;
  124. size_t used;
  125. char *ptr;
  126. assert(dest != NULL);
  127. assert(src != NULL);
  128. if (!len)
  129. return true;
  130. if (!src || !dest)
  131. return false;
  132. used = dest->used;
  133. tmp = used + len;
  134. if (tmp < used || tmp < len) /* integer overflow */
  135. return false;
  136. if (!array_alloc(dest, 1, tmp))
  137. return false;
  138. ptr = dest->mem;
  139. assert(ptr != NULL);
  140. #ifdef DEBUG_ARRAY
  141. Log(LOG_DEBUG,
  142. "array_catb(): appending %u bytes to array (now %u bytes in array).",
  143. len, tmp);
  144. #endif
  145. memcpy(ptr + used, src, len);
  146. dest->used = tmp;
  147. return true;
  148. }
  149. /* append string to dest */
  150. bool
  151. array_cats(array * dest, const char *src)
  152. {
  153. return array_catb(dest, src, strlen(src));
  154. }
  155. /* append trailing NUL byte to array */
  156. bool
  157. array_cat0(array * a)
  158. {
  159. return array_catb(a, "", 1);
  160. }
  161. /* append trailing NUL byte to array, but do not count it. */
  162. bool
  163. array_cat0_temporary(array * a)
  164. {
  165. char *endpos = array_alloc(a, 1, array_bytes(a));
  166. if (!endpos)
  167. return false;
  168. *endpos = '\0';
  169. return true;
  170. }
  171. /* add contents of array src to array dest. */
  172. bool
  173. array_cat(array * dest, const array * const src)
  174. {
  175. if (array_UNUSABLE(src))
  176. return false;
  177. return array_catb(dest, src->mem, src->used);
  178. }
  179. /* return pointer to the element at pos.
  180. return NULL if the array is unallocated, or if pos is larger than
  181. the number of elements stored int the array. */
  182. void *
  183. array_get(array * a, size_t membersize, size_t pos)
  184. {
  185. size_t totalsize;
  186. size_t posplus1 = pos + 1;
  187. assert(membersize > 0);
  188. assert(a != NULL);
  189. if (!posplus1 || array_UNUSABLE(a))
  190. return NULL;
  191. if (!safemult_sizet(posplus1, membersize, &totalsize))
  192. return NULL;
  193. if (a->allocated < totalsize)
  194. return NULL;
  195. totalsize = pos * membersize;
  196. return a->mem + totalsize;
  197. }
  198. void
  199. array_free(array * a)
  200. {
  201. assert(a != NULL);
  202. #ifdef DEBUG_ARRAY
  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_start(const array * const a)
  214. {
  215. assert(a != NULL);
  216. return a->mem;
  217. }
  218. void
  219. array_trunc(array * a)
  220. {
  221. assert(a != NULL);
  222. a->used = 0;
  223. }
  224. void
  225. array_truncate(array * a, size_t membersize, size_t len)
  226. {
  227. size_t newlen;
  228. assert(a != NULL);
  229. if (!safemult_sizet(membersize, len, &newlen))
  230. return;
  231. if (newlen <= a->allocated)
  232. a->used = newlen;
  233. }
  234. /* move elements starting at pos to beginning of array */
  235. void
  236. array_moveleft(array * a, size_t membersize, size_t pos)
  237. {
  238. size_t bytepos;
  239. assert(a != NULL);
  240. assert(membersize > 0);
  241. if (!safemult_sizet(membersize, pos, &bytepos)) {
  242. a->used = 0;
  243. return;
  244. }
  245. if (!bytepos)
  246. return; /* nothing to do */
  247. #ifdef DEBUG_ARRAY
  248. Log(LOG_DEBUG,
  249. "array_moveleft(): %u bytes used in array, starting at position %u.",
  250. a->used, bytepos);
  251. #endif
  252. if (a->used <= bytepos) {
  253. a->used = 0;
  254. return;
  255. }
  256. a->used -= bytepos;
  257. memmove(a->mem, a->mem + bytepos, a->used);
  258. }
  259. /* -eof- */