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.
25 /* Enable more Debug messages in alloc / append / memmove code. */
26 /* #define DEBUG_ARRAY */
28 #define array_UNUSABLE(x) ( !(x)->mem )
32 safemult_sizet(size_t a, size_t b, size_t *res)
36 if (b && (tmp / b != a))
54 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
56 array_alloc(array * a, size_t size, size_t pos)
58 size_t alloc, pos_plus1 = pos + 1;
63 if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
66 if (a->allocated < alloc) {
68 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
71 tmp = realloc(a->mem, alloc);
77 memset(a->mem + a->used, 0, a->allocated - a->used);
81 assert(a->allocated >= a->used);
83 return a->mem + (pos * size);
87 /*return number of initialized ELEMS in a. */
89 array_length(const array * const a, size_t membersize)
92 assert(membersize > 0);
94 if (array_UNUSABLE(a))
98 return membersize ? a->used / membersize : 0;
102 /* copy array src to array dest */
104 array_copy(array * dest, const array * const src)
106 if (array_UNUSABLE(src))
109 assert(src->allocated);
110 return array_copyb(dest, src->mem, src->used);
114 /* return false on failure (realloc failure, invalid src/dest array) */
116 array_copyb(array * dest, const char *src, size_t len)
118 assert(dest != NULL);
119 assert(src != NULL );
125 return array_catb(dest, src, len);
129 /* copy string to dest */
131 array_copys(array * dest, const char *src)
133 return array_copyb(dest, src, strlen(src));
137 /* append len bytes from src to the array dest.
138 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
140 array_catb(array * dest, const char *src, size_t len)
146 assert(dest != NULL);
158 if (tmp < used || tmp < len) /* integer overflow */
161 if (!array_alloc(dest, 1, tmp))
170 "array_catb(): appending %u bytes to array (now %u bytes in array).",
173 memcpy(ptr + used, src, len);
179 /* append string to dest */
181 array_cats(array * dest, const char *src)
183 return array_catb(dest, src, strlen(src));
187 /* append trailing NUL byte to array */
189 array_cat0(array * a)
191 return array_catb(a, "", 1);
195 /* append trailing NUL byte to array, but do not count it. */
197 array_cat0_temporary(array * a)
199 char *endpos = array_alloc(a, 1, array_bytes(a));
207 /* add contents of array src to array dest. */
209 array_cat(array * dest, const array * const src)
211 if (array_UNUSABLE(src))
214 return array_catb(dest, src->mem, src->used);
218 /* return pointer to the element at pos.
219 return NULL if the array is unallocated, or if pos is larger than
220 the number of elements stored int the array. */
222 array_get(array * a, size_t membersize, size_t pos)
225 size_t posplus1 = pos + 1;
227 assert(membersize > 0);
230 if (!posplus1 || array_UNUSABLE(a))
233 if (!safemult_sizet(posplus1, membersize, &totalsize))
236 if (a->allocated < totalsize)
239 totalsize = pos * membersize;
240 return a->mem + totalsize;
245 array_free(array * a)
250 "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
251 a->allocated, a->used);
260 array_free_wipe(array *a)
262 size_t bytes = a->allocated;
264 memset(a->mem, 0, bytes);
269 array_start(const array * const a)
277 array_trunc(array * a)
285 array_truncate(array * a, size_t membersize, size_t len)
289 if (!safemult_sizet(membersize, len, &newlen))
292 if (newlen <= a->allocated)
297 /* move elements starting at pos to beginning of array */
299 array_moveleft(array * a, size_t membersize, size_t pos)
304 assert(membersize > 0);
306 if (!safemult_sizet(membersize, pos, &bytepos)) {
312 return; /* nothing to do */
316 "array_moveleft(): %u bytes used in array, starting at position %u.",
319 if (a->used <= bytepos) {
325 memmove(a->mem, a->mem + bytepos, a->used);