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.7 2005/08/28 12:18:50 fw Exp $";
24 #define array_UNUSABLE(x) ( ! x->mem || (0 == x->allocated) )
26 #define ALIGN_32U(x) ((x | 0x1fU) +1)
27 #define ALIGN_1024U(x) ((x | 0x3ffU) +1)
28 #define ALIGN_4096U(x) ((x | 0xfffU) +1)
32 safemult_uint(unsigned int a, unsigned int b, unsigned int *res)
61 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
63 array_alloc(array * a, unsigned int size, unsigned int pos)
65 unsigned int alloc, pos_plus1 = pos + 1;
66 unsigned int aligned = 0;
74 if (!safemult_uint(size, pos_plus1, &alloc))
77 if (a->allocated < alloc) {
79 aligned = ALIGN_32U(alloc);
82 aligned = ALIGN_1024U(alloc);
84 aligned = ALIGN_4096U(alloc);
88 Log(LOG_DEBUG, "array_alloc(): rounded %u to %u bytes.", alloc, aligned);
91 assert(aligned >= alloc);
93 if (aligned < alloc) /* rounding overflow */
98 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
99 a->allocated, aligned);
102 tmp = realloc(a->mem, alloc);
107 a->allocated = alloc;
109 assert(a->allocated > a->used);
111 memset(a->mem + a->used, 0, a->allocated - a->used);
115 return a->mem + (pos * size);
119 /*return number of initialized ELEMS in a. */
121 array_length(const array * const a, unsigned int membersize)
124 assert(membersize > 0);
126 if (array_UNUSABLE(a))
129 return membersize ? a->used / membersize : 0;
133 /* copy array src to array dest */
135 array_copy(array * dest, const array * const src)
137 if (array_UNUSABLE(src))
140 return array_copyb(dest, src->mem, src->used);
144 /* return false if we could not append src (realloc failure, invalid src/dest array) */
146 array_copyb(array * dest, const char *src, unsigned int len)
148 assert(dest != NULL);
149 assert(src != NULL );
154 if (!array_alloc(dest, 1, len))
158 memcpy(dest->mem, src, len);
161 "array_copyb(): copied %u bytes to array (%u bytes allocated).",
162 len, dest->allocated);
168 /* copy string to dest */
170 array_copys(array * dest, const char *src)
172 return array_copyb(dest, src, strlen(src));
176 /* append len bytes from src to the array dest.
177 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
179 array_catb(array * dest, const char *src, unsigned int len)
185 assert(dest != NULL);
197 if (tmp < used || tmp < len) /* integer overflow */
200 if (!array_alloc(dest, 1, tmp))
209 "array_catb(): appending %u bytes to array (now %u bytes in array).",
212 memcpy(ptr + used, src, len);
218 /* append string to dest */
220 array_cats(array * dest, const char *src)
222 return array_catb(dest, src, strlen(src));
226 /* append trailing NUL byte to array */
228 array_cat0(array * a)
230 return array_catb(a, "", 1);
234 /* append trailing NUL byte to array, but do not count it. */
236 array_cat0_temporary(array * a)
238 char *endpos = array_alloc(a, 1, array_bytes(a));
246 /* add contents of array src to array dest. */
248 array_cat(array * dest, const array * const src)
250 if (array_UNUSABLE(src))
253 return array_catb(dest, src->mem, src->used);
257 /* return pointer to the element at pos.
258 return NULL if the array is unallocated, or if pos is larger than
259 the number of elements stored int the array. */
261 array_get(array * a, unsigned int membersize, unsigned int pos)
263 unsigned int totalsize;
265 assert(membersize > 0);
268 if (array_UNUSABLE(a))
271 if (!safemult_uint(pos, membersize, &totalsize))
274 if (a->allocated < totalsize)
277 return a->mem + pos * membersize;
282 array_free(array * a)
287 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
288 a->allocated, a->used);
298 array_free_wipe(array * a)
300 if (!array_UNUSABLE(a))
301 memset(a->mem, 0, a->allocated);
308 array_start(const array * const a)
316 array_trunc(array * a)
324 array_truncate(array * a, unsigned int membersize, unsigned int len)
328 if (!safemult_uint(membersize, len, &newlen))
331 if (newlen <= a->allocated)
336 /* move elements starting at pos to beginning of array */
338 array_moveleft(array * a, unsigned int membersize, unsigned int pos)
340 unsigned int bytepos;
343 assert(membersize > 0);
348 if (!safemult_uint(membersize, pos, &bytepos)) {
354 return; /* nothing to do */
358 "array_moveleft(): %u bytes used in array, starting at position %u.",
361 if (a->used <= bytepos) {
367 memmove(a->mem, a->mem + bytepos, a->used);