1 /*****************************************************************************
2 * vlc_arrays.h : Arrays and data structures handling
3 *****************************************************************************
4 * Copyright (C) 1999-2004 VLC authors and VideoLAN
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 *****************************************************************************/
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
;
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
);
55 #define TAB_INIT( count, tab ) \
61 #define TAB_CLEAN( count, tab ) \
68 #define TAB_APPEND_CAST( cast, count, tab, p ) \
71 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
73 (tab) = cast malloc( sizeof( *(tab) ) ); \
74 if( !(tab) ) abort(); \
79 #define TAB_APPEND( count, tab, p ) \
80 TAB_APPEND_CAST( , count, tab, p )
82 #define TAB_FIND( count, tab, p, idx ) \
84 for( (idx) = 0; (idx) < (count); (idx)++ ) \
85 if( (tab)[(idx)] == (p) ) \
87 if( (idx) >= (count) ) \
92 #define TAB_ERASE( count, tab, index ) \
95 memmove( (tab) + (index), \
96 (tab) + (index) + 1, \
97 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
106 #define TAB_REMOVE( count, tab, p ) \
109 TAB_FIND( count, tab, p, i_index ); \
111 TAB_ERASE( count, tab, i_index ); \
114 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
116 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
118 (tab) = cast malloc( sizeof( *(tab) ) ); \
119 if( !(tab) ) abort(); \
120 if( (count) - (index) > 0 ) \
121 memmove( (tab) + (index) + 1, \
123 ((count) - (index)) * sizeof( *(tab) ) );\
124 (tab)[(index)] = (p); \
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 ) \
142 int low = 0, high = count - 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 ) \
149 else if ( mid_val > key ) \
153 answer = mid; break; \
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]))
181 #define DECL_ARRAY(type) struct { \
187 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
189 #define ARRAY_INIT(array) \
191 (array).i_alloc = 0; \
192 (array).i_size = 0; \
193 (array).p_elems = NULL; \
196 #define ARRAY_RESET(array) \
198 (array).i_alloc = 0; \
199 (array).i_size = 0; \
200 free( (array).p_elems ); (array).p_elems = NULL; \
203 #define ARRAY_APPEND(array, elem) \
205 _ARRAY_GROW1(array); \
206 (array).p_elems[(array).i_size] = elem; \
210 #define ARRAY_INSERT(array,elem,pos) \
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; \
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) \
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) ); \
235 _ARRAY_SHRINK(array); \
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 ) { \
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
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
);
274 static inline size_t vlc_array_count( vlc_array_t
* p_array
)
276 return p_array
->i_count
;
280 # define vlc_array_item_at_index(ar, idx) \
282 const vlc_array_t *: ((ar)->pp_elems[idx]), \
283 vlc_array_t *: ((ar)->pp_elems[idx]))
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
,
293 return ar
->pp_elems
[idx
];
297 static inline ssize_t
vlc_array_index_of_item( const vlc_array_t
*ar
,
300 for( size_t i
= 0; i
< ar
->i_count
; i
++ )
302 if( ar
->pp_elems
[i
] == elem
)
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
) )
316 size_t tail
= ar
->i_count
- idx
;
318 memmove( pp
+ idx
+ 1, pp
+ idx
, sizeof( void * ) * tail
);
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
) )
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
) )
339 pp
[ar
->i_count
++] = elem
;
344 static inline void vlc_array_append_or_abort( vlc_array_t
*ar
, void *elem
)
346 if( vlc_array_append( ar
, elem
) != 0 )
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;
356 memmove( pp
+ idx
, pp
+ idx
+ 1, sizeof( void * ) * tail
);
360 if( ar
->i_count
> 0 )
362 pp
= (void **)realloc( pp
, sizeof( void * ) * ar
->i_count
);
363 if( likely(pp
!= NULL
) )
374 /************************************************************************
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
)
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
401 struct vlc_dictionary_entry_t
* p_next
;
402 } vlc_dictionary_entry_t
;
404 typedef struct vlc_dictionary_t
407 vlc_dictionary_entry_t
** p_entries
;
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
;
418 p_dict
->p_entries
= (vlc_dictionary_entry_t
**)calloc( i_size
, sizeof(*p_dict
->p_entries
) );
419 if( !p_dict
->p_entries
)
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
),
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
];
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
);
445 free( p_dict
->p_entries
);
446 p_dict
->p_entries
= NULL
;
452 vlc_dictionary_has_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
454 if( !p_dict
->p_entries
)
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
) )
464 return p_entry
!= NULL
;
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
];
477 return kVLCDictionaryNotFound
;
479 /* Make sure we return the right item. (Hash collision) */
481 if( !strcmp( psz_key
, p_entry
->psz_key
) )
482 return p_entry
->p_value
;
483 p_entry
= p_entry
->p_next
;
486 return kVLCDictionaryNotFound
;
490 vlc_dictionary_keys_count( const vlc_dictionary_t
* p_dict
)
492 vlc_dictionary_entry_t
* p_entry
;
495 if( !p_dict
->p_entries
)
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
++;
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
] )
515 static inline char **
516 vlc_dictionary_all_keys( const vlc_dictionary_t
* p_dict
)
518 vlc_dictionary_entry_t
* p_entry
;
520 int i
, count
= vlc_dictionary_keys_count( p_dict
);
522 ppsz_ret
= (char**)malloc(sizeof(char *) * (count
+ 1));
523 if( unlikely(!ppsz_ret
) )
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
;
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
;
553 /* Count how many items there was */
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 */
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
];
569 vlc_dictionary_insert_impl_( &new_dict
, p_entry
->psz_key
,
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
;
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 );
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
),
594 if( !p_dict
->p_entries
)
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
;
602 return; /* Not found, nothing to do */
607 if( !strcmp( psz_key
, p_entry
->psz_key
) )
609 if( pf_free
!= NULL
)
610 ( * pf_free
)( p_entry
->p_value
, p_obj
);
612 p_dict
->p_entries
[i_pos
] = p_entry
->p_next
;
614 p_prev
->p_next
= p_entry
->p_next
;
615 free( p_entry
->psz_key
);
620 p_entry
= p_entry
->p_next
;
623 /* No key was found */
628 template <typename T
>
629 void vlc_delete_all( T
&container
)
631 typename
T::iterator it
= container
.begin();
632 while ( it
!= container
.end() )