2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
15 #include "buxtonarray.h"
18 BuxtonArray *buxton_array_new(void)
20 BuxtonArray *ret = NULL;
21 /* If this fails, we simply return NULL and allow the user
22 * to deal with the error */
23 ret = calloc(1, sizeof(BuxtonArray));
27 bool buxton_array_add(BuxtonArray *array,
31 size_t curr, new_size;
33 if (!array || !data) {
37 array->data = calloc(1, sizeof(void*));
43 new_len = array->len += 1;
44 curr = (size_t)(array->len*sizeof(void*));
45 new_size = curr + sizeof(void*);
46 if (new_len >= array->len) {
47 /* Resize the array to hold one more pointer */
48 array->data = greedy_realloc((void**)&array->data, &curr, new_size);
53 /* Store the pointer at the end of the array */
55 array->data[array->len-1] = data;
59 void *buxton_array_get(BuxtonArray *array, uint16_t index)
64 if (index > array->len) {
67 return array->data[index];
71 void buxton_array_free(BuxtonArray **array,
72 buxton_free_func free_method)
75 if (!array || !*array) {
80 /* Call the free_method on all members */
81 for (i = 0; i < (*array)->len; i++)
82 free_method((*array)->data[i]);
84 /* Ensure this array is indeed gone. */
91 * Editor modelines - http://www.wireshark.org/tools/modelines.html
99 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
100 * :indentSize=8:tabSize=8:noTabs=false: