]> git.sesse.net Git - vlc/blobdiff - include/vlc_arrays.h
Removes trailing spaces. Removes tabs.
[vlc] / include / vlc_arrays.h
index 0be9f960afb6b655622e9a5add4d568518452a0f..e6525c01f12f23bf28a7bacdae637dc148d6dc54 100644 (file)
@@ -318,9 +318,14 @@ static void * const kVLCDictionaryNotFound = NULL;
 
 static inline void vlc_dictionary_init( vlc_dictionary_t * p_dict, int i_size )
 {
-    p_dict->p_entries = 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 +353,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 +365,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++ )
     {
@@ -385,9 +394,9 @@ vlc_dictionary_all_keys( const vlc_dictionary_t * p_dict )
     char ** ppsz_ret;
     int i, count = vlc_dictionary_keys_count( p_dict );
 
-    ppsz_ret = malloc(sizeof(char *) * (count + 1));
+    ppsz_ret = (char**)malloc(sizeof(char *) * (count + 1));
     assert( ppsz_ret );
-    
     count = 0;
     for( i = 0; i < p_dict->i_size; i++ )
     {
@@ -405,12 +414,16 @@ 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];
 
     if( !p_entry )
     {
-        p_entry = p_dict->p_entries[i_pos] = malloc(sizeof(struct vlc_dictionary_entry_t));
+        p_entry = p_dict->p_entries[i_pos] = (struct vlc_dictionary_entry_t *)malloc(
+                sizeof(struct vlc_dictionary_entry_t));
         assert( p_entry );
 
         p_entry->psz_key = strdup( psz_key );
@@ -428,7 +441,7 @@ __vlc_dictionary_insert( vlc_dictionary_t * p_dict, const char * psz_key,
     }
 
     /* Hash collision here */
-    p_entry = malloc(sizeof(struct vlc_dictionary_entry_t));
+    p_entry = (struct vlc_dictionary_entry_t *)malloc(sizeof(struct vlc_dictionary_entry_t));
     assert( p_entry );
     p_entry->psz_key = strdup( psz_key );
     p_entry->p_value = p_value;
@@ -444,7 +457,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 );
@@ -457,7 +470,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;
@@ -475,6 +488,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;
@@ -487,6 +503,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 */