config_file: refactor freeing of config entry lists The interface for freeing config list entries is more tangled than required. Instead of calling `cvar_free` for every list entry in `free_vars`, we now just provide a function `config_entry_list_free`. This function will iterate through all list entries and free the associated configuration entry as well as the list entry itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
diff --git a/src/config_file.c b/src/config_file.c
index 4294f0b..8ef94af 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -82,15 +82,20 @@ static int config_error_readonly(void)
return -1;
}
-static void cvar_free(config_entry_list *var)
+static void config_entry_list_free(config_entry_list *list)
{
- if (var == NULL)
- return;
+ config_entry_list *next;
+
+ while (list != NULL) {
+ next = list->next;
+
+ git__free((char*) list->entry->name);
+ git__free((char *) list->entry->value);
+ git__free(list->entry);
+ git__free(list);
- git__free((char*)var->entry->name);
- git__free((char *)var->entry->value);
- git__free(var->entry);
- git__free(var);
+ list = next;
+ };
}
int git_config_file_normalize_section(char *start, char *end)
@@ -144,32 +149,18 @@ static int append_entry(git_strmap *values, git_config_entry *entry)
return error;
}
-static void free_vars(git_strmap *values)
-{
- config_entry_list *var = NULL;
-
- if (values == NULL)
- return;
-
- git_strmap_foreach_value(values, var,
- while (var != NULL) {
- config_entry_list *next = var->next;
- cvar_free(var);
- var = next;
- });
-
- git_strmap_free(values);
-}
-
static void refcounted_strmap_free(refcounted_strmap *map)
{
+ config_entry_list *list = NULL;
+
if (!map)
return;
if (git_atomic_dec(&map->refcount) != 0)
return;
- free_vars(map->values);
+ git_strmap_foreach_value(map->values, list, config_entry_list_free(list));
+ git_strmap_free(map->values);
git__free(map);
}