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