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 * libarray - dynamically allocate arrays.
9 * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
14 * Functions to dynamically allocate arrays.
17 /* Additionan debug messages related to array handling: 0=off / 1=on */
31 #define array_UNUSABLE(x) ( !(x)->mem )
35 safemult_sizet(size_t a, size_t b, size_t *res)
39 if (b && (tmp / b != a))
57 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
59 array_alloc(array * a, size_t size, size_t pos)
61 size_t alloc, pos_plus1 = pos + 1;
66 if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
69 if (a->allocated < alloc) {
71 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
74 tmp = realloc(a->mem, alloc);
80 memset(a->mem + a->used, 0, a->allocated - a->used);
84 assert(a->allocated >= a->used);
86 return a->mem + (pos * size);
90 /*return number of initialized ELEMS in a. */
92 array_length(const array * const a, size_t membersize)
95 assert(membersize > 0);
97 if (array_UNUSABLE(a))
100 assert(a->allocated);
101 return membersize ? a->used / membersize : 0;
105 /* copy array src to array dest */
107 array_copy(array * dest, const array * const src)
109 if (array_UNUSABLE(src))
112 assert(src->allocated);
113 return array_copyb(dest, src->mem, src->used);
117 /* return false on failure (realloc failure, invalid src/dest array) */
119 array_copyb(array * dest, const char *src, size_t len)
121 assert(dest != NULL);
122 assert(src != NULL );
128 return array_catb(dest, src, len);
132 /* copy string to dest */
134 array_copys(array * dest, const char *src)
136 return array_copyb(dest, src, strlen(src));
140 /* append len bytes from src to the array dest.
141 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
143 array_catb(array * dest, const char *src, size_t len)
149 assert(dest != NULL);
161 if (tmp < used || tmp < len) /* integer overflow */
164 if (!array_alloc(dest, 1, tmp))
173 "array_catb(): appending %u bytes to array (now %u bytes in array).",
176 memcpy(ptr + used, src, len);
182 /* append string to dest */
184 array_cats(array * dest, const char *src)
186 return array_catb(dest, src, strlen(src));
190 /* append trailing NUL byte to array */
192 array_cat0(array * a)
194 return array_catb(a, "", 1);
198 /* append trailing NUL byte to array, but do not count it. */
200 array_cat0_temporary(array * a)
202 char *endpos = array_alloc(a, 1, array_bytes(a));
210 /* add contents of array src to array dest. */
212 array_cat(array * dest, const array * const src)
214 if (array_UNUSABLE(src))
217 return array_catb(dest, src->mem, src->used);
221 /* return pointer to the element at pos.
222 return NULL if the array is unallocated, or if pos is larger than
223 the number of elements stored int the array. */
225 array_get(array * a, size_t membersize, size_t pos)
228 size_t posplus1 = pos + 1;
230 assert(membersize > 0);
233 if (!posplus1 || array_UNUSABLE(a))
236 if (!safemult_sizet(posplus1, membersize, &totalsize))
239 if (a->allocated < totalsize)
242 totalsize = pos * membersize;
243 return a->mem + totalsize;
248 array_free(array * a)
253 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
254 a->allocated, a->used);
263 array_free_wipe(array *a)
265 size_t bytes = a->allocated;
267 memset(a->mem, 0, bytes);
272 array_start(const array * const a)
280 array_trunc(array * a)
288 array_truncate(array * a, size_t membersize, size_t len)
292 if (!safemult_sizet(membersize, len, &newlen))
295 if (newlen <= a->allocated)
300 /* move elements starting at pos to beginning of array */
302 array_moveleft(array * a, size_t membersize, size_t pos)
307 assert(membersize > 0);
309 if (!safemult_sizet(membersize, pos, &bytepos)) {
315 return; /* nothing to do */
319 "array_moveleft(): %u bytes used in array, starting at position %u.",
322 if (a->used <= bytepos) {
328 memmove(a->mem, a->mem + bytepos, a->used);