|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or |
| 3 | + * modify it under the terms of the GNU General Public |
| 4 | + * License v2 as published by the Free Software Foundation. |
| 5 | + * |
| 6 | + * This program is distributed in the hope that it will be useful, |
| 7 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 9 | + * General Public License for more details. |
| 10 | + * |
| 11 | + * You should have received a copy of the GNU General Public |
| 12 | + * License along with this program; if not, write to the |
| 13 | + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 14 | + * Boston, MA 021110-1307, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +#define _GNU_SOURCE 1 |
| 18 | +#include <stdio.h> |
| 19 | +#include <stdlib.h> |
| 20 | +#include <string.h> |
| 21 | +#include "common/array.h" |
| 22 | + |
| 23 | +/* |
| 24 | + * Extensible array of pointers. Length is the number of user defined pointers, |
| 25 | + * capacity is the whole allocated size. Pointers are potentially unstable after |
| 26 | + * an append operation. Array initialized to all zeros is valid and can be extended. |
| 27 | + */ |
| 28 | + |
| 29 | +static const int alloc_increment = 32; |
| 30 | + |
| 31 | +/* Initialize new array, preallocate @capacity elemennts. */ |
| 32 | +int array_init(struct array *arr, unsigned int capacity) |
| 33 | +{ |
| 34 | + void *tmp; |
| 35 | + |
| 36 | + arr->data = NULL; |
| 37 | + arr->length = 0; |
| 38 | + arr->capacity = capacity; |
| 39 | + if (arr->capacity == 0) |
| 40 | + arr->capacity = alloc_increment; |
| 41 | + |
| 42 | + tmp = calloc(arr->capacity, sizeof(void *)); |
| 43 | + if (!tmp) |
| 44 | + return -1; |
| 45 | + arr->data = tmp; |
| 46 | + return 0; |
| 47 | +} |
| 48 | + |
| 49 | +/* Free internal data array. */ |
| 50 | +void array_free(struct array *arr) |
| 51 | +{ |
| 52 | + free(arr->data); |
| 53 | + arr->length = 0; |
| 54 | + arr->capacity = 0; |
| 55 | + arr->data = NULL; |
| 56 | +} |
| 57 | + |
| 58 | +/* Free all elements up to length. */ |
| 59 | +void array_free_elements(struct array *arr) |
| 60 | +{ |
| 61 | + unsigned int i; |
| 62 | + |
| 63 | + for (i = 0; i < arr->length; i++) { |
| 64 | + free(arr->data[i]); |
| 65 | + arr->data[i] = NULL; |
| 66 | + } |
| 67 | + arr->length = 0; |
| 68 | +} |
| 69 | + |
| 70 | +/* Reset all elements to NULL up to capacity. */ |
| 71 | +void array_clear(struct array *arr) |
| 72 | +{ |
| 73 | + for (unsigned int i = 0; i < arr->capacity; i++) |
| 74 | + arr->data[i] = NULL; |
| 75 | +} |
| 76 | + |
| 77 | +/* Use the whole capacity for elements. */ |
| 78 | +void array_use_capacity(struct array *arr) |
| 79 | +{ |
| 80 | + arr->length = arr->capacity; |
| 81 | +} |
| 82 | + |
| 83 | +/* Append a new element (increas length), extend the array if needed. */ |
| 84 | +int array_append(struct array *arr, void *element) |
| 85 | +{ |
| 86 | + if (arr->length == arr->capacity) { |
| 87 | + void **tmp; |
| 88 | + |
| 89 | + tmp = realloc(arr->data, (arr->capacity + alloc_increment) * sizeof(void *)); |
| 90 | + if (!tmp) |
| 91 | + return -1; |
| 92 | + arr->data = tmp; |
| 93 | + memset(&arr->data[arr->capacity], 0, alloc_increment * sizeof(void *)); |
| 94 | + arr->capacity += alloc_increment; |
| 95 | + } |
| 96 | + arr->data[arr->length] = element; |
| 97 | + arr->length++; |
| 98 | + return 0; |
| 99 | +} |
0 commit comments