gl: android: precise rgba sampling with samplerExternalOES
[vlc.git] / include / vlc_arrays.h
blobb548ec2dc1ad3fbfc860821068b706e194384afd
1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
5 * $Id$
7 * Authors: Samuel Hocevar <sam@zoy.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #ifndef VLC_ARRAYS_H_
26 #define VLC_ARRAYS_H_
28 /**
29 * \file
30 * This file defines functions, structures and macros for handling arrays in vlc
33 /* realloc() that never fails *if* downsizing */
34 static inline void *realloc_down( void *ptr, size_t size )
36 void *ret = realloc( ptr, size );
37 return ret ? ret : ptr;
40 /**
41 * This wrapper around realloc() will free the input pointer when
42 * realloc() returns NULL. The use case ptr = realloc(ptr, newsize) will
43 * cause a memory leak when ptr pointed to a heap allocation before,
44 * leaving the buffer allocated but unreferenced. vlc_realloc() is a
45 * drop-in replacement for that use case (and only that use case).
47 static inline void *realloc_or_free( void *p, size_t sz )
49 void *n = realloc(p,sz);
50 if( !n )
51 free(p);
52 return n;
55 #define TAB_INIT( count, tab ) \
56 do { \
57 (count) = 0; \
58 (tab) = NULL; \
59 } while(0)
61 #define TAB_CLEAN( count, tab ) \
62 do { \
63 free( tab ); \
64 (count)= 0; \
65 (tab)= NULL; \
66 } while(0)
68 #define TAB_APPEND_CAST( cast, count, tab, p ) \
69 do { \
70 if( (count) > 0 ) \
71 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
72 else \
73 (tab) = cast malloc( sizeof( *(tab) ) ); \
74 if( !(tab) ) abort(); \
75 (tab)[count] = (p); \
76 (count)++; \
77 } while(0)
79 #define TAB_APPEND( count, tab, p ) \
80 TAB_APPEND_CAST( , count, tab, p )
82 #define TAB_FIND( count, tab, p, idx ) \
83 do { \
84 for( (idx) = 0; (idx) < (count); (idx)++ ) \
85 if( (tab)[(idx)] == (p) ) \
86 break; \
87 if( (idx) >= (count) ) \
88 (idx) = -1; \
89 } while(0)
92 #define TAB_ERASE( count, tab, index ) \
93 do { \
94 if( (count) > 1 ) \
95 memmove( (tab) + (index), \
96 (tab) + (index) + 1, \
97 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
98 (count)--; \
99 if( (count) == 0 ) \
101 free( tab ); \
102 (tab) = NULL; \
104 } while(0)
106 #define TAB_REMOVE( count, tab, p ) \
107 do { \
108 int i_index; \
109 TAB_FIND( count, tab, p, i_index ); \
110 if( i_index >= 0 ) \
111 TAB_ERASE( count, tab, i_index ); \
112 } while(0)
114 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
115 if( (count) > 0 ) \
116 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
117 else \
118 (tab) = cast malloc( sizeof( *(tab) ) ); \
119 if( !(tab) ) abort(); \
120 if( (count) - (index) > 0 ) \
121 memmove( (tab) + (index) + 1, \
122 (tab) + (index), \
123 ((count) - (index)) * sizeof( *(tab) ) );\
124 (tab)[(index)] = (p); \
125 (count)++; \
126 } while(0)
128 #define TAB_INSERT( count, tab, p, index ) \
129 TAB_INSERT_CAST( , count, tab, p, index )
132 * Binary search in a sorted array. The key must be comparable by < and >
133 * \param entries array of entries
134 * \param count number of entries
135 * \param elem key to check within an entry (like .id, or ->i_id)
136 * \param zetype type of the key
137 * \param key value of the key
138 * \param answer index of answer within the array. -1 if not found
140 #define BSEARCH( entries, count, elem, zetype, key, answer ) \
141 do { \
142 int low = 0, high = count - 1; \
143 answer = -1; \
144 while( low <= high ) {\
145 int mid = ((unsigned int)low + (unsigned int)high) >> 1;\
146 zetype mid_val = entries[mid] elem;\
147 if( mid_val < key ) \
148 low = mid + 1; \
149 else if ( mid_val > key ) \
150 high = mid -1; \
151 else \
153 answer = mid; break; \
156 } while(0)
159 /************************************************************************
160 * Dynamic arrays with progressive allocation
161 ************************************************************************/
163 /* Internal functions */
164 #define _ARRAY_ALLOC(array, newsize) { \
165 (array).i_alloc = newsize; \
166 (array).p_elems = realloc( (array).p_elems, (array).i_alloc * \
167 sizeof(*(array).p_elems) ); \
168 if( !(array).p_elems ) abort(); \
171 #define _ARRAY_GROW1(array) { \
172 if( (array).i_alloc < 10 ) \
173 _ARRAY_ALLOC(array, 10 ) \
174 else if( (array).i_alloc == (array).i_size ) \
175 _ARRAY_ALLOC(array, (int)((array).i_alloc * 1.5) ) \
178 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
180 /* API */
181 #define DECL_ARRAY(type) struct { \
182 int i_alloc; \
183 int i_size; \
184 type *p_elems; \
187 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
189 #define ARRAY_INIT(array) \
190 do { \
191 (array).i_alloc = 0; \
192 (array).i_size = 0; \
193 (array).p_elems = NULL; \
194 } while(0)
196 #define ARRAY_RESET(array) \
197 do { \
198 (array).i_alloc = 0; \
199 (array).i_size = 0; \
200 free( (array).p_elems ); (array).p_elems = NULL; \
201 } while(0)
203 #define ARRAY_APPEND(array, elem) \
204 do { \
205 _ARRAY_GROW1(array); \
206 (array).p_elems[(array).i_size] = elem; \
207 (array).i_size++; \
208 } while(0)
210 #define ARRAY_INSERT(array,elem,pos) \
211 do { \
212 _ARRAY_GROW1(array); \
213 if( (array).i_size - pos ) { \
214 memmove( (array).p_elems + pos + 1, (array).p_elems + pos, \
215 ((array).i_size-pos) * sizeof(*(array).p_elems) ); \
217 (array).p_elems[pos] = elem; \
218 (array).i_size++; \
219 } while(0)
221 #define _ARRAY_SHRINK(array) { \
222 if( (array).i_size > 10 && (array).i_size < (int)((array).i_alloc / 1.5) ) { \
223 _ARRAY_ALLOC(array, (array).i_size + 5); \
227 #define ARRAY_REMOVE(array,pos) \
228 do { \
229 if( (array).i_size - (pos) - 1 ) \
231 memmove( (array).p_elems + pos, (array).p_elems + pos + 1, \
232 ( (array).i_size - pos - 1 ) *sizeof(*(array).p_elems) ); \
234 (array).i_size--; \
235 _ARRAY_SHRINK(array); \
236 } while(0)
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)
243 #define FOREACH_ARRAY( item, array ) { \
244 int fe_idx; \
245 for( fe_idx = 0 ; fe_idx < (array).i_size ; fe_idx++ ) \
247 item = (array).p_elems[fe_idx];
249 #define FOREACH_END() } }
252 /************************************************************************
253 * Dynamic arrays with progressive allocation (Preferred API)
254 ************************************************************************/
255 typedef struct vlc_array_t
257 size_t i_count;
258 void ** pp_elems;
259 } vlc_array_t;
261 static inline void vlc_array_init( vlc_array_t * p_array )
263 p_array->i_count = 0;
264 p_array->pp_elems = NULL;
267 static inline void vlc_array_clear( vlc_array_t * p_array )
269 free( p_array->pp_elems );
270 vlc_array_init( p_array );
273 /* Read */
274 static inline size_t vlc_array_count( vlc_array_t * p_array )
276 return p_array->i_count;
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
285 static inline void *vlc_array_item_at_index( vlc_array_t *ar, size_t idx )
287 return ar->pp_elems[idx];
290 static inline const void *vlc_array_item_at_index( const vlc_array_t *ar,
291 size_t idx )
293 return ar->pp_elems[idx];
295 #endif
297 static inline ssize_t vlc_array_index_of_item( const vlc_array_t *ar,
298 const void *elem )
300 for( size_t i = 0; i < ar->i_count; i++ )
302 if( ar->pp_elems[i] == elem )
303 return i;
305 return -1;
308 /* Write */
309 static 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;
316 size_t tail = ar->i_count - idx;
317 if( tail > 0 )
318 memmove( pp + idx + 1, pp + idx, sizeof( void * ) * tail );
320 pp[idx] = elem;
321 ar->i_count++;
322 ar->pp_elems = pp;
323 return 0;
326 static 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();
332 static 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;
339 pp[ar->i_count++] = elem;
340 ar->pp_elems = pp;
341 return 0;
344 static inline void vlc_array_append_or_abort( vlc_array_t *ar, void *elem )
346 if( vlc_array_append( ar, elem ) != 0 )
347 abort();
350 static 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;
355 if( tail > 0 )
356 memmove( pp + idx, pp + idx + 1, sizeof( void * ) * tail );
358 ar->i_count--;
360 if( ar->i_count > 0 )
362 pp = (void **)realloc( pp, sizeof( void * ) * ar->i_count );
363 if( likely(pp != NULL) )
364 ar->pp_elems = pp;
366 else
368 free( pp );
369 ar->pp_elems = NULL;
374 /************************************************************************
375 * Dictionaries
376 ************************************************************************/
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.
382 static inline uint64_t DictHash( const char *psz_string, int hashsize )
384 uint64_t i_hash = 0;
385 if( psz_string )
387 while( *psz_string )
389 i_hash += *psz_string++;
390 i_hash += i_hash << 10;
391 i_hash ^= i_hash >> 8;
394 return i_hash % hashsize;
397 typedef struct vlc_dictionary_entry_t
399 char * psz_key;
400 void * p_value;
401 struct vlc_dictionary_entry_t * p_next;
402 } vlc_dictionary_entry_t;
404 typedef struct vlc_dictionary_t
406 int i_size;
407 vlc_dictionary_entry_t ** p_entries;
408 } vlc_dictionary_t;
410 static void * const kVLCDictionaryNotFound = NULL;
412 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
414 p_dict->p_entries = NULL;
416 if( i_size > 0 )
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;
422 p_dict->i_size = i_size;
425 static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict,
426 void ( * pf_free )( void * p_data, void * p_obj ),
427 void * p_obj )
429 if( p_dict->p_entries )
431 for( int i = 0; i < p_dict->i_size; i++ )
433 vlc_dictionary_entry_t * p_current, * p_next;
434 p_current = p_dict->p_entries[i];
435 while( p_current )
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;
445 free( p_dict->p_entries );
446 p_dict->p_entries = NULL;
448 p_dict->i_size = 0;
451 static inline int
452 vlc_dictionary_has_key( const vlc_dictionary_t * p_dict, const char * psz_key )
454 if( !p_dict->p_entries )
455 return 0;
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 )
461 if( !strcmp( psz_key, p_entry->psz_key ) )
462 break;
464 return p_entry != NULL;
467 static inline void *
468 vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key )
470 if( !p_dict->p_entries )
471 return kVLCDictionaryNotFound;
473 int i_pos = DictHash( psz_key, p_dict->i_size );
474 vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos];
476 if( !p_entry )
477 return kVLCDictionaryNotFound;
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 );
486 return kVLCDictionaryNotFound;
489 static inline int
490 vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict )
492 vlc_dictionary_entry_t * p_entry;
493 int i, count = 0;
495 if( !p_dict->p_entries )
496 return 0;
498 for( i = 0; i < p_dict->i_size; i++ )
500 for( p_entry = p_dict->p_entries[i]; p_entry; p_entry = p_entry->p_next ) count++;
502 return count;
505 static inline bool
506 vlc_dictionary_is_empty( const vlc_dictionary_t * p_dict )
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;
515 static inline char **
516 vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
518 vlc_dictionary_entry_t * p_entry;
519 char ** ppsz_ret;
520 int i, count = vlc_dictionary_keys_count( p_dict );
522 ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
523 if( unlikely(!ppsz_ret) )
524 return NULL;
526 count = 0;
527 for( i = 0; i < p_dict->i_size; i++ )
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 );
532 ppsz_ret[count] = NULL;
533 return ppsz_ret;
536 static inline void
537 vlc_dictionary_insert_impl_( vlc_dictionary_t * p_dict, const char * psz_key,
538 void * p_value, bool rebuild )
540 if( !p_dict->p_entries )
541 vlc_dictionary_init( p_dict, 1 );
543 int i_pos = DictHash( psz_key, p_dict->i_size );
544 vlc_dictionary_entry_t * p_entry;
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 )
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 */
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++ )
566 p_entry = p_dict->p_entries[i];
567 while( p_entry )
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;
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;
583 static inline void
584 vlc_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 );
589 static inline void
590 vlc_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 )
594 if( !p_dict->p_entries )
595 return;
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;
601 if( !p_entry )
602 return; /* Not found, nothing to do */
604 /* Hash collision */
605 p_prev = NULL;
606 do {
607 if( !strcmp( psz_key, p_entry->psz_key ) )
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;
619 p_prev = p_entry;
620 p_entry = p_entry->p_next;
621 } while( p_entry );
623 /* No key was found */
626 #ifdef __cplusplus
627 // C++ helpers
628 template <typename T>
629 void vlc_delete_all( T &container )
631 typename T::iterator it = container.begin();
632 while ( it != container.end() )
634 delete *it;
635 ++it;
637 container.clear();
640 #endif
642 #endif