]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/array.c
array: remove alignment of requested size
[ngircd-alex.git] / src / ngircd / array.c
1 /*
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.
7  *
8  * functions to dynamically allocate arrays.
9  * Copyright (c) 2005 Florian Westphal (westphal@foo.fh-furtwangen.de)
10  *
11  */
12
13 #include "array.h"
14
15 static char UNUSED id[] = "$Id: array.c,v 1.15 2007/11/18 15:05:35 alex Exp $";
16
17 #include <assert.h>
18
19 #include <stdlib.h>
20 #include <string.h>
21
22 #include "log.h"
23
24 /* Enable more Debug messages in alloc / append / memmove code. */
25 /* #define DEBUG_ARRAY */
26
27
28
29 #define array_UNUSABLE(x)       ( !(x)->mem || (0 == (x)->allocated) )
30 static bool
31 safemult_sizet(size_t a, size_t b, size_t *res)
32 {
33         size_t tmp = a * b;
34
35         if (b && (tmp / b != a))
36                 return false;
37
38         *res = tmp;
39         return true;
40 }
41
42
43 void
44 array_init(array *a)
45 {
46         assert(a != NULL);
47         a->mem = NULL;
48         a->allocated = 0;
49         a->used = 0;
50 }
51
52
53 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
54 void *
55 array_alloc(array * a, size_t size, size_t pos)
56 {
57         size_t alloc, pos_plus1 = pos + 1;
58         char *tmp;
59
60         assert(size > 0);
61
62         if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
63                 return NULL;
64
65         if (a->allocated < alloc) {
66 #ifdef DEBUG_ARRAY
67                 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
68                     a->allocated, alloc);
69 #endif
70                 tmp = realloc(a->mem, alloc);
71                 if (!tmp)
72                         return NULL;
73
74                 a->mem = tmp;
75                 a->allocated = alloc;
76                 memset(a->mem + a->used, 0, a->allocated - a->used);
77                 a->used = alloc;
78         }
79
80         assert(a->allocated >= a->used);
81
82         return a->mem + (pos * size);
83 }
84
85
86 /*return number of initialized ELEMS in a. */
87 size_t
88 array_length(const array * const a, size_t membersize)
89 {
90         assert(a != NULL);
91         assert(membersize > 0);
92
93         if (array_UNUSABLE(a))
94                 return 0;
95
96         return membersize ? a->used / membersize : 0;
97 }
98
99
100 /* copy array src to array dest */
101 bool
102 array_copy(array * dest, const array * const src)
103 {
104         if (array_UNUSABLE(src))
105                 return false;
106
107         return array_copyb(dest, src->mem, src->used);
108 }
109
110
111 /* return false on failure (realloc failure, invalid src/dest array) */
112 bool
113 array_copyb(array * dest, const char *src, size_t len)
114 {
115         assert(dest != NULL);
116         assert(src != NULL );
117
118         if (!src || !dest)
119                 return false;
120
121         array_trunc(dest);
122         return array_catb(dest, src, len);
123 }
124
125
126 /* copy string to dest */
127 bool
128 array_copys(array * dest, const char *src)
129 {
130         return array_copyb(dest, src, strlen(src));
131 }
132
133
134 /* append len bytes from src to the array dest.
135 return false if we could not append all bytes (realloc failure, invalid src/dest array) */
136 bool
137 array_catb(array * dest, const char *src, size_t len)
138 {
139         size_t tmp;
140         size_t used;
141         char *ptr;
142
143         assert(dest != NULL);
144         assert(src != NULL);
145
146         if (!len)
147                 return true;
148
149         if (!src || !dest)
150                 return false;
151
152         used = dest->used;
153         tmp = used + len;
154
155         if (tmp < used || tmp < len)    /* integer overflow */
156                 return false;
157
158         if (!array_alloc(dest, 1, tmp))
159                 return false;
160
161         ptr = dest->mem;
162
163         assert(ptr != NULL);
164
165 #ifdef DEBUG_ARRAY
166         Log(LOG_DEBUG,
167             "array_catb(): appending %u bytes to array (now %u bytes in array).",
168             len, tmp);
169 #endif
170         memcpy(ptr + used, src, len);
171         dest->used = tmp;
172         return true;
173 }
174
175
176 /* append string to dest */
177 bool
178 array_cats(array * dest, const char *src)
179 {
180         return array_catb(dest, src, strlen(src));
181 }
182
183
184 /* append trailing NUL byte to array */
185 bool
186 array_cat0(array * a)
187 {
188         return array_catb(a, "", 1);
189 }
190
191
192 /* append trailing NUL byte to array, but do not count it. */
193 bool
194 array_cat0_temporary(array * a)
195 {
196         char *endpos = array_alloc(a, 1, array_bytes(a));
197         if (!endpos)
198                 return false;
199
200         *endpos = '\0';
201         return true;
202 }
203
204 /* add contents of array src to array dest. */
205 bool
206 array_cat(array * dest, const array * const src)
207 {
208         if (array_UNUSABLE(src))
209                 return false;
210
211         return array_catb(dest, src->mem, src->used);
212 }
213
214
215 /* return pointer to the element at pos.
216    return NULL if the array is unallocated, or if pos is larger than
217    the number of elements stored int the array. */
218 void *
219 array_get(array * a, size_t membersize, size_t pos)
220 {
221         size_t totalsize;
222         size_t posplus1 = pos + 1;
223
224         assert(membersize > 0);
225         assert(a != NULL);
226
227         if (!posplus1 || array_UNUSABLE(a))
228                 return NULL;
229
230         if (!safemult_sizet(posplus1, membersize, &totalsize))
231                 return NULL;
232
233         if (a->allocated < totalsize)
234                 return NULL;
235
236         totalsize = pos * membersize;
237         return a->mem + totalsize;
238 }
239
240
241 void
242 array_free(array * a)
243 {
244         assert(a != NULL);
245 #ifdef DEBUG_ARRAY
246         Log(LOG_DEBUG,
247             "array_free(): %u bytes free'd (%u bytes still used at time of free()).",
248             a->allocated, a->used);
249 #endif
250         free(a->mem);
251         a->mem = NULL;
252         a->allocated = 0;
253         a->used = 0;
254 }
255
256 void
257 array_free_wipe(array *a)
258 {
259         size_t bytes = a->allocated;
260         if (bytes)
261                 memset(a->mem, 0, bytes);
262         array_free(a);
263 }
264
265 void *
266 array_start(const array * const a)
267 {
268         assert(a != NULL);
269         return a->mem;
270 }
271
272
273 void
274 array_trunc(array * a)
275 {
276         assert(a != NULL);
277         a->used = 0;
278 }
279
280
281 void
282 array_truncate(array * a, size_t membersize, size_t len)
283 {
284         size_t newlen;
285         assert(a != NULL);
286         if (!safemult_sizet(membersize, len, &newlen))
287                 return;
288
289         if (newlen <= a->allocated)
290                 a->used = newlen;
291 }
292
293
294 /* move elements starting at pos to beginning of array */
295 void
296 array_moveleft(array * a, size_t membersize, size_t pos)
297 {
298         size_t bytepos;
299
300         assert(a != NULL);
301         assert(membersize > 0);
302
303         if (!safemult_sizet(membersize, pos, &bytepos)) {
304                 a->used = 0;
305                 return;
306         }
307
308         if (!bytepos)
309                 return; /* nothing to do */
310
311 #ifdef DEBUG_ARRAY
312         Log(LOG_DEBUG,
313             "array_moveleft(): %u bytes used in array, starting at position %u.",
314             a->used, bytepos);
315 #endif
316         if (a->used <= bytepos) {
317                 a->used = 0;
318                 return;
319         }
320
321         a->used -= bytepos;
322         memmove(a->mem, a->mem + bytepos, a->used);
323 }
324
325 /* -eof- */