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