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