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 *****************************************************************************/
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
;
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
);
54 #define TAB_INIT( count, tab ) \
60 #define TAB_CLEAN( count, tab ) \
67 #define TAB_APPEND_CAST( cast, count, tab, p ) \
70 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
72 (tab) = cast malloc( sizeof( *(tab) ) ); \
73 if( !(tab) ) abort(); \
78 #define TAB_APPEND( count, tab, p ) \
79 TAB_APPEND_CAST( , count, tab, p )
81 #define TAB_FIND( count, tab, p, idx ) \
83 for( (idx) = 0; (idx) < (count); (idx)++ ) \
84 if( (tab)[(idx)] == (p) ) \
86 if( (idx) >= (count) ) \
91 #define TAB_ERASE( count, tab, index ) \
94 memmove( (tab) + (index), \
95 (tab) + (index) + 1, \
96 ((count) - (index) - 1 ) * sizeof( *(tab) ) );\
105 #define TAB_REMOVE( count, tab, p ) \
108 TAB_FIND( count, tab, p, i_index ); \
110 TAB_ERASE( count, tab, i_index ); \
113 #define TAB_INSERT_CAST( cast, count, tab, p, index ) do { \
115 (tab) = cast realloc( tab, sizeof( *(tab) ) * ( (count) + 1 ) ); \
117 (tab) = cast malloc( sizeof( *(tab) ) ); \
118 if( !(tab) ) abort(); \
119 if( (count) - (index) > 0 ) \
120 memmove( (tab) + (index) + 1, \
122 ((count) - (index)) * sizeof( *(tab) ) );\
123 (tab)[(index)] = (p); \
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 ) \
141 int low = 0, high = count - 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 ) \
148 else if ( mid_val > key ) \
152 answer = mid; break; \
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]))
180 #define DECL_ARRAY(type) struct { \
186 #define TYPEDEF_ARRAY(type, name) typedef DECL_ARRAY(type) name;
188 #define ARRAY_INIT(array) \
190 (array).i_alloc = 0; \
191 (array).i_size = 0; \
192 (array).p_elems = NULL; \
195 #define ARRAY_RESET(array) \
197 (array).i_alloc = 0; \
198 (array).i_size = 0; \
199 free( (array).p_elems ); (array).p_elems = NULL; \
202 #define ARRAY_APPEND(array, elem) \
204 _ARRAY_GROW1(array); \
205 (array).p_elems[(array).i_size] = elem; \
209 #define ARRAY_INSERT(array,elem,pos) \
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; \
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) \
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) );\
237 _ARRAY_SHRINK(array); \
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
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
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
);
276 static inline size_t vlc_array_count( vlc_array_t
* p_array
)
278 return p_array
->i_count
;
282 # define vlc_array_item_at_index(ar, idx) \
284 const vlc_array_t *: ((ar)->pp_elems[idx]), \
285 vlc_array_t *: ((ar)->pp_elems[idx]))
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
,
295 return ar
->pp_elems
[idx
];
299 static inline ssize_t
vlc_array_index_of_item( const vlc_array_t
*ar
,
302 for( size_t i
= 0; i
< ar
->i_count
; i
++ )
304 if( ar
->pp_elems
[i
] == elem
)
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
) )
318 size_t tail
= ar
->i_count
- idx
;
320 memmove( pp
+ idx
+ 1, pp
+ idx
, sizeof( void * ) * tail
);
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
) )
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
) )
341 pp
[ar
->i_count
++] = elem
;
346 static inline void vlc_array_append_or_abort( vlc_array_t
*ar
, void *elem
)
348 if( vlc_array_append( ar
, elem
) != 0 )
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;
358 memmove( pp
+ idx
, pp
+ idx
+ 1, sizeof( void * ) * tail
);
362 if( ar
->i_count
> 0 )
364 pp
= (void **)realloc( pp
, sizeof( void * ) * ar
->i_count
);
365 if( likely(pp
!= NULL
) )
376 /************************************************************************
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
)
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
403 struct vlc_dictionary_entry_t
* p_next
;
404 } vlc_dictionary_entry_t
;
406 typedef struct vlc_dictionary_t
409 vlc_dictionary_entry_t
** p_entries
;
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
;
420 p_dict
->p_entries
= (vlc_dictionary_entry_t
**)calloc( i_size
, sizeof(*p_dict
->p_entries
) );
421 if( !p_dict
->p_entries
)
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
),
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
];
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
);
447 free( p_dict
->p_entries
);
448 p_dict
->p_entries
= NULL
;
454 vlc_dictionary_has_key( const vlc_dictionary_t
* p_dict
, const char * psz_key
)
456 if( !p_dict
->p_entries
)
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
) )
466 return p_entry
!= NULL
;
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
];
479 return kVLCDictionaryNotFound
;
481 /* Make sure we return the right item. (Hash collision) */
483 if( !strcmp( psz_key
, p_entry
->psz_key
) )
484 return p_entry
->p_value
;
485 p_entry
= p_entry
->p_next
;
488 return kVLCDictionaryNotFound
;
492 vlc_dictionary_keys_count( const vlc_dictionary_t
* p_dict
)
494 vlc_dictionary_entry_t
* p_entry
;
497 if( !p_dict
->p_entries
)
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
++;
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
] )
517 static inline char **
518 vlc_dictionary_all_keys( const vlc_dictionary_t
* p_dict
)
520 vlc_dictionary_entry_t
* p_entry
;
522 int i
, count
= vlc_dictionary_keys_count( p_dict
);
524 ppsz_ret
= (char**)malloc(sizeof(char *) * (count
+ 1));
525 if( unlikely(!ppsz_ret
) )
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
;
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
;
555 /* Count how many items there was */
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 */
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
];
571 vlc_dictionary_insert_impl_( &new_dict
, p_entry
->psz_key
,
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
;
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 );
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
),
596 if( !p_dict
->p_entries
)
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
;
604 return; /* Not found, nothing to do */
609 if( !strcmp( psz_key
, p_entry
->psz_key
) )
611 if( pf_free
!= NULL
)
612 ( * pf_free
)( p_entry
->p_value
, p_obj
);
614 p_dict
->p_entries
[i_pos
] = p_entry
->p_next
;
616 p_prev
->p_next
= p_entry
->p_next
;
617 free( p_entry
->psz_key
);
622 p_entry
= p_entry
->p_next
;
625 /* No key was found */
630 template <typename T
>
631 void vlc_delete_all( T
&container
)
633 typename
T::iterator it
= container
.begin();
634 while ( it
!= container
.end() )