153a5a1b3Sopenharmony_ci#ifndef foopulsecoredynarrayhfoo 253a5a1b3Sopenharmony_ci#define foopulsecoredynarrayhfoo 353a5a1b3Sopenharmony_ci 453a5a1b3Sopenharmony_ci/*** 553a5a1b3Sopenharmony_ci This file is part of PulseAudio. 653a5a1b3Sopenharmony_ci 753a5a1b3Sopenharmony_ci Copyright 2004-2008 Lennart Poettering 853a5a1b3Sopenharmony_ci 953a5a1b3Sopenharmony_ci PulseAudio is free software; you can redistribute it and/or modify 1053a5a1b3Sopenharmony_ci it under the terms of the GNU Lesser General Public License as 1153a5a1b3Sopenharmony_ci published by the Free Software Foundation; either version 2.1 of the 1253a5a1b3Sopenharmony_ci License, or (at your option) any later version. 1353a5a1b3Sopenharmony_ci 1453a5a1b3Sopenharmony_ci PulseAudio is distributed in the hope that it will be useful, but 1553a5a1b3Sopenharmony_ci WITHOUT ANY WARRANTY; without even the implied warranty of 1653a5a1b3Sopenharmony_ci MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 1753a5a1b3Sopenharmony_ci Lesser General Public License for more details. 1853a5a1b3Sopenharmony_ci 1953a5a1b3Sopenharmony_ci You should have received a copy of the GNU Lesser General Public 2053a5a1b3Sopenharmony_ci License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>. 2153a5a1b3Sopenharmony_ci***/ 2253a5a1b3Sopenharmony_ci 2353a5a1b3Sopenharmony_ci#include <pulse/def.h> 2453a5a1b3Sopenharmony_ci 2553a5a1b3Sopenharmony_citypedef struct pa_dynarray pa_dynarray; 2653a5a1b3Sopenharmony_ci 2753a5a1b3Sopenharmony_ci/* Implementation of a simple dynamically sized array for storing pointers. 2853a5a1b3Sopenharmony_ci * 2953a5a1b3Sopenharmony_ci * When the array is created, a free callback can be provided, which will be 3053a5a1b3Sopenharmony_ci * then used when removing items from the array and when freeing the array. If 3153a5a1b3Sopenharmony_ci * the free callback is not provided, the memory management of the stored items 3253a5a1b3Sopenharmony_ci * is the responsibility of the array user. If there is need to remove items 3353a5a1b3Sopenharmony_ci * from the array without freeing them, while also having the free callback 3453a5a1b3Sopenharmony_ci * set, the functions with "steal" in their name can be used. 3553a5a1b3Sopenharmony_ci * 3653a5a1b3Sopenharmony_ci * Removing items from the middle of the array causes the last item to be 3753a5a1b3Sopenharmony_ci * moved to the place of the removed item. That is, array ordering is not 3853a5a1b3Sopenharmony_ci * preserved. 3953a5a1b3Sopenharmony_ci * 4053a5a1b3Sopenharmony_ci * The array doesn't support storing NULL pointers. */ 4153a5a1b3Sopenharmony_ci 4253a5a1b3Sopenharmony_cipa_dynarray* pa_dynarray_new(pa_free_cb_t free_cb); 4353a5a1b3Sopenharmony_civoid pa_dynarray_free(pa_dynarray *array); 4453a5a1b3Sopenharmony_ci 4553a5a1b3Sopenharmony_civoid pa_dynarray_append(pa_dynarray *array, void *p); 4653a5a1b3Sopenharmony_ci 4753a5a1b3Sopenharmony_ci/* Returns the element at index i, or NULL if i is out of bounds. */ 4853a5a1b3Sopenharmony_civoid *pa_dynarray_get(pa_dynarray *array, unsigned i); 4953a5a1b3Sopenharmony_ci 5053a5a1b3Sopenharmony_ci/* Returns the last element, or NULL if the array is empty. */ 5153a5a1b3Sopenharmony_civoid *pa_dynarray_last(pa_dynarray *array); 5253a5a1b3Sopenharmony_ci 5353a5a1b3Sopenharmony_ci/* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. */ 5453a5a1b3Sopenharmony_ciint pa_dynarray_remove_by_index(pa_dynarray *array, unsigned i); 5553a5a1b3Sopenharmony_ci 5653a5a1b3Sopenharmony_ci/* Returns -PA_ERR_NOENTITY if p is not found in the array, and zero 5753a5a1b3Sopenharmony_ci * otherwise. If the array contains multiple occurrences of p, only one of 5853a5a1b3Sopenharmony_ci * them is removed (and it's unspecified which one). */ 5953a5a1b3Sopenharmony_ciint pa_dynarray_remove_by_data(pa_dynarray *array, void *p); 6053a5a1b3Sopenharmony_ci 6153a5a1b3Sopenharmony_ci/* Returns the removed item, or NULL if the array is empty. */ 6253a5a1b3Sopenharmony_civoid *pa_dynarray_steal_last(pa_dynarray *array); 6353a5a1b3Sopenharmony_ci 6453a5a1b3Sopenharmony_ciunsigned pa_dynarray_size(pa_dynarray *array); 6553a5a1b3Sopenharmony_ci 6653a5a1b3Sopenharmony_ci/* Returns -PA_ERR_NOENTITY if i is out of bounds, and zero otherwise. 6753a5a1b3Sopenharmony_ci * Here i is the location index in the array like 0, ..., array->entries */ 6853a5a1b3Sopenharmony_ciint pa_dynarray_insert_by_index(pa_dynarray *array, void *p, unsigned i); 6953a5a1b3Sopenharmony_ci 7053a5a1b3Sopenharmony_ci#define PA_DYNARRAY_FOREACH(elem, array, idx) \ 7153a5a1b3Sopenharmony_ci for ((idx) = 0; ((elem) = pa_dynarray_get(array, idx)); (idx)++) 7253a5a1b3Sopenharmony_ci 7353a5a1b3Sopenharmony_ci#endif 74