X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=include%2Fvlc_arrays.h;h=7db0794855c009b19193b57c1e3d4749b5588bef;hb=c08fa1c24e6ff85f92529ea98838c8078e54061d;hp=02ac9923b2d80618f5cdf4bfa1ace0d7888d67f8;hpb=dcabd0549ce40c9d93b7d5bc54f3b0e3ad79ff2a;p=vlc diff --git a/include/vlc_arrays.h b/include/vlc_arrays.h index 02ac9923b2..7db0794855 100644 --- a/include/vlc_arrays.h +++ b/include/vlc_arrays.h @@ -117,6 +117,7 @@ } \ } while(0) + #define TAB_REMOVE( count, tab, p ) \ do { \ int _i_index_; \ @@ -278,6 +279,74 @@ #define FOREACH_END() } } + +/************************************************************************ + * Dynamic arrays with progressive allocation (Prefered API) + ************************************************************************/ +typedef struct vlc_array_t +{ + int i_count; + void ** pp_elems; +} vlc_array_t; + +static inline void vlc_array_init( vlc_array_t * p_array ) +{ + memset( p_array, 0, sizeof(vlc_array_t) ); +} + +static inline void vlc_array_clear( vlc_array_t * p_array ) +{ + free( p_array->pp_elems ); + memset( p_array, 0, sizeof(vlc_array_t) ); +} + +/* Read */ +static inline int +vlc_array_count( vlc_array_t * p_array ) +{ + return p_array->i_count; +} + +static inline void * +vlc_array_object_at_index( vlc_array_t * p_array, int i_index ) +{ + return p_array->pp_elems[i_index]; +} + +/* Write */ +static inline void +vlc_array_insert( vlc_array_t * p_array, void * p_elem, int i_index ) +{ + TAB_INSERT( p_array->i_count, p_array->pp_elems, p_elem, i_index ); +} + +static inline void +vlc_array_append( vlc_array_t * p_array, void * p_elem ) +{ + vlc_array_insert( p_array, p_elem, p_array->i_count ); +} + +static inline void +vlc_array_remove( vlc_array_t * p_array, int i_index ) +{ + if( i_index >= 0 ) + { + if( p_array->i_count > 1 ) + { + memmove( p_array->pp_elems + i_index, + p_array->pp_elems + i_index+1, + ( p_array->i_count - i_index - 1 ) * sizeof( void* ) ); + } + p_array->i_count--; + if( p_array->i_count == 0 ) + { + free( p_array->pp_elems ); + p_array->pp_elems = NULL; + } + } +} + + /************************************************************************ * Dictionaries ************************************************************************/ @@ -318,9 +387,14 @@ static void * const kVLCDictionaryNotFound = NULL; static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size ) { - p_dict->p_entries = (struct vlc_dictionary_entry_t **)malloc(sizeof(struct vlc_dictionary_entry_t *) * i_size); - assert( p_dict->p_entries ); - memset( p_dict->p_entries, 0, sizeof(struct vlc_dictionary_entry_t *) * i_size ); + if( i_size > 0 ) + { + p_dict->p_entries = (struct vlc_dictionary_entry_t **)malloc(sizeof(struct vlc_dictionary_entry_t *) * i_size); + assert( p_dict->p_entries ); + memset( p_dict->p_entries, 0, sizeof(struct vlc_dictionary_entry_t *) * i_size ); + } + else + p_dict->p_entries = NULL; p_dict->i_size = i_size; } @@ -348,6 +422,9 @@ static inline void vlc_dictionary_clear( vlc_dictionary_t * p_dict ) static inline void * vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key ) { + if( !p_dict->p_entries ) + return kVLCDictionaryNotFound; + int i_pos = DictHash( psz_key, p_dict->i_size ); struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos]; @@ -357,19 +434,20 @@ vlc_dictionary_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_ if( !p_entry ) return kVLCDictionaryNotFound; - /* Hash collision */ + /* Hash collision */ do { if( !strcmp( psz_key, p_entry->psz_key ) ) return p_entry->p_value; p_entry = p_entry->p_next; } while( p_entry ); + return kVLCDictionaryNotFound; } static inline int vlc_dictionary_keys_count( const vlc_dictionary_t * p_dict ) { - struct vlc_dictionary_entry_t * p_entry; + struct vlc_dictionary_entry_t * p_entry; int i, count = 0; for( i = 0; i < p_dict->i_size; i++ ) { @@ -387,7 +465,7 @@ vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict ) ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1)); assert( ppsz_ret ); - + count = 0; for( i = 0; i < p_dict->i_size; i++ ) { @@ -405,6 +483,9 @@ static inline void __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p_value, vlc_bool_t rebuild ) { + if( !p_dict->p_entries ) + vlc_dictionary_init( p_dict, 1 ); + int i_pos = DictHash( psz_key, p_dict->i_size ); struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos]; @@ -445,7 +526,7 @@ __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, { /* Here it starts to be not good, rebuild a bigger dictionary */ struct vlc_dictionary_t new_dict; - int i_new_size = ( p_dict->i_size * 3) / 2; /* XXX: this need tuning */ + int i_new_size = ( (p_dict->i_size+2) * 3) / 2; /* XXX: this need tuning */ int i; vlc_dictionary_init( &new_dict, i_new_size ); @@ -458,7 +539,7 @@ __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, p_entry->p_value, 0 /* To avoid multiple rebuild loop */); p_entry = p_entry->p_next; - } + } } vlc_dictionary_clear( p_dict ); p_dict->i_size = new_dict.i_size; @@ -476,6 +557,9 @@ vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key, void * p static inline void vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char * psz_key ) { + if( !p_dict->p_entries ) + return; + int i_pos = DictHash( psz_key, p_dict->i_size ); struct vlc_dictionary_entry_t * p_entry = p_dict->p_entries[i_pos]; struct vlc_dictionary_entry_t * p_prev; @@ -488,6 +572,7 @@ vlc_dictionary_remove_value_for_key( const vlc_dictionary_t * p_dict, const char free( p_entry->psz_key ); free( p_entry ); p_dict->p_entries[i_pos] = NULL; + return; } /* Hash collision */