• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef foopulsecoredynarrayhfoo
2 #define foopulsecoredynarrayhfoo
3 
4 /***
5   This file is part of PulseAudio.
6 
7   Copyright 2004-2008 Lennart Poettering
8 
9   PulseAudio is free software; you can redistribute it and/or modify
10   it under the terms of the GNU Lesser General Public License as
11   published by the Free Software Foundation; either version 2.1 of the
12   License, or (at your option) any later version.
13 
14   PulseAudio is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18 
19   You should have received a copy of the GNU Lesser General Public
20   License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
21 ***/
22 
23 #include <pulse/def.h>
24 
25 typedef struct pa_dynarray pa_dynarray;
26 
27 /* Implementation of a simple dynamically sized array for storing pointers.
28  *
29  * When the array is created, a free callback can be provided, which will be
30  * then used when removing items from the array and when freeing the array. If
31  * the free callback is not provided, the memory management of the stored items
32  * is the responsibility of the array user. If there is need to remove items
33  * from the array without freeing them, while also having the free callback
34  * set, the functions with "steal" in their name can be used.
35  *
36  * Removing items from the middle of the array causes the last item to be
37  * moved to the place of the removed item. That is, array ordering is not
38  * preserved.
39  *
40  * The array doesn't support storing NULL pointers. */
41 
42 pa_dynarray* pa_dynarray_new(pa_free_cb_t free_cb);
43 void pa_dynarray_free(pa_dynarray *array);
44 
45 void pa_dynarray_append(pa_dynarray *array, void *p);
46 
47 /* Returns the element at index i, or NULL if i is out of bounds. */
48 void *pa_dynarray_get(pa_dynarray *array, unsigned i);
49 
50 /* Returns the last element, or NULL if the array is empty. */
51 void *pa_dynarray_last(pa_dynarray *array);
52 
53 /* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. */
54 int pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i);
55 
56 /* Returns -PA_ERR_NOENTITY if p is not found in the array, and zero
57  * otherwise. If the array contains multiple occurrences of p, only one of
58  * them is removed (and it's unspecified which one). */
59 int pa_dynarray_remove_by_data(pa_dynarray *array, void *p);
60 
61 /* Returns the removed item, or NULL if the array is empty. */
62 void *pa_dynarray_steal_last(pa_dynarray *array);
63 
64 unsigned pa_dynarray_size(pa_dynarray *array);
65 
66 /* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise.
67  * Here i is the location index in the array like 0, ..., array->entries */
68 int pa_dynarray_insert_by_index(pa_dynarray *array, void *p, unsigned i);
69 
70 #define PA_DYNARRAY_FOREACH(elem, array, idx) \
71     for ((idx) = 0; ((elem) = pa_dynarray_get(array, idx)); (idx)++)
72 
73 #endif
74