33static inline void *
realloc_down(
void *ptr,
size_t size )
35 void *ret = realloc( ptr, size );
36 return ret ? ret : ptr;
48 void *n = realloc(
p,sz);
54#define TAB_INIT( count, tab ) \
60#define TAB_CLEAN( count, tab ) \
67#define TAB_APPEND_CAST( cast, count, tab, p ) \
70 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
72 (tab) = cast malloc( sizeof( *(tab) ) ); \
73 if( !(tab) ) abort(); \
78#define TAB_APPEND( count, tab, p ) \
79 TAB_APPEND_CAST( , count, tab, p )
81#define TAB_FIND( count, tab, p, idx ) \
83 for( (idx) = 0; (idx) < (count); (idx)++ ) \
84 if( (tab)[(idx)] == (p) ) \
86 if( (idx) >= (count) ) \
91#define TAB_ERASE( count, tab, index ) \
94 memmove( (tab) + (index), \
95 (tab) + (index) + 1, \
96 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
105#define TAB_REMOVE( count, tab, p ) \
108 TAB_FIND( count, tab, p, i_index ); \
110 TAB_ERASE( count, tab, i_index ); \
113#define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
115 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
117 (tab) = cast malloc( sizeof( *(tab) ) ); \
118 if( !(tab) ) abort(); \
119 if( (count) - (index) > 0 ) \
120 memmove( (tab) + (index) + 1, \
122 ((count) - (index)) * sizeof( *(tab) ) );\
123 (tab)[(index)] = (p); \
127#define TAB_INSERT( count, tab, p, index ) \
128 TAB_INSERT_CAST( , count, tab, p, index )
139#define BSEARCH( entries, count, elem, zetype, key, answer ) \
141 int low = 0, high = count - 1; \
143 while( low <= high ) {\
144 int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
145 zetype mid_val = entries[mid] elem;\
146 if( mid_val < key ) \
148 else if ( mid_val > key ) \
152 answer = mid; break; \
163#define _ARRAY_ALLOC(array, newsize) { \
164 (array).i_alloc = newsize; \
165 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
166 sizeof(*(array).p_elems) ); \
167 if( !(array).p_elems ) abort(); \
170#define _ARRAY_GROW1(array) { \
171 if( (array).i_alloc < 10 ) \
172 _ARRAY_ALLOC(array, 10 ) \
173 else if( (array).i_alloc == (array).i_size ) \
174 _ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) ) \
178#define DECL_ARRAY(type) struct { \
184#define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
186#define ARRAY_INIT(array) \
188 (array).i_alloc = 0; \
189 (array).i_size = 0; \
190 (array).p_elems = NULL; \
193#define ARRAY_RESET(array) \
195 (array).i_alloc = 0; \
196 (array).i_size = 0; \
197 free( (array).p_elems ); (array).p_elems = NULL; \
200#define ARRAY_APPEND(array, elem) \
202 _ARRAY_GROW1(array); \
203 (array).p_elems[(array).i_size] = elem; \
207#define ARRAY_INSERT(array,elem,pos) \
209 _ARRAY_GROW1(array); \
210 if( (array).i_size - (pos) ) { \
211 memmove( (array).p_elems + (pos) + 1, (array).p_elems + (pos), \
212 ((array).i_size-(pos)) * sizeof(*(array).p_elems) ); \
214 (array).p_elems[pos] = elem; \
218#define _ARRAY_SHRINK(array) { \
219 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
220 _ARRAY_ALLOC(array, (array).i_size + 5); \
224#define ARRAY_FIND(array, p, idx) \
225 TAB_FIND((array).i_size, (array).p_elems, p, idx)
227#define ARRAY_REMOVE(array,pos) \
229 if( (array).i_size - (pos) - 1 ) \
231 memmove( (array).p_elems + (pos), (array).p_elems + (pos) + 1, \
232 ( (array).i_size - (pos) - 1 ) *sizeof(*(array).p_elems) );\
235 _ARRAY_SHRINK(array); \
238#define ARRAY_VAL(array, pos) array.p_elems[pos]
240#define ARRAY_BSEARCH(array, elem, zetype, key, answer) \
241 BSEARCH( (array).p_elems, (array).i_size, elem, zetype, key, answer)
245#define ARRAY_FOREACH(item, array) \
246 for (int array_index_##item = 0; \
247 array_index_##item < (array).i_size && \
248 ((item) = (array).p_elems[array_index_##item], 1); \
249 ++array_index_##item)
280# define vlc_array_item_at_index(ar, idx) \
282 const vlc_array_t *: ((ar)->pp_elems[idx]), \
283 vlc_array_t *: ((ar)->pp_elems[idx]))
300 for(
size_t i = 0; i < ar->
i_count; i++ )
311 void **pp = (
void **)realloc( ar->
pp_elems,
312 sizeof(
void * ) * (ar->
i_count + 1) );
316 size_t tail = ar->
i_count - idx;
318 memmove( pp + idx + 1, pp + idx,
sizeof(
void * ) * tail );
334 void **pp = (
void **)realloc( ar->
pp_elems,
335 sizeof(
void * ) * (ar->
i_count + 1) );
353 size_t tail = ar->
i_count - idx - 1;
356 memmove( pp + idx, pp + idx + 1,
sizeof(
void * ) * tail );
362 pp = (
void **)realloc( pp,
sizeof(
void * ) * ar->
i_count );
382static inline uint64_t
DictHash(
const char *psz_string,
int hashsize )
389 i_hash += *psz_string++;
390 i_hash += i_hash << 10;
391 i_hash ^= i_hash >> 8;
394 return i_hash % hashsize;
426 void ( * pf_free )(
void * p_data,
void * p_obj ),
431 for(
int i = 0; i < p_dict->
i_size; i++ )
437 p_next = p_current->
p_next;
438 if( pf_free != NULL )
439 ( * pf_free )( p_current->
p_value, p_obj );
459 for( ; p_entry != NULL; p_entry = p_entry->
p_next )
461 if( !strcmp( psz_key, p_entry->
psz_key ) )
464 return p_entry != NULL;
481 if( !strcmp( psz_key, p_entry->
psz_key ) )
483 p_entry = p_entry->
p_next;
498 for( i = 0; i < p_dict->
i_size; i++ )
509 for(
int i = 0; i < p_dict->
i_size; i++ )
522 ppsz_ret = (
char**)malloc(
sizeof(
char *) * (
count + 1));
527 for( i = 0; i < p_dict->
i_size; i++ )
529 for( p_entry = p_dict->
p_entries[i]; p_entry; p_entry = p_entry->
p_next )
532 ppsz_ret[
count] = NULL;
538 void * p_value,
bool rebuild )
556 p_entry = p_entry->
p_next;
561 int i_new_size = ( (p_dict->
i_size+2) * 3) / 2;
564 for( i = 0; i < p_dict->
i_size; i++ )
572 p_entry = p_entry->
p_next;
591 void ( * pf_free )(
void * p_data,
void * p_obj ),
607 if( !strcmp( psz_key, p_entry->
psz_key ) )
609 if( pf_free != NULL )
610 ( * pf_free )( p_entry->
p_value, p_obj );
620 p_entry = p_entry->
p_next;
629void vlc_delete_all( T &container )
631 typename T::iterator it = container.begin();
632 while ( it != container.end() )
size_t count
Definition: core.c:403
#define unlikely(p)
Predicted false condition.
Definition: vlc_common.h:257
#define likely(p)
Predicted true condition.
Definition: vlc_common.h:248
Definition: vlc_arrays.h:257
size_t i_count
Definition: vlc_arrays.h:258
void ** pp_elems
Definition: vlc_arrays.h:259
Definition: vlc_arrays.h:399
void * p_value
Definition: vlc_arrays.h:401
struct vlc_dictionary_entry_t * p_next
Definition: vlc_arrays.h:402
char * psz_key
Definition: vlc_arrays.h:400
Definition: vlc_arrays.h:406
int i_size
Definition: vlc_arrays.h:407
vlc_dictionary_entry_t ** p_entries
Definition: vlc_arrays.h:408
static void * realloc_or_free(void *p, size_t sz)
This wrapper around realloc() will free the input pointer when realloc() returns NULL.
Definition: vlc_arrays.h:47
static char ** vlc_dictionary_all_keys(const vlc_dictionary_t *p_dict)
Definition: vlc_arrays.h:517
static int vlc_array_append(vlc_array_t *ar, void *elem)
Definition: vlc_arrays.h:333
static void vlc_dictionary_remove_value_for_key(const vlc_dictionary_t *p_dict, const char *psz_key, void(*pf_free)(void *p_data, void *p_obj), void *p_obj)
Definition: vlc_arrays.h:591
static void vlc_array_insert_or_abort(vlc_array_t *ar, void *elem, int idx)
Definition: vlc_arrays.h:327
struct vlc_dictionary_t vlc_dictionary_t
static void *const kVLCDictionaryNotFound
Definition: vlc_arrays.h:411
static void * vlc_dictionary_value_for_key(const vlc_dictionary_t *p_dict, const char *psz_key)
Definition: vlc_arrays.h:469
static void vlc_dictionary_clear(vlc_dictionary_t *p_dict, void(*pf_free)(void *p_data, void *p_obj), void *p_obj)
Definition: vlc_arrays.h:426
static void vlc_array_init(vlc_array_t *p_array)
Definition: vlc_arrays.h:262
static bool vlc_dictionary_is_empty(const vlc_dictionary_t *p_dict)
Definition: vlc_arrays.h:507
static void vlc_array_clear(vlc_array_t *p_array)
Definition: vlc_arrays.h:268
#define vlc_array_item_at_index(ar, idx)
Definition: vlc_arrays.h:281
static void vlc_array_remove(vlc_array_t *ar, size_t idx)
Definition: vlc_arrays.h:351
struct vlc_dictionary_entry_t vlc_dictionary_entry_t
static void vlc_array_append_or_abort(vlc_array_t *ar, void *elem)
Definition: vlc_arrays.h:345
static ssize_t vlc_array_index_of_item(const vlc_array_t *ar, const void *elem)
Definition: vlc_arrays.h:298
static int vlc_dictionary_has_key(const vlc_dictionary_t *p_dict, const char *psz_key)
Definition: vlc_arrays.h:453
static int vlc_dictionary_keys_count(const vlc_dictionary_t *p_dict)
Definition: vlc_arrays.h:491
static void * realloc_down(void *ptr, size_t size)
Definition: vlc_arrays.h:34
static int vlc_array_insert(vlc_array_t *ar, void *elem, int idx)
Definition: vlc_arrays.h:310
struct vlc_array_t vlc_array_t
static void vlc_dictionary_init(vlc_dictionary_t *p_dict, int i_size)
Definition: vlc_arrays.h:413
static void vlc_dictionary_insert_impl_(vlc_dictionary_t *p_dict, const char *psz_key, void *p_value, bool rebuild)
Definition: vlc_arrays.h:538
static uint64_t DictHash(const char *psz_string, int hashsize)
Definition: vlc_arrays.h:383
static void vlc_dictionary_insert(vlc_dictionary_t *p_dict, const char *psz_key, void *p_value)
Definition: vlc_arrays.h:585
static size_t vlc_array_count(vlc_array_t *p_array)
Definition: vlc_arrays.h:275
This file is a collection of common definitions and types.
char * strdup(const char *)