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