]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/array.c
array: remove check for allocated == 0
[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 #include <assert.h>
16
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include "log.h"
21
22 /* Enable more Debug messages in alloc / append / memmove code. */
23 /* #define DEBUG_ARRAY */
24
25
26
27 #define array_UNUSABLE(x)       ( !(x)->mem )
28 static bool
29 safemult_sizet(size_t a, size_t b, size_t *res)
30 {
31         size_t tmp = a * b;
32
33         if (b && (tmp / b != a))
34                 return false;
35
36         *res = tmp;
37         return true;
38 }
39
40
41 void
42 array_init(array *a)
43 {
44         assert(a != NULL);
45         a->mem = NULL;
46         a->allocated = 0;
47         a->used = 0;
48 }
49
50
51 /* if realloc() fails, array_alloc return NULL. otherwise return pointer to elem pos in array */
52 void *
53 array_alloc(array * a, size_t size, size_t pos)
54 {
55         size_t alloc, pos_plus1 = pos + 1;
56         char *tmp;
57
58         assert(size > 0);
59
60         if (pos_plus1 == 0 || !safemult_sizet(size, pos_plus1, &alloc))
61                 return NULL;
62
63         if (a->allocated < alloc) {
64 #ifdef DEBUG_ARRAY
65                 Log(LOG_DEBUG, "array_alloc(): changing size from %u to %u bytes.",
66                     a->allocated, alloc);
67 #endif
68                 tmp = realloc(a->mem, alloc);
69                 if (!tmp)
70                         return NULL;
71
72                 a->mem = tmp;
73                 a->allocated = alloc;
74                 memset(a->mem + a->used, 0, a->allocated - a->used);
75                 a->used = alloc;
76         }
77
78         assert(a->allocated >= a->used);
79
80         return a->mem + (pos * size);
81 }
82
83
84 /*return number of initialized ELEMS in a. */
85 size_t
86 array_length(const array * const a, size_t membersize)
87 {
88         assert(a != NULL);
89         assert(membersize > 0);
90
91         if (array_UNUSABLE(a))
92                 return 0;
93
94         assert(a->allocated);
95         return membersize ? a->used / membersize : 0;
96 }
97
98
99 /* copy array src to array dest */
100 bool
101 array_copy(array * dest, const array * const src)
102 {
103         if (array_UNUSABLE(src))
104                 return false;
105
106         assert(src->allocated);
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- */