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
282# define vlc_array_item_at_index(ar, idx) \
283 _Generic((ar), \
284 const vlc_array_t *: ((ar)->pp_elems[idx]), \
285 vlc_array_t *: ((ar)->pp_elems[idx]))
286#else
287static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
288{
289 return ar->pp_elems[idx];
290}
291
292static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
293 size_t idx )
294{
295 return ar->pp_elems[idx];
296}
297#endif
298
299static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
300 const void *elem )
301{
302 for( size_t i = 0; i < ar->i_count; i++ )
303 {
304 if( ar->pp_elems[i] == elem )
305 return i;
306 }
307 return -1;
308}
309
310/* Write */
311static inline int vlc_array_insert( vlc_array_t *ar, void *elem, int idx )
313 void **pp = (void **)realloc( ar->pp_elems,
314 sizeof( void * ) * (ar->i_count + 1) );
315 if( unlikely(pp == NULL) )
316 return -1;
317
318 size_t tail = ar->i_count - idx;
319 if( tail > 0 )
320 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
321
322 pp[idx] = elem;
323 ar->i_count++;
324 ar->pp_elems = pp;
325 return 0;
326}
327
328static inline void vlc_array_insert_or_abort( vlc_array_t *ar, void *elem, int idx )
330 if( vlc_array_insert( ar, elem, idx ) )
331 abort();
332}
333
334static inline int vlc_array_append( vlc_array_t *ar, void *elem )
336 void **pp = (void **)realloc( ar->pp_elems,
337 sizeof( void * ) * (ar->i_count + 1) );
338 if( unlikely(pp == NULL) )
339 return -1;
340
341 pp[ar->i_count++] = elem;
342 ar->pp_elems = pp;
343 return 0;
344}
345
346static inline void vlc_array_append_or_abort( vlc_array_t *ar, void *elem )
348 if( vlc_array_append( ar, elem ) != 0 )
349 abort();
350}
351
352static inline void vlc_array_remove( vlc_array_t *ar, size_t idx )
354 void **pp = ar->pp_elems;
355 size_t tail = ar->i_count - idx - 1;
356
357 if( tail > 0 )
358 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
359
360 ar->i_count--;
361
362 if( ar->i_count > 0 )
363 {
364 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
365 if( likely(pp != NULL) )
366 ar->pp_elems = pp;
367 }
368 else
369 {
370 free( pp );
371 ar->pp_elems = NULL;
372 }
373}
374
375
376/************************************************************************
377 * Dictionaries
378 ************************************************************************/
379
380/* This function is not intended to be crypto-secure, we only want it to be
381 * fast and not suck too much. This one is pretty fast and did 0 collisions
382 * in wenglish's dictionary.
383 */
384static inline uint64_t DictHash( const char *psz_string, int hashsize )
386 uint64_t i_hash = 0;
387 if( psz_string )
388 {
389 while( *psz_string )
390 {
391 i_hash += *psz_string++;
392 i_hash += i_hash << 10;
393 i_hash ^= i_hash >> 8;
394 }
395 }
396 return i_hash % hashsize;
397}
398
399typedef struct vlc_dictionary_entry_t
406typedef struct vlc_dictionary_t
412static void * const kVLCDictionaryNotFound = NULL;
414static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
416 p_dict->p_entries = NULL;
417
418 if( i_size > 0 )
419 {
420 p_dict->p_entries = (vlc_dictionary_entry_t **)calloc( i_size, sizeof(*p_dict->p_entries) );
421 if( !p_dict->p_entries )
422 i_size = 0;
423 }
424 p_dict->i_size = i_size;
425}
426
427static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
428 void ( * pf_free )( void * p_data, void * p_obj ),
429 void * p_obj )
430{
431 if( p_dict->p_entries )
432 {
433 for( int i = 0; i < p_dict->i_size; i++ )
434 {
435 vlc_dictionary_entry_t * p_current, * p_next;
436 p_current = p_dict->p_entries[i];
437 while( p_current )
438 {
439 p_next = p_current->p_next;
440 if( pf_free != NULL )
441 ( * pf_free )( p_current->p_value, p_obj );
442 free( p_current->psz_key );
443 free( p_current );
444 p_current = p_next;
445 }
446 }
447 free( p_dict->p_entries );
448 p_dict->p_entries = NULL;
449 }
450 p_dict->i_size = 0;
451}
452
453static inline int
454vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
456 if( !p_dict->p_entries )
457 return 0;
458
459 int i_pos = DictHash( psz_key, p_dict->i_size );
460 const vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
461 for( ; p_entry != NULL; p_entry = p_entry->p_next )
462 {
463 if( !strcmp( psz_key, p_entry->psz_key ) )
464 break;
465 }
466 return p_entry != NULL;
467}
468
469static inline void *
470vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
472 if( !p_dict->p_entries )
474
475 int i_pos = DictHash( psz_key, p_dict->i_size );
476 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
477
478 if( !p_entry )
480
481 /* Make sure we return the right item. (Hash collision) */
482 do {
483 if( !strcmp( psz_key, p_entry->psz_key ) )
484 return p_entry->p_value;
485 p_entry = p_entry->p_next;
486 } while( p_entry );
487
489}
490
491static inline int
494 vlc_dictionary_entry_t * p_entry;
495 int i, count = 0;
496
497 if( !p_dict->p_entries )
498 return 0;
499
500 for( i = 0; i < p_dict->i_size; i++ )
501 {
502 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
503 }
504 return count;
505}
506
507static inline bool
510 if( p_dict->p_entries )
511 for( int i = 0; i < p_dict->i_size; i++ )
512 if( p_dict->p_entries[i] )
513 return false;
514 return true;
515}
516
517static inline char **
520 vlc_dictionary_entry_t * p_entry;
521 char ** ppsz_ret;
522 int i, count = vlc_dictionary_keys_count( p_dict );
523
524 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
525 if( unlikely(!ppsz_ret) )
526 return NULL;
527
528 count = 0;
529 for( i = 0; i < p_dict->i_size; i++ )
530 {
531 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next )
532 ppsz_ret[count++] = strdup( p_entry->psz_key );
533 }
534 ppsz_ret[count] = NULL;
535 return ppsz_ret;
536}
537
538static inline void
539vlc_dictionary_insert_impl_( vlc_dictionary_t * p_dict, const char * psz_key,
540 void * p_value, bool rebuild )
541{
542 if( !p_dict->p_entries )
543 vlc_dictionary_init( p_dict, 1 );
544
545 int i_pos = DictHash( psz_key, p_dict->i_size );
546 vlc_dictionary_entry_t * p_entry;
547
548 p_entry = (vlc_dictionary_entry_t *)malloc(sizeof(*p_entry));
549 p_entry->psz_key = strdup( psz_key );
550 p_entry->p_value = p_value;
551 p_entry->p_next = p_dict->p_entries[i_pos];
552 p_dict->p_entries[i_pos] = p_entry;
553 if( rebuild )
554 {
555 /* Count how many items there was */
556 int count;
557 for( count = 1; p_entry->p_next; count++ )
558 p_entry = p_entry->p_next;
559 if( count > 3 ) /* XXX: this need tuning */
560 {
561 /* Here it starts to be not good, rebuild a bigger dictionary */
562 struct vlc_dictionary_t new_dict;
563 int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */
564 int i;
565 vlc_dictionary_init( &new_dict, i_new_size );
566 for( i = 0; i < p_dict->i_size; i++ )
567 {
568 p_entry = p_dict->p_entries[i];
569 while( p_entry )
570 {
571 vlc_dictionary_insert_impl_( &new_dict, p_entry->psz_key,
572 p_entry->p_value,
573 false /* To avoid multiple rebuild loop */);
574 p_entry = p_entry->p_next;
575 }
576 }
577
578 vlc_dictionary_clear( p_dict, NULL, NULL );
579 p_dict->i_size = new_dict.i_size;
580 p_dict->p_entries = new_dict.p_entries;
581 }
582 }
583}
584
585static inline void
586vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value )
588 vlc_dictionary_insert_impl_( p_dict, psz_key, p_value, true );
589}
590
591static inline void
592vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key,
593 void ( * pf_free )( void * p_data, void * p_obj ),
594 void * p_obj )
595{
596 if( !p_dict->p_entries )
597 return;
598
599 int i_pos = DictHash( psz_key, p_dict->i_size );
600 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
601 vlc_dictionary_entry_t * p_prev;
602
603 if( !p_entry )
604 return; /* Not found, nothing to do */
605
606 /* Hash collision */
607 p_prev = NULL;
608 do {
609 if( !strcmp( psz_key, p_entry->psz_key ) )
610 {
611 if( pf_free != NULL )
612 ( * pf_free )( p_entry->p_value, p_obj );
613 if( !p_prev )
614 p_dict->p_entries[i_pos] = p_entry->p_next;
615 else
616 p_prev->p_next = p_entry->p_next;
617 free( p_entry->psz_key );
618 free( p_entry );
619 return;
620 }
621 p_prev = p_entry;
622 p_entry = p_entry->p_next;
623 } while( p_entry );
624
625 /* No key was found */
626}
627
628#ifdef __cplusplus
629// C++ helpers
630template <typename T>
631void vlc_delete_all( T &container )
632{
633 typename T::iterator it = container.begin();
634 while ( it != container.end() )
635 {
636 delete *it;
637 ++it;
638 }
639 container.clear();
640}
641
642#endif
643
644#endif
size_t count
Definition core.c:403
#define p(t)
#define unlikely(p)
Predicted false condition.
Definition vlc_common.h:246
#define likely(p)
Predicted true condition.
Definition vlc_common.h:237
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:401
void * p_value
Definition vlc_arrays.h:403
struct vlc_dictionary_entry_t * p_next
Definition vlc_arrays.h:404
char * psz_key
Definition vlc_arrays.h:402
Definition vlc_arrays.h:408
int i_size
Definition vlc_arrays.h:409
vlc_dictionary_entry_t ** p_entries
Definition vlc_arrays.h:410
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:519
static int vlc_array_append(vlc_array_t *ar, void *elem)
Definition vlc_arrays.h:335
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:593
static void vlc_array_insert_or_abort(vlc_array_t *ar, void *elem, int idx)
Definition vlc_arrays.h:329
static void *const kVLCDictionaryNotFound
Definition vlc_arrays.h:413
static void * vlc_dictionary_value_for_key(const vlc_dictionary_t *p_dict, const char *psz_key)
Definition vlc_arrays.h:471
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:428
static void vlc_array_init(vlc_array_t *p_array)
Definition vlc_arrays.h:264
static bool vlc_dictionary_is_empty(const vlc_dictionary_t *p_dict)
Definition vlc_arrays.h:509
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:283
static void vlc_array_remove(vlc_array_t *ar, size_t idx)
Definition vlc_arrays.h:353
static void vlc_array_append_or_abort(vlc_array_t *ar, void *elem)
Definition vlc_arrays.h:347
static ssize_t vlc_array_index_of_item(const vlc_array_t *ar, const void *elem)
Definition vlc_arrays.h:300
static int vlc_dictionary_has_key(const vlc_dictionary_t *p_dict, const char *psz_key)
Definition vlc_arrays.h:455
static size_t vlc_array_count(const vlc_array_t *p_array)
Definition vlc_arrays.h:277
static int vlc_dictionary_keys_count(const vlc_dictionary_t *p_dict)
Definition vlc_arrays.h:493
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:312
static void vlc_dictionary_init(vlc_dictionary_t *p_dict, int i_size)
Definition vlc_arrays.h:415
static void vlc_dictionary_insert_impl_(vlc_dictionary_t *p_dict, const char *psz_key, void *p_value, bool rebuild)
Definition vlc_arrays.h:540
static uint64_t DictHash(const char *psz_string, int hashsize)
Definition vlc_arrays.h:385
static void vlc_dictionary_insert(vlc_dictionary_t *p_dict, const char *psz_key, void *p_value)
Definition vlc_arrays.h:587
This file is a collection of common definitions and types.
char * strdup(const char *)