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)
15 static char UNUSED id[] = "$Id: array.c,v 1.15 2007/11/18 15:05:35 alex Exp $";
24 /* Enable more Debug messages in alloc / append / memmove code. */
25 /* #define DEBUG_ARRAY */
29 #define array_UNUSABLE(x) ( !(x)->mem || (0 == (x)->allocated) )
31 #define ALIGN_32U(x) (((x)+(unsigned)31 ) & ~((unsigned)31))
32 #define ALIGN_1024U(x) (((x)+(unsigned)1023) & ~((unsigned)1023))
33 #define ALIGN_4096U(x) (((x)+(unsigned)4095) & ~((unsigned)4095))
37 safemult_sizet(size_t a, size_t b, size_t *res)
41 if (b && (tmp / b != a))
59 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
61 array_alloc(array * a, size_t size, size_t pos)
63 size_t alloc, pos_plus1 = pos + 1;
69 if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
72 if (a->allocated < alloc) {
74 aligned = ALIGN_32U(alloc);
77 aligned = ALIGN_1024U(alloc);
79 aligned = ALIGN_4096U(alloc);
83 Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned);
86 assert(aligned >= alloc);
88 if (aligned < alloc) /* rounding overflow */
93 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
94 a->allocated, aligned);
97 tmp = realloc(a->mem, alloc);
102 a->allocated = alloc;
104 assert(a->allocated > a->used);
106 memset(a->mem + a->used, 0, a->allocated - a->used);
110 return a->mem + (pos * size);
114 /*return number of initialized ELEMS in a. */
116 array_length(const array * const a, size_t membersize)
119 assert(membersize > 0);
121 if (array_UNUSABLE(a))
124 return membersize ? a->used / membersize : 0;
128 /* copy array src to array dest */
130 array_copy(array * dest, const array * const src)
132 if (array_UNUSABLE(src))
135 return array_copyb(dest, src->mem, src->used);
139 /* return false on failure (realloc failure, invalid src/dest array) */
141 array_copyb(array * dest, const char *src, size_t len)
143 assert(dest != NULL);
144 assert(src != NULL );
150 return array_catb(dest, src, len);
154 /* copy string to dest */
156 array_copys(array * dest, const char *src)
158 return array_copyb(dest, src, strlen(src));
162 /* append len bytes from src to the array dest.
163 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
165 array_catb(array * dest, const char *src, size_t len)
171 assert(dest != NULL);
183 if (tmp < used || tmp < len) /* integer overflow */
186 if (!array_alloc(dest, 1, tmp))
195 "array_catb(): appending %u bytes to array (now %u bytes in array).",
198 memcpy(ptr + used, src, len);
204 /* append string to dest */
206 array_cats(array * dest, const char *src)
208 return array_catb(dest, src, strlen(src));
212 /* append trailing NUL byte to array */
214 array_cat0(array * a)
216 return array_catb(a, "", 1);
220 /* append trailing NUL byte to array, but do not count it. */
222 array_cat0_temporary(array * a)
224 char *endpos = array_alloc(a, 1, array_bytes(a));
232 /* add contents of array src to array dest. */
234 array_cat(array * dest, const array * const src)
236 if (array_UNUSABLE(src))
239 return array_catb(dest, src->mem, src->used);
243 /* return pointer to the element at pos.
244 return NULL if the array is unallocated, or if pos is larger than
245 the number of elements stored int the array. */
247 array_get(array * a, size_t membersize, size_t pos)
250 size_t posplus1 = pos + 1;
252 assert(membersize > 0);
255 if (!posplus1 || array_UNUSABLE(a))
258 if (!safemult_sizet(posplus1, membersize, &totalsize))
261 if (a->allocated < totalsize)
264 totalsize = pos * membersize;
265 return a->mem + totalsize;
270 array_free(array * a)
275 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
276 a->allocated, a->used);
285 array_free_wipe(array *a)
287 size_t bytes = a->allocated;
289 memset(a->mem, 0, bytes);
294 array_start(const array * const a)
302 array_trunc(array * a)
310 array_truncate(array * a, size_t membersize, size_t len)
314 if (!safemult_sizet(membersize, len, &newlen))
317 if (newlen <= a->allocated)
322 /* move elements starting at pos to beginning of array */
324 array_moveleft(array * a, size_t membersize, size_t pos)
329 assert(membersize > 0);
331 if (!safemult_sizet(membersize, pos, &bytepos)) {
337 return; /* nothing to do */
341 "array_moveleft(): %u bytes used in array, starting at position %u.",
344 if (a->used <= bytepos) {
350 memmove(a->mem, a->mem + bytepos, a->used);