Mercurial > code
comparison array.c @ 48:cdae3689f1b6
Update of array and cosmetic in ARRAY_ macros
author | David Demelier <markand@malikania.fr> |
---|---|
date | Fri, 07 Oct 2011 16:46:31 +0200 |
parents | f1e184940197 |
children | 8a8db17c02a4 |
comparison
equal
deleted
inserted
replaced
47:fea13579acbe | 48:cdae3689f1b6 |
---|---|
25 #define OFFSET(x) (arr->unit * (x)) | 25 #define OFFSET(x) (arr->unit * (x)) |
26 | 26 |
27 static int array_grow(struct array *); | 27 static int array_grow(struct array *); |
28 | 28 |
29 struct array * | 29 struct array * |
30 array_new(const void *data, size_t unit, int bsize, int type) | 30 array_new(enum array_type type, size_t unit, int length) |
31 { | 31 { |
32 struct array *arr; | 32 struct array *arr; |
33 | 33 |
34 if (unit == 0 || (arr = malloc(sizeof (struct array))) == NULL) | 34 if (unit == 0 || (arr = malloc(sizeof (struct array))) == NULL) |
35 return NULL; | 35 return NULL; |
36 | 36 |
37 memset(arr, 0, sizeof (struct array)); | 37 memset(arr, 0, sizeof (struct array)); |
38 | |
39 arr->type = type; | 38 arr->type = type; |
40 arr->bsize = (bsize == 0) ? ARRAY_DEFAULT_BSIZE : bsize; | 39 arr->bsize = (length == 0) ? ARRAY_DEFAULT_BSIZE : length; |
41 arr->unit = unit; | 40 arr->unit = unit; |
42 arr->size = OFFSET(arr->bsize); | 41 arr->size = OFFSET(arr->bsize); |
43 | 42 |
44 if ((arr->data = malloc(arr->size)) == NULL) { | 43 if ((arr->data = malloc(arr->size)) == NULL) { |
45 free(arr); | 44 free(arr); |
46 return NULL; | 45 return NULL; |
47 } | 46 } |
48 | |
49 if (data) | |
50 memcpy(arr->data, data, arr->unit); | |
51 | 47 |
52 return arr; | 48 return arr; |
53 } | 49 } |
54 | 50 |
55 /* | 51 /* |
169 */ | 165 */ |
170 | 166 |
171 int | 167 int |
172 array_iswap(struct array *arr, int i1, int i2) | 168 array_iswap(struct array *arr, int i1, int i2) |
173 { | 169 { |
170 void *tmp; | |
171 | |
174 /* Out of bounds */ | 172 /* Out of bounds */ |
175 if (i1 >= arr->length || i1 < 0 || i2 >= arr->length || i2 < 0) | 173 if (i1 >= arr->length || i1 < 0 || i2 >= arr->length || i2 < 0) |
176 return -1; | 174 return -1; |
177 | 175 |
178 /* | 176 /* |
179 * Only allocate at this time, the user may do not want to use this | 177 * Only allocate at this time, the user may do not want to use this |
180 * function. | 178 * function. |
181 */ | 179 */ |
182 | 180 |
183 if (arr->_tmp == NULL && (arr->_tmp = malloc(arr->unit)) == NULL) | 181 if ((tmp = malloc(arr->unit)) == NULL) |
184 return -1; | 182 return -1; |
185 | 183 |
186 memcpy((char *) arr->_tmp, (char *) arr->data + OFFSET(i1), arr->unit); | 184 memcpy((char *) tmp, (char *) arr->data + OFFSET(i1), arr->unit); |
187 memcpy((char *) arr->data + OFFSET(i1), (char *) arr->data + OFFSET(i2), | 185 memcpy((char *) arr->data + OFFSET(i1), (char *) arr->data + OFFSET(i2), |
188 arr->unit); | 186 arr->unit); |
189 memcpy((char *) arr->data + OFFSET(i2), (char *) arr->_tmp, arr->unit); | 187 memcpy((char *) arr->data + OFFSET(i2), (char *) tmp, arr->unit); |
190 | 188 |
191 /* | 189 /* |
192 * Clear bytes for safety. | 190 * Clear bytes for safety you probably don't want a password or |
191 * secure data to be left somewhere in the memory. | |
193 */ | 192 */ |
194 | 193 |
195 memset(arr->_tmp, 0, arr->unit); | 194 memset(tmp, 0, arr->unit); |
195 free(tmp); | |
196 | 196 |
197 return 0; | 197 return 0; |
198 } | 198 } |
199 | 199 |
200 /* | 200 /* |
283 { | 283 { |
284 array_clear(arr); | 284 array_clear(arr); |
285 | 285 |
286 if (arr->data) | 286 if (arr->data) |
287 free(arr->data); | 287 free(arr->data); |
288 if (arr->_tmp) | |
289 free(arr->_tmp); | |
290 | 288 |
291 free(arr); | 289 free(arr); |
292 } | 290 } |
293 | 291 |
294 /* | 292 /* |