VLC 4.0.0-dev
vlc_arrays.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 *
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Clément Stenac <zorglub@videolan.org>
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
23
24#ifndef VLC_ARRAYS_H_
25#define VLC_ARRAYS_H_
26
27/**
28 * \file
29 * This file defines functions, structures and macros for handling arrays in vlc
30 */
31
32/* realloc() that never fails *if* downsizing */
33static inline void *realloc_down( void *ptr, size_t size )
35 void *ret = realloc( ptr, size );
36 return ret ? ret : ptr;
37}
38
39/**
40 * This wrapper around realloc() will free the input pointer when
41 * realloc() returns NULL. The use case ptr = realloc(ptr, newsize) will
42 * cause a memory leak when ptr pointed to a heap allocation before,
43 * leaving the buffer allocated but unreferenced. vlc_realloc() is a
44 * drop-in replacement for that use case (and only that use case).
45 */
46static inline void *realloc_or_free( void *p, size_t sz )
48 void *n = realloc(p,sz);
49 if( !n )
50 free(p);
51 return n;
52}
53
54#define TAB_INIT( count, tab ) \
55 do { \
56 (count) = 0; \
57 (tab) = NULL; \
58 } while(0)
59
60#define TAB_CLEAN( count, tab ) \
61 do { \
62 free( tab ); \
63 (count)= 0; \
64 (tab)= NULL; \
65 } while(0)
66
67#define TAB_APPEND_CAST( cast, count, tab, p ) \
68 do { \
69 if( (count) > 0 ) \
70 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
71 else \
72 (tab) = cast malloc( sizeof( *(tab) ) ); \
73 if( !(tab) ) abort(); \
74 (tab)[count] = (p); \
75 (count)++; \
76 } while(0)
77
78#define TAB_APPEND( count, tab, p ) \
79 TAB_APPEND_CAST( , count, tab, p )
80
81#define TAB_FIND( count, tab, p, idx ) \
82 do { \
83 for( (idx) = 0; (idx) < (count); (idx)++ ) \
84 if( (tab)[(idx)] == (p) ) \
85 break; \
86 if( (idx) >= (count) ) \
87 (idx) = -1; \
88 } while(0)
89
90
91#define TAB_ERASE( count, tab, index ) \
92 do { \
93 if( (count) > 1 ) \
94 memmove( (tab) + (index), \
95 (tab) + (index) + 1, \
96 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
97 (count)--; \
98 if( (count) == 0 ) \
99 { \
100 free( tab ); \
101 (tab) = NULL; \
102 } \
103 } while(0)
104
105#define TAB_REMOVE( count, tab, p ) \
106 do { \
107 int i_index; \
108 TAB_FIND( count, tab, p, i_index ); \
109 if( i_index >= 0 ) \
110 TAB_ERASE( count, tab, i_index ); \
111 } while(0)
112
113#define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
114 if( (count) > 0 ) \
115 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
116 else \
117 (tab) = cast malloc( sizeof( *(tab) ) ); \
118 if( !(tab) ) abort(); \
119 if( (count) - (index) > 0 ) \
120 memmove( (tab) + (index) + 1, \
121 (tab) + (index), \
122 ((count) - (index)) * sizeof( *(tab) ) );\
123 (tab)[(index)] = (p); \
124 (count)++; \
125} while(0)
126
127#define TAB_INSERT( count, tab, p, index ) \
128 TAB_INSERT_CAST( , count, tab, p, index )
129
130/**
131 * Binary search in a sorted array. The key must be comparable by < and >
132 * \param entries array of entries
133 * \param count number of entries
134 * \param elem key to check within an entry (like .id, or ->i_id)
135 * \param zetype type of the key
136 * \param key value of the key
137 * \param answer index of answer within the array. -1 if not found
138 */
139#define BSEARCH( entries, count, elem, zetype, key, answer ) \
140 do { \
141 int low = 0, high = count - 1; \
142 answer = -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 ) \
147 low = mid + 1; \
148 else if ( mid_val > key ) \
149 high = mid -1; \
150 else \
151 { \
152 answer = mid; break; \
153 }\
154 } \
155 } while(0)
156
157
158/************************************************************************
159 * Dynamic arrays with progressive allocation
160 ************************************************************************/
161
162/* Internal functions */
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(); \
168}
169
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) ) \
175}
176
177/* API */
178#define DECL_ARRAY(type) struct { \
179 int i_alloc; \
180 int i_size; \
181 type *p_elems; \
182}
183
184#define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
186#define ARRAY_INIT(array) \
187 do { \
188 (array).i_alloc = 0; \
189 (array).i_size = 0; \
190 (array).p_elems = NULL; \
191 } while(0)
192
193#define ARRAY_RESET(array) \
194 do { \
195 (array).i_alloc = 0; \
196 (array).i_size = 0; \
197 free( (array).p_elems ); (array).p_elems = NULL; \
198 } while(0)
199
200#define ARRAY_APPEND(array, elem) \
201 do { \
202 _ARRAY_GROW1(array); \
203 (array).p_elems[(array).i_size] = elem; \
204 (array).i_size++; \
205 } while(0)
206
207#define ARRAY_INSERT(array,elem,pos) \
208 do { \
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) ); \
213 } \
214 (array).p_elems[pos] = elem; \
215 (array).i_size++; \
216 } while(0)
217
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); \
221 } \
222}
223
224#define ARRAY_FIND(array, p, idx) \
225 TAB_FIND((array).i_size, (array).p_elems, p, idx)
226
227#define ARRAY_REMOVE(array,pos) \
228 do { \
229 if( (array).i_size - (pos) - 1 ) \
230 { \
231 memmove( (array).p_elems + (pos), (array).p_elems + (pos) + 1, \
232 ( (array).i_size - (pos) - 1 ) *sizeof(*(array).p_elems) );\
233 } \
234 (array).i_size--; \
235 _ARRAY_SHRINK(array); \
236 } while(0)
237
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)
242
243/* append ##item to index variable name to avoid variable shadowing warnings for
244 * nested loops */
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)
250
251
252/************************************************************************
253 * Dynamic arrays with progressive allocation (Preferred API)
254 ************************************************************************/
255typedef struct vlc_array_t
257 size_t i_count;
258 void ** pp_elems;
261static inline void vlc_array_init( vlc_array_t * p_array )
263 p_array->i_count = 0;
264 p_array->pp_elems = NULL;
265}
266
267static inline void vlc_array_clear( vlc_array_t * p_array )
269 free( p_array->pp_elems );
270 vlc_array_init( p_array );
271}
272
273/* Read */
274static inline size_t vlc_array_count( vlc_array_t * p_array )
276 return p_array->i_count;
277}
278
279#ifndef __cplusplus
280# define vlc_array_item_at_index(ar, idx) \
281 _Generic((ar), \
282 const vlc_array_t *: ((ar)->pp_elems[idx]), \
283 vlc_array_t *: ((ar)->pp_elems[idx]))
284#else
285static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
286{
287 return ar->pp_elems[idx];
288}
289
290static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
291 size_t idx )
292{
293 return ar->pp_elems[idx];
294}
295#endif
296
297static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
298 const void *elem )
299{
300 for( size_t i = 0; i < ar->i_count; i++ )
301 {
302 if( ar->pp_elems[i] == elem )
303 return i;
304 }
305 return -1;
306}
307
308/* Write */
309static inline int vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
311 void **pp = (void **)realloc( ar->pp_elems,
312 sizeof( void * ) * (ar->i_count + 1) );
313 if( unlikely(pp == NULL) )
314 return -1;
315
316 size_t tail = ar->i_count - idx;
317 if( tail > 0 )
318 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
319
320 pp[idx] = elem;
321 ar->i_count++;
322 ar->pp_elems = pp;
323 return 0;
324}
325
326static inline void vlc_array_insert_or_abort( vlc_array_t *ar, void *elem, int idx )
328 if( vlc_array_insert( ar, elem, idx ) )
329 abort();
330}
331
332static inline int vlc_array_append( vlc_array_t *ar, void *elem )
334 void **pp = (void **)realloc( ar->pp_elems,
335 sizeof( void * ) * (ar->i_count + 1) );
336 if( unlikely(pp == NULL) )
337 return -1;
338
339 pp[ar->i_count++] = elem;
340 ar->pp_elems = pp;
341 return 0;
342}
343
344static inline void vlc_array_append_or_abort( vlc_array_t *ar, void *elem )
346 if( vlc_array_append( ar, elem ) != 0 )
347 abort();
348}
349
350static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
352 void **pp = ar->pp_elems;
353 size_t tail = ar->i_count - idx - 1;
354
355 if( tail > 0 )
356 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
357
358 ar->i_count--;
359
360 if( ar->i_count > 0 )
361 {
362 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
363 if( likely(pp != NULL) )
364 ar->pp_elems = pp;
365 }
366 else
367 {
368 free( pp );
369 ar->pp_elems = NULL;
370 }
371}
372
373
374/************************************************************************
375 * Dictionaries
376 ************************************************************************/
377
378/* This function is not intended to be crypto-secure, we only want it to be
379 * fast and not suck too much. This one is pretty fast and did 0 collisions
380 * in wenglish's dictionary.
381 */
382static inline uint64_t DictHash( const char *psz_string, int hashsize )
384 uint64_t i_hash = 0;
385 if( psz_string )
386 {
387 while( *psz_string )
388 {
389 i_hash += *psz_string++;
390 i_hash += i_hash << 10;
391 i_hash ^= i_hash >> 8;
392 }
393 }
394 return i_hash % hashsize;
395}
396
397typedef struct vlc_dictionary_entry_t
399 char * psz_key;
400 void * p_value;
404typedef struct vlc_dictionary_t
406 int i_size;
410static void * const kVLCDictionaryNotFound = NULL;
412static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
414 p_dict->p_entries = NULL;
415
416 if( i_size > 0 )
417 {
418 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
419 if( !p_dict->p_entries )
420 i_size = 0;
421 }
422 p_dict->i_size = i_size;
423}
424
425static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
426 void ( * pf_free )( void * p_data, void * p_obj ),
427 void * p_obj )
428{
429 if( p_dict->p_entries )
430 {
431 for( int i = 0; i < p_dict->i_size; i++ )
432 {
433 vlc_dictionary_entry_t * p_current, * p_next;
434 p_current = p_dict->p_entries[i];
435 while( p_current )
436 {
437 p_next = p_current->p_next;
438 if( pf_free != NULL )
439 ( * pf_free )( p_current->p_value, p_obj );
440 free( p_current->psz_key );
441 free( p_current );
442 p_current = p_next;
443 }
444 }
445 free( p_dict->p_entries );
446 p_dict->p_entries = NULL;
447 }
448 p_dict->i_size = 0;
449}
450
451static inline int
452vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
454 if( !p_dict->p_entries )
455 return 0;
456
457 int i_pos = DictHash( psz_key, p_dict->i_size );
458 const vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
459 for( ; p_entry != NULL; p_entry = p_entry->p_next )
460 {
461 if( !strcmp( psz_key, p_entry->psz_key ) )
462 break;
463 }
464 return p_entry != NULL;
465}
466
467static inline void *
468vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
470 if( !p_dict->p_entries )
472
473 int i_pos = DictHash( psz_key, p_dict->i_size );
474 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
475
476 if( !p_entry )
478
479 /* Make sure we return the right item. (Hash collision) */
480 do {
481 if( !strcmp( psz_key, p_entry->psz_key ) )
482 return p_entry->p_value;
483 p_entry = p_entry->p_next;
484 } while( p_entry );
485
487}
488
489static inline int
492 vlc_dictionary_entry_t * p_entry;
493 int i, count = 0;
494
495 if( !p_dict->p_entries )
496 return 0;
497
498 for( i = 0; i < p_dict->i_size; i++ )
499 {
500 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
501 }
502 return count;
503}
504
505static inline bool
508 if( p_dict->p_entries )
509 for( int i = 0; i < p_dict->i_size; i++ )
510 if( p_dict->p_entries[i] )
511 return false;
512 return true;
513}
514
515static inline char **
518 vlc_dictionary_entry_t * p_entry;
519 char ** ppsz_ret;
520 int i, count = vlc_dictionary_keys_count( p_dict );
521
522 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
523 if( unlikely(!ppsz_ret) )
524 return NULL;
525
526 count = 0;
527 for( i = 0; i < p_dict->i_size; i++ )
528 {
529 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
530 ppsz_ret[count++] = strdup( p_entry->psz_key );
531 }
532 ppsz_ret[count] = NULL;
533 return ppsz_ret;
534}
535
536static inline void
537vlc_dictionary_insert_impl_( vlc_dictionary_t * p_dict, const char * psz_key,
538 void * p_value, bool rebuild )
539{
540 if( !p_dict->p_entries )
541 vlc_dictionary_init( p_dict, 1 );
542
543 int i_pos = DictHash( psz_key, p_dict->i_size );
544 vlc_dictionary_entry_t * p_entry;
545
546 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
547 p_entry->psz_key = strdup( psz_key );
548 p_entry->p_value = p_value;
549 p_entry->p_next = p_dict->p_entries[i_pos];
550 p_dict->p_entries[i_pos] = p_entry;
551 if( rebuild )
552 {
553 /* Count how many items there was */
554 int count;
555 for( count = 1; p_entry->p_next; count++ )
556 p_entry = p_entry->p_next;
557 if( count > 3 ) /* XXX: this need tuning */
558 {
559 /* Here it starts to be not good, rebuild a bigger dictionary */
560 struct vlc_dictionary_t new_dict;
561 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
562 int i;
563 vlc_dictionary_init( &new_dict, i_new_size );
564 for( i = 0; i < p_dict->i_size; i++ )
565 {
566 p_entry = p_dict->p_entries[i];
567 while( p_entry )
568 {
569 vlc_dictionary_insert_impl_( &new_dict, p_entry->psz_key,
570 p_entry->p_value,
571 false /* To avoid multiple rebuild loop */);
572 p_entry = p_entry->p_next;
573 }
574 }
575
576 vlc_dictionary_clear( p_dict, NULL, NULL );
577 p_dict->i_size = new_dict.i_size;
578 p_dict->p_entries = new_dict.p_entries;
579 }
580 }
581}
582
583static inline void
584vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
586 vlc_dictionary_insert_impl_( p_dict, psz_key, p_value, true );
587}
588
589static inline void
590vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
591 void ( * pf_free )( void * p_data, void * p_obj ),
592 void * p_obj )
593{
594 if( !p_dict->p_entries )
595 return;
596
597 int i_pos = DictHash( psz_key, p_dict->i_size );
598 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
599 vlc_dictionary_entry_t * p_prev;
600
601 if( !p_entry )
602 return; /* Not found, nothing to do */
603
604 /* Hash collision */
605 p_prev = NULL;
606 do {
607 if( !strcmp( psz_key, p_entry->psz_key ) )
608 {
609 if( pf_free != NULL )
610 ( * pf_free )( p_entry->p_value, p_obj );
611 if( !p_prev )
612 p_dict->p_entries[i_pos] = p_entry->p_next;
613 else
614 p_prev->p_next = p_entry->p_next;
615 free( p_entry->psz_key );
616 free( p_entry );
617 return;
618 }
619 p_prev = p_entry;
620 p_entry = p_entry->p_next;
621 } while( p_entry );
622
623 /* No key was found */
624}
625
626#ifdef __cplusplus
627// C++ helpers
628template <typename T>
629void vlc_delete_all( T &container )
630{
631 typename T::iterator it = container.begin();
632 while ( it != container.end() )
633 {
634 delete *it;
635 ++it;
636 }
637 container.clear();
638}
639
640#endif
641
642#endif
size_t count
Definition: core.c:403
#define p(t)
#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 *)