codec: add hxxx_helper test decoder
[vlc.git] / include / vlc_arrays.h
blobb558422f9361091f78895189af46259974d35c98
1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
6 * Authors: Samuel Hocevar <sam@zoy.org>
7 * Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #ifndef VLC_ARRAYS_H_
25 #define VLC_ARRAYS_H_
27 /**
28 * \file
29 * This file defines functions, structures and macros for handling arrays in vlc
32 /* realloc() that never fails *if* downsizing */
33 static inline void *realloc_down( void *ptr, size_t size )
35 void *ret = realloc( ptr, size );
36 return ret ? ret : ptr;
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).
46 static 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;
54 #define TAB_INIT( count, tab ) \
55 do { \
56 (count) = 0; \
57 (tab) = NULL; \
58 } while(0)
60 #define TAB_CLEAN( count, tab ) \
61 do { \
62 free( tab ); \
63 (count)= 0; \
64 (tab)= NULL; \
65 } while(0)
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)
78 #define TAB_APPEND( count, tab, p ) \
79 TAB_APPEND_CAST( , count, tab, p )
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)
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; \
103 } while(0)
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)
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)
127 #define TAB_INSERT( count, tab, p, index ) \
128 TAB_INSERT_CAST( , count, tab, p, index )
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
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 \
152 answer = mid; break; \
155 } while(0)
158 /************************************************************************
159 * Dynamic arrays with progressive allocation
160 ************************************************************************/
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(); \
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) ) \
177 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
179 /* API */
180 #define DECL_ARRAY(type) struct { \
181 int i_alloc; \
182 int i_size; \
183 type *p_elems; \
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)
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)
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)
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) ); \
216 (array).p_elems[pos] = elem; \
217 (array).i_size++; \
218 } while(0)
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); \
226 #define ARRAY_FIND(array, p, idx) \
227 TAB_FIND((array).i_size, (array).p_elems, p, idx)
229 #define ARRAY_REMOVE(array,pos) \
230 do { \
231 if( (array).i_size - (pos) - 1 ) \
233 memmove( (array).p_elems + (pos), (array).p_elems + (pos) + 1, \
234 ( (array).i_size - (pos) - 1 ) *sizeof(*(array).p_elems) );\
236 (array).i_size--; \
237 _ARRAY_SHRINK(array); \
238 } while(0)
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)
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)
254 /************************************************************************
255 * Dynamic arrays with progressive allocation (Preferred API)
256 ************************************************************************/
257 typedef struct vlc_array_t
259 size_t i_count;
260 void ** pp_elems;
261 } vlc_array_t;
263 static inline void vlc_array_init( vlc_array_t * p_array )
265 p_array->i_count = 0;
266 p_array->pp_elems = NULL;
269 static inline void vlc_array_clear( vlc_array_t * p_array )
271 free( p_array->pp_elems );
272 vlc_array_init( p_array );
275 /* Read */
276 static inline size_t vlc_array_count( vlc_array_t * p_array )
278 return p_array->i_count;
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
287 static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
289 return ar->pp_elems[idx];
292 static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
293 size_t idx )
295 return ar->pp_elems[idx];
297 #endif
299 static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
300 const void *elem )
302 for( size_t i = 0; i < ar->i_count; i++ )
304 if( ar->pp_elems[i] == elem )
305 return i;
307 return -1;
310 /* Write */
311 static 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;
318 size_t tail = ar->i_count - idx;
319 if( tail > 0 )
320 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
322 pp[idx] = elem;
323 ar->i_count++;
324 ar->pp_elems = pp;
325 return 0;
328 static 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();
334 static 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;
341 pp[ar->i_count++] = elem;
342 ar->pp_elems = pp;
343 return 0;
346 static inline void vlc_array_append_or_abort( vlc_array_t *ar, void *elem )
348 if( vlc_array_append( ar, elem ) != 0 )
349 abort();
352 static 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;
357 if( tail > 0 )
358 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
360 ar->i_count--;
362 if( ar->i_count > 0 )
364 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
365 if( likely(pp != NULL) )
366 ar->pp_elems = pp;
368 else
370 free( pp );
371 ar->pp_elems = NULL;
376 /************************************************************************
377 * Dictionaries
378 ************************************************************************/
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.
384 static inline uint64_t DictHash( const char *psz_string, int hashsize )
386 uint64_t i_hash = 0;
387 if( psz_string )
389 while( *psz_string )
391 i_hash += *psz_string++;
392 i_hash += i_hash << 10;
393 i_hash ^= i_hash >> 8;
396 return i_hash % hashsize;
399 typedef struct vlc_dictionary_entry_t
401 char * psz_key;
402 void * p_value;
403 struct vlc_dictionary_entry_t * p_next;
404 } vlc_dictionary_entry_t;
406 typedef struct vlc_dictionary_t
408 int i_size;
409 vlc_dictionary_entry_t ** p_entries;
410 } vlc_dictionary_t;
412 static void * const kVLCDictionaryNotFound = NULL;
414 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
416 p_dict->p_entries = NULL;
418 if( i_size > 0 )
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;
424 p_dict->i_size = i_size;
427 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
428 void ( * pf_free )( void * p_data, void * p_obj ),
429 void * p_obj )
431 if( p_dict->p_entries )
433 for( int i = 0; i < p_dict->i_size; i++ )
435 vlc_dictionary_entry_t * p_current, * p_next;
436 p_current = p_dict->p_entries[i];
437 while( p_current )
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;
447 free( p_dict->p_entries );
448 p_dict->p_entries = NULL;
450 p_dict->i_size = 0;
453 static inline int
454 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
456 if( !p_dict->p_entries )
457 return 0;
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 )
463 if( !strcmp( psz_key, p_entry->psz_key ) )
464 break;
466 return p_entry != NULL;
469 static inline void *
470 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
472 if( !p_dict->p_entries )
473 return kVLCDictionaryNotFound;
475 int i_pos = DictHash( psz_key, p_dict->i_size );
476 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
478 if( !p_entry )
479 return kVLCDictionaryNotFound;
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 );
488 return kVLCDictionaryNotFound;
491 static inline int
492 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
494 vlc_dictionary_entry_t * p_entry;
495 int i, count = 0;
497 if( !p_dict->p_entries )
498 return 0;
500 for( i = 0; i < p_dict->i_size; i++ )
502 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
504 return count;
507 static inline bool
508 vlc_dictionary_is_empty( const vlc_dictionary_t * p_dict )
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;
517 static inline char **
518 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
520 vlc_dictionary_entry_t * p_entry;
521 char ** ppsz_ret;
522 int i, count = vlc_dictionary_keys_count( p_dict );
524 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
525 if( unlikely(!ppsz_ret) )
526 return NULL;
528 count = 0;
529 for( i = 0; i < p_dict->i_size; i++ )
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 );
534 ppsz_ret[count] = NULL;
535 return ppsz_ret;
538 static inline void
539 vlc_dictionary_insert_impl_( vlc_dictionary_t * p_dict, const char * psz_key,
540 void * p_value, bool rebuild )
542 if( !p_dict->p_entries )
543 vlc_dictionary_init( p_dict, 1 );
545 int i_pos = DictHash( psz_key, p_dict->i_size );
546 vlc_dictionary_entry_t * p_entry;
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 )
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 */
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++ )
568 p_entry = p_dict->p_entries[i];
569 while( p_entry )
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;
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;
585 static inline void
586 vlc_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 );
591 static inline void
592 vlc_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 )
596 if( !p_dict->p_entries )
597 return;
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;
603 if( !p_entry )
604 return; /* Not found, nothing to do */
606 /* Hash collision */
607 p_prev = NULL;
608 do {
609 if( !strcmp( psz_key, p_entry->psz_key ) )
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;
621 p_prev = p_entry;
622 p_entry = p_entry->p_next;
623 } while( p_entry );
625 /* No key was found */
628 #ifdef __cplusplus
629 // C++ helpers
630 template <typename T>
631 void vlc_delete_all( T &container )
633 typename T::iterator it = container.begin();
634 while ( it != container.end() )
636 delete *it;
637 ++it;
639 container.clear();
642 #endif
644 #endif