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.2 2005/07/07 21:26:31 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)
51 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
53 array_alloc(array * a, unsigned int size, unsigned int pos)
55 unsigned int alloc, pos_plus1 = pos + 1;
56 unsigned int aligned = 0;
64 if (!safemult_uint(size, pos_plus1, &alloc))
67 if (a->allocated < alloc) {
69 aligned = ALIGN_32U(alloc);
72 aligned = ALIGN_1024U(alloc);
74 aligned = ALIGN_4096U(alloc);
78 Log(LOG_DEBUG, "Rounded %u to %u byte.", alloc, aligned);
81 assert(aligned >= alloc);
83 if (aligned < alloc) /* rounding overflow */
88 Log(LOG_DEBUG, "array_alloc: changing size from %u to %u byte",
89 a->allocated, aligned);
92 tmp = realloc(a->mem, alloc);
99 assert(a->allocated > a->used);
101 memset(a->mem + a->used, 0, a->allocated - a->used);
105 return a->mem + (pos * size);
109 /*return number of initialized ELEMS in a. */
111 array_length(const array * const a, unsigned int membersize)
116 if (array_UNUSABLE(a))
119 return membersize ? a->used / membersize : 0;
123 /* copy array src to array dest */
125 array_copy(array * dest, const array * const src)
127 if (array_UNUSABLE(src))
130 return array_copyb(dest, src->mem, src->used);
134 /* return false if we could not append src (realloc failure, invalid src/dest array) */
136 array_copyb(array * dest, const char *src, unsigned int len)
144 if (!array_alloc(dest, 1, len))
148 memcpy(dest->mem, src, len);
151 "array_copyb: copied %u bytes to array (%u total bytes allocated)",
152 len, dest->allocated);
158 /* copy string to dest */
160 array_copys(array * dest, const char *src)
162 return array_copyb(dest, src, strlen(src));
166 /* append len bytes from src to the array dest.
167 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
169 array_catb(array * dest, const char *src, unsigned int len)
187 if (tmp < used || tmp < len) /* integer overflow */
190 if (!array_alloc(dest, 1, tmp))
199 "array_catb: appending %u bytes to array (now %u total bytes in array)",
202 memcpy(ptr + used, src, len);
208 /* append string to dest */
210 array_cats(array * dest, const char *src)
212 return array_catb(dest, src, strlen(src));
216 /* append trailing NUL byte to array */
218 array_cat0(array * a)
220 return array_catb(a, "", 1);
224 /* add contents of array src to array dest. */
226 array_cat(array * dest, const array * const src)
228 if (array_UNUSABLE(src))
231 return array_catb(dest, src->mem, src->used);
235 /* return pointer to the element at pos.
236 return NULL if the array is unallocated, or if pos is larger than
237 the number of elements stored int the array. */
239 array_get(array * a, unsigned int membersize, unsigned int pos)
241 unsigned int totalsize;
246 if (array_UNUSABLE(a))
249 if (!safemult_uint(pos, membersize, &totalsize))
252 if (a->allocated < totalsize)
255 return a->mem + pos * membersize;
260 array_free(array * a)
265 "array_free: %u bytes free'd (%u bytes still used at time of free())",
266 a->allocated, a->used);
276 array_free_wipe(array * a)
278 if (!array_UNUSABLE(a))
279 memset(a->mem, 0, a->allocated);
286 array_start(const array * const a)
294 array_trunc(array * a)
302 array_truncate(array * a, unsigned int membersize, unsigned int len)
306 if (!safemult_uint(membersize, len, &newlen))
309 if (newlen <= a->allocated)
314 /* move elements starting at pos to beginning of array */
316 array_moveleft(array * a, unsigned int membersize, unsigned int pos)
318 unsigned int bytepos;
326 if (!safemult_uint(membersize, pos, &bytepos)) {
332 return; /* nothing to do */
336 "array_moveleft: %u used bytes in array, move to beginning, starting at pos %u",
339 if (a->used <= bytepos) {
345 memmove(a->mem, a->mem + bytepos, a->used);