| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/* |
| 2 |
|
|
* $Id: arraylist.c,v 1.4 2006/01/26 02:16:28 mclark Exp $ |
| 3 |
|
|
* |
| 4 |
|
|
* Copyright (c) 2004, 2005 Metaparadigm Pte. Ltd. |
| 5 |
|
|
* Michael Clark <michael@metaparadigm.com> |
| 6 |
|
|
* |
| 7 |
|
|
* This library is free software; you can redistribute it and/or modify |
| 8 |
|
|
* it under the terms of the MIT license. See COPYING for details. |
| 9 |
|
|
* |
| 10 |
|
|
*/ |
| 11 |
|
|
|
| 12 |
|
|
#include "config.h" |
| 13 |
|
|
|
| 14 |
|
|
#include <limits.h> |
| 15 |
|
|
|
| 16 |
|
|
#ifdef STDC_HEADERS |
| 17 |
|
|
#include <stdlib.h> |
| 18 |
|
|
#include <string.h> |
| 19 |
|
|
#endif /* STDC_HEADERS */ |
| 20 |
|
|
|
| 21 |
|
|
#if defined(HAVE_STRINGS_H) && !defined(_STRING_H) && !defined(__USE_BSD) |
| 22 |
|
|
#include <strings.h> |
| 23 |
|
|
#endif /* HAVE_STRINGS_H */ |
| 24 |
|
|
|
| 25 |
|
|
#ifndef SIZE_T_MAX |
| 26 |
|
|
#if SIZEOF_SIZE_T == SIZEOF_INT |
| 27 |
|
|
#define SIZE_T_MAX UINT_MAX |
| 28 |
|
|
#elif SIZEOF_SIZE_T == SIZEOF_LONG |
| 29 |
|
|
#define SIZE_T_MAX ULONG_MAX |
| 30 |
|
|
#elif SIZEOF_SIZE_T == SIZEOF_LONG_LONG |
| 31 |
|
|
#define SIZE_T_MAX ULLONG_MAX |
| 32 |
|
|
#else |
| 33 |
|
|
#error Unable to determine size of size_t |
| 34 |
|
|
#endif |
| 35 |
|
|
#endif |
| 36 |
|
|
|
| 37 |
|
|
#include "arraylist.h" |
| 38 |
|
|
|
| 39 |
|
✗ |
struct array_list *array_list_new(array_list_free_fn *free_fn) |
| 40 |
|
|
{ |
| 41 |
|
✗ |
return array_list_new2(free_fn, ARRAY_LIST_DEFAULT_SIZE); |
| 42 |
|
|
} |
| 43 |
|
|
|
| 44 |
|
✗ |
struct array_list *array_list_new2(array_list_free_fn *free_fn, int initial_size) |
| 45 |
|
|
{ |
| 46 |
|
|
struct array_list *arr; |
| 47 |
|
|
|
| 48 |
|
✗ |
if (initial_size < 0 || (size_t)initial_size >= SIZE_T_MAX / sizeof(void *)) |
| 49 |
|
|
return NULL; |
| 50 |
|
✗ |
arr = (struct array_list *)malloc(sizeof(struct array_list)); |
| 51 |
|
✗ |
if (!arr) |
| 52 |
|
|
return NULL; |
| 53 |
|
✗ |
arr->size = initial_size; |
| 54 |
|
✗ |
arr->length = 0; |
| 55 |
|
✗ |
arr->free_fn = free_fn; |
| 56 |
|
✗ |
if (!(arr->array = (void **)malloc(arr->size * sizeof(void *)))) |
| 57 |
|
|
{ |
| 58 |
|
✗ |
free(arr); |
| 59 |
|
✗ |
return NULL; |
| 60 |
|
|
} |
| 61 |
|
|
return arr; |
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
✗ |
extern void array_list_free(struct array_list *arr) |
| 65 |
|
|
{ |
| 66 |
|
|
size_t i; |
| 67 |
|
✗ |
for (i = 0; i < arr->length; i++) |
| 68 |
|
✗ |
if (arr->array[i]) |
| 69 |
|
✗ |
arr->free_fn(arr->array[i]); |
| 70 |
|
✗ |
free(arr->array); |
| 71 |
|
✗ |
free(arr); |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
✗ |
void *array_list_get_idx(struct array_list *arr, size_t i) |
| 75 |
|
|
{ |
| 76 |
|
✗ |
if (i >= arr->length) |
| 77 |
|
|
return NULL; |
| 78 |
|
✗ |
return arr->array[i]; |
| 79 |
|
|
} |
| 80 |
|
|
|
| 81 |
|
✗ |
static int array_list_expand_internal(struct array_list *arr, size_t max) |
| 82 |
|
|
{ |
| 83 |
|
|
void *t; |
| 84 |
|
|
size_t new_size; |
| 85 |
|
|
|
| 86 |
|
✗ |
if (max < arr->size) |
| 87 |
|
|
return 0; |
| 88 |
|
|
/* Avoid undefined behaviour on size_t overflow */ |
| 89 |
|
✗ |
if (arr->size >= SIZE_T_MAX / 2) |
| 90 |
|
|
new_size = max; |
| 91 |
|
|
else |
| 92 |
|
|
{ |
| 93 |
|
✗ |
new_size = arr->size << 1; |
| 94 |
|
|
if (new_size < max) |
| 95 |
|
|
new_size = max; |
| 96 |
|
|
} |
| 97 |
|
✗ |
if (new_size > (~((size_t)0)) / sizeof(void *)) |
| 98 |
|
|
return -1; |
| 99 |
|
✗ |
if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
| 100 |
|
|
return -1; |
| 101 |
|
✗ |
arr->array = (void **)t; |
| 102 |
|
✗ |
arr->size = new_size; |
| 103 |
|
✗ |
return 0; |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
✗ |
int array_list_shrink(struct array_list *arr, size_t empty_slots) |
| 107 |
|
|
{ |
| 108 |
|
|
void *t; |
| 109 |
|
|
size_t new_size; |
| 110 |
|
|
|
| 111 |
|
✗ |
if (empty_slots >= SIZE_T_MAX / sizeof(void *) - arr->length) |
| 112 |
|
|
return -1; |
| 113 |
|
✗ |
new_size = arr->length + empty_slots; |
| 114 |
|
✗ |
if (new_size == arr->size) |
| 115 |
|
|
return 0; |
| 116 |
|
✗ |
if (new_size > arr->size) |
| 117 |
|
✗ |
return array_list_expand_internal(arr, new_size); |
| 118 |
|
|
if (new_size == 0) |
| 119 |
|
|
new_size = 1; |
| 120 |
|
|
|
| 121 |
|
✗ |
if (!(t = realloc(arr->array, new_size * sizeof(void *)))) |
| 122 |
|
|
return -1; |
| 123 |
|
✗ |
arr->array = (void **)t; |
| 124 |
|
✗ |
arr->size = new_size; |
| 125 |
|
✗ |
return 0; |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
|
|
//static inline int _array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
| 129 |
|
✗ |
int array_list_put_idx(struct array_list *arr, size_t idx, void *data) |
| 130 |
|
|
{ |
| 131 |
|
✗ |
if (idx > SIZE_T_MAX - 1) |
| 132 |
|
|
return -1; |
| 133 |
|
✗ |
if (array_list_expand_internal(arr, idx + 1)) |
| 134 |
|
|
return -1; |
| 135 |
|
✗ |
if (idx < arr->length && arr->array[idx]) |
| 136 |
|
✗ |
arr->free_fn(arr->array[idx]); |
| 137 |
|
✗ |
arr->array[idx] = data; |
| 138 |
|
✗ |
if (idx > arr->length) |
| 139 |
|
|
{ |
| 140 |
|
|
/* Zero out the arraylist slots in between the old length |
| 141 |
|
|
and the newly added entry so we know those entries are |
| 142 |
|
|
empty. |
| 143 |
|
|
e.g. when setting array[7] in an array that used to be |
| 144 |
|
|
only 5 elements longs, array[5] and array[6] need to be |
| 145 |
|
|
set to 0. |
| 146 |
|
|
*/ |
| 147 |
|
✗ |
memset(arr->array + arr->length, 0, (idx - arr->length) * sizeof(void *)); |
| 148 |
|
|
} |
| 149 |
|
✗ |
if (arr->length <= idx) |
| 150 |
|
✗ |
arr->length = idx + 1; |
| 151 |
|
|
return 0; |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
✗ |
int array_list_add(struct array_list *arr, void *data) |
| 155 |
|
|
{ |
| 156 |
|
|
/* Repeat some of array_list_put_idx() so we can skip several |
| 157 |
|
|
checks that we know are unnecessary when appending at the end |
| 158 |
|
|
*/ |
| 159 |
|
✗ |
size_t idx = arr->length; |
| 160 |
|
✗ |
if (idx > SIZE_T_MAX - 1) |
| 161 |
|
|
return -1; |
| 162 |
|
✗ |
if (array_list_expand_internal(arr, idx + 1)) |
| 163 |
|
|
return -1; |
| 164 |
|
✗ |
arr->array[idx] = data; |
| 165 |
|
✗ |
arr->length++; |
| 166 |
|
✗ |
return 0; |
| 167 |
|
|
} |
| 168 |
|
|
|
| 169 |
|
✗ |
void array_list_sort(struct array_list *arr, int (*compar)(const void *, const void *)) |
| 170 |
|
|
{ |
| 171 |
|
✗ |
qsort(arr->array, arr->length, sizeof(arr->array[0]), compar); |
| 172 |
|
|
} |
| 173 |
|
|
|
| 174 |
|
✗ |
void *array_list_bsearch(const void **key, struct array_list *arr, |
| 175 |
|
|
int (*compar)(const void *, const void *)) |
| 176 |
|
|
{ |
| 177 |
|
✗ |
return bsearch(key, arr->array, arr->length, sizeof(arr->array[0]), compar); |
| 178 |
|
|
} |
| 179 |
|
|
|
| 180 |
|
✗ |
size_t array_list_length(struct array_list *arr) |
| 181 |
|
|
{ |
| 182 |
|
✗ |
return arr->length; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
✗ |
int array_list_del_idx(struct array_list *arr, size_t idx, size_t count) |
| 186 |
|
|
{ |
| 187 |
|
|
size_t i, stop; |
| 188 |
|
|
|
| 189 |
|
|
/* Avoid overflow in calculation with large indices. */ |
| 190 |
|
✗ |
if (idx > SIZE_T_MAX - count) |
| 191 |
|
|
return -1; |
| 192 |
|
✗ |
stop = idx + count; |
| 193 |
|
✗ |
if (idx >= arr->length || stop > arr->length) |
| 194 |
|
|
return -1; |
| 195 |
|
✗ |
for (i = idx; i < stop; ++i) |
| 196 |
|
|
{ |
| 197 |
|
|
// Because put_idx can skip entries, we need to check if |
| 198 |
|
|
// there's actually anything in each slot we're erasing. |
| 199 |
|
✗ |
if (arr->array[i]) |
| 200 |
|
✗ |
arr->free_fn(arr->array[i]); |
| 201 |
|
|
} |
| 202 |
|
✗ |
memmove(arr->array + idx, arr->array + stop, (arr->length - stop) * sizeof(void *)); |
| 203 |
|
✗ |
arr->length -= count; |
| 204 |
|
✗ |
return 0; |
| 205 |
|
|
} |
| 206 |
|
|
|