]>
code.delx.au - pulseaudio/blob - src/pulsecore/dynarray.c
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, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/macro.h>
36 unsigned n_allocated
, n_entries
;
40 pa_dynarray
* pa_dynarray_new(pa_free_cb_t free_cb
) {
43 array
= pa_xnew0(pa_dynarray
, 1);
44 array
->free_cb
= free_cb
;
49 void pa_dynarray_free(pa_dynarray
*array
) {
54 for (i
= 0; i
< array
->n_entries
; i
++)
55 array
->free_cb(array
->data
[i
]);
57 pa_xfree(array
->data
);
61 void pa_dynarray_append(pa_dynarray
*array
, void *p
) {
65 if (array
->n_entries
== array
->n_allocated
) {
66 unsigned n
= PA_MAX(array
->n_allocated
* 2, 25U);
68 array
->data
= pa_xrealloc(array
->data
, sizeof(void *) * n
);
69 array
->n_allocated
= n
;
72 array
->data
[array
->n_entries
++] = p
;
75 void *pa_dynarray_get(pa_dynarray
*array
, unsigned i
) {
77 pa_assert(i
< array
->n_entries
);
79 return array
->data
[i
];
82 void *pa_dynarray_steal_last(pa_dynarray
*array
) {
85 if (array
->n_entries
> 0)
86 return array
->data
[--array
->n_entries
];
91 unsigned pa_dynarray_size(pa_dynarray
*array
) {
94 return array
->n_entries
;