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