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.
8 * functions to dynamically allocate arrays.
9 * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
22 /* Enable more Debug messages in alloc / append / memmove code. */
23 /* #define DEBUG_ARRAY */
27 #define array_UNUSABLE(x) ( !(x)->mem )
29 safemult_sizet(size_t a, size_t b, size_t *res)
33 if (b && (tmp / b != a))
51 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
53 array_alloc(array * a, size_t size, size_t pos)
55 size_t alloc, pos_plus1 = pos + 1;
60 if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
63 if (a->allocated < alloc) {
65 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
68 tmp = realloc(a->mem, alloc);
74 memset(a->mem + a->used, 0, a->allocated - a->used);
78 assert(a->allocated >= a->used);
80 return a->mem + (pos * size);
84 /*return number of initialized ELEMS in a. */
86 array_length(const array * const a, size_t membersize)
89 assert(membersize > 0);
91 if (array_UNUSABLE(a))
95 return membersize ? a->used / membersize : 0;
99 /* copy array src to array dest */
101 array_copy(array * dest, const array * const src)
103 if (array_UNUSABLE(src))
106 assert(src->allocated);
107 return array_copyb(dest, src->mem, src->used);
111 /* return false on failure (realloc failure, invalid src/dest array) */
113 array_copyb(array * dest, const char *src, size_t len)
115 assert(dest != NULL);
116 assert(src != NULL );
122 return array_catb(dest, src, len);
126 /* copy string to dest */
128 array_copys(array * dest, const char *src)
130 return array_copyb(dest, src, strlen(src));
134 /* append len bytes from src to the array dest.
135 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
137 array_catb(array * dest, const char *src, size_t len)
143 assert(dest != NULL);
155 if (tmp < used || tmp < len) /* integer overflow */
158 if (!array_alloc(dest, 1, tmp))
167 "array_catb(): appending %u bytes to array (now %u bytes in array).",
170 memcpy(ptr + used, src, len);
176 /* append string to dest */
178 array_cats(array * dest, const char *src)
180 return array_catb(dest, src, strlen(src));
184 /* append trailing NUL byte to array */
186 array_cat0(array * a)
188 return array_catb(a, "", 1);
192 /* append trailing NUL byte to array, but do not count it. */
194 array_cat0_temporary(array * a)
196 char *endpos = array_alloc(a, 1, array_bytes(a));
204 /* add contents of array src to array dest. */
206 array_cat(array * dest, const array * const src)
208 if (array_UNUSABLE(src))
211 return array_catb(dest, src->mem, src->used);
215 /* return pointer to the element at pos.
216 return NULL if the array is unallocated, or if pos is larger than
217 the number of elements stored int the array. */
219 array_get(array * a, size_t membersize, size_t pos)
222 size_t posplus1 = pos + 1;
224 assert(membersize > 0);
227 if (!posplus1 || array_UNUSABLE(a))
230 if (!safemult_sizet(posplus1, membersize, &totalsize))
233 if (a->allocated < totalsize)
236 totalsize = pos * membersize;
237 return a->mem + totalsize;
242 array_free(array * a)
247 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
248 a->allocated, a->used);
257 array_free_wipe(array *a)
259 size_t bytes = a->allocated;
261 memset(a->mem, 0, bytes);
266 array_start(const array * const a)
274 array_trunc(array * a)
282 array_truncate(array * a, size_t membersize, size_t len)
286 if (!safemult_sizet(membersize, len, &newlen))
289 if (newlen <= a->allocated)
294 /* move elements starting at pos to beginning of array */
296 array_moveleft(array * a, size_t membersize, size_t pos)
301 assert(membersize > 0);
303 if (!safemult_sizet(membersize, pos, &bytepos)) {
309 return; /* nothing to do */
313 "array_moveleft(): %u bytes used in array, starting at position %u.",
316 if (a->used <= bytepos) {
322 memmove(a->mem, a->mem + bytepos, a->used);