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.8 2005/08/30 13:36:32 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_sizet(size_t a, size_t b, size_t *res)
61 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
63 array_alloc(array * a, size_t size, size_t pos)
65 size_t alloc, pos_plus1 = pos + 1;
74 if (!safemult_sizet(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, size_t 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 on failure (realloc failure, invalid src/dest array) */
146 array_copyb(array * dest, const char *src, size_t len)
148 assert(dest != NULL);
149 assert(src != NULL );
155 return array_catb(dest, src, len);
159 /* copy string to dest */
161 array_copys(array * dest, const char *src)
163 return array_copyb(dest, src, strlen(src));
167 /* append len bytes from src to the array dest.
168 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
170 array_catb(array * dest, const char *src, size_t len)
176 assert(dest != NULL);
188 if (tmp < used || tmp < len) /* integer overflow */
191 if (!array_alloc(dest, 1, tmp))
200 "array_catb(): appending %u bytes to array (now %u bytes in array).",
203 memcpy(ptr + used, src, len);
209 /* append string to dest */
211 array_cats(array * dest, const char *src)
213 return array_catb(dest, src, strlen(src));
217 /* append trailing NUL byte to array */
219 array_cat0(array * a)
221 return array_catb(a, "", 1);
225 /* append trailing NUL byte to array, but do not count it. */
227 array_cat0_temporary(array * a)
229 char *endpos = array_alloc(a, 1, array_bytes(a));
237 /* add contents of array src to array dest. */
239 array_cat(array * dest, const array * const src)
241 if (array_UNUSABLE(src))
244 return array_catb(dest, src->mem, src->used);
248 /* return pointer to the element at pos.
249 return NULL if the array is unallocated, or if pos is larger than
250 the number of elements stored int the array. */
252 array_get(array * a, size_t membersize, size_t pos)
256 assert(membersize > 0);
259 if (array_UNUSABLE(a))
262 if (!safemult_sizet(pos, membersize, &totalsize))
265 if (a->allocated < totalsize)
268 return a->mem + pos * membersize;
273 array_free(array * a)
278 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
279 a->allocated, a->used);
289 array_free_wipe(array * a)
291 if (!array_UNUSABLE(a))
292 memset(a->mem, 0, a->allocated);
299 array_start(const array * const a)
307 array_trunc(array * a)
315 array_truncate(array * a, size_t membersize, size_t len)
319 if (!safemult_sizet(membersize, len, &newlen))
322 if (newlen <= a->allocated)
327 /* move elements starting at pos to beginning of array */
329 array_moveleft(array * a, size_t membersize, size_t pos)
334 assert(membersize > 0);
339 if (!safemult_sizet(membersize, pos, &bytepos)) {
345 return; /* nothing to do */
349 "array_moveleft(): %u bytes used in array, starting at position %u.",
352 if (a->used <= bytepos) {
358 memmove(a->mem, a->mem + bytepos, a->used);