2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
6 PulseAudio 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 of the
9 License, or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
27 #include <pulse/xmalloc.h>
28 #include <pulsecore/macro.h>
34 unsigned n_allocated, n_entries;
38 pa_dynarray* pa_dynarray_new(pa_free_cb_t free_cb) {
41 array = pa_xnew0(pa_dynarray, 1);
42 array->free_cb = free_cb;
47 void pa_dynarray_free(pa_dynarray *array) {
52 for (i = 0; i < array->n_entries; i++)
53 array->free_cb(array->data[i]);
55 pa_xfree(array->data);
59 void pa_dynarray_append(pa_dynarray *array, void *p) {
63 if (array->n_entries == array->n_allocated) {
64 unsigned n = PA_MAX(array->n_allocated * 2, 25U);
66 array->data = pa_xrealloc(array->data, sizeof(void *) * n);
67 array->n_allocated = n;
70 array->data[array->n_entries++] = p;
73 void *pa_dynarray_get(pa_dynarray *array, unsigned i) {
75 pa_assert(i < array->n_entries);
77 return array->data[i];
80 void *pa_dynarray_steal_last(pa_dynarray *array) {
83 if (array->n_entries > 0)
84 return array->data[--array->n_entries];
89 unsigned pa_dynarray_size(pa_dynarray *array) {
92 return array->n_entries;