Commit 123e5963 by Patrick Steinhardt

config_entries: abstract away reference counting

Instead of directly calling `git_atomic_inc` in users of the config
entries store, provide a `git_config_entries_incref` function to further
decouple the interfaces. Convert the refcount to a `git_refcount`
structure while at it.
parent 5a7e0b3c
...@@ -44,8 +44,7 @@ int git_config_entries_new(git_config_entries **out) ...@@ -44,8 +44,7 @@ int git_config_entries_new(git_config_entries **out)
entries = git__calloc(1, sizeof(git_config_entries)); entries = git__calloc(1, sizeof(git_config_entries));
GITERR_CHECK_ALLOC(entries); GITERR_CHECK_ALLOC(entries);
GIT_REFCOUNT_INC(entries);
git_atomic_set(&entries->refcount, 1);
if ((error = git_strmap_alloc(&entries->map)) < 0) if ((error = git_strmap_alloc(&entries->map)) < 0)
git__free(entries); git__free(entries);
...@@ -55,15 +54,14 @@ int git_config_entries_new(git_config_entries **out) ...@@ -55,15 +54,14 @@ int git_config_entries_new(git_config_entries **out)
return error; return error;
} }
void git_config_entries_free(git_config_entries *entries) void git_config_entries_incref(git_config_entries *entries)
{ {
config_entry_list *list = NULL, *next; GIT_REFCOUNT_INC(entries);
}
if (!entries)
return;
if (git_atomic_dec(&entries->refcount) != 0) static void config_entries_free(git_config_entries *entries)
return; {
config_entry_list *list = NULL, *next;
git_strmap_foreach_value(entries->map, list, config_entry_list_free(list)); git_strmap_foreach_value(entries->map, list, config_entry_list_free(list));
git_strmap_free(entries->map); git_strmap_free(entries->map);
...@@ -78,6 +76,12 @@ void git_config_entries_free(git_config_entries *entries) ...@@ -78,6 +76,12 @@ void git_config_entries_free(git_config_entries *entries)
git__free(entries); git__free(entries);
} }
void git_config_entries_free(git_config_entries *entries)
{
if (entries)
GIT_REFCOUNT_DEC(entries, config_entries_free);
}
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry) int git_config_entries_append(git_config_entries *entries, git_config_entry *entry)
{ {
git_strmap_iter pos; git_strmap_iter pos;
......
...@@ -17,12 +17,13 @@ typedef struct config_entry_list { ...@@ -17,12 +17,13 @@ typedef struct config_entry_list {
} config_entry_list; } config_entry_list;
typedef struct { typedef struct {
git_atomic refcount; git_refcount rc;
git_strmap *map; git_strmap *map;
config_entry_list *list; config_entry_list *list;
} git_config_entries; } git_config_entries;
int git_config_entries_new(git_config_entries **out); int git_config_entries_new(git_config_entries **out);
void git_config_entries_incref(git_config_entries *entries);
void git_config_entries_free(git_config_entries *entries); void git_config_entries_free(git_config_entries *entries);
/* Add or append the new config option */ /* Add or append the new config option */
int git_config_entries_append(git_config_entries *entries, git_config_entry *entry); int git_config_entries_append(git_config_entries *entries, git_config_entry *entry);
......
...@@ -88,7 +88,7 @@ static git_config_entries *diskfile_entries_take(diskfile_header *h) ...@@ -88,7 +88,7 @@ static git_config_entries *diskfile_entries_take(diskfile_header *h)
} }
entries = h->entries; entries = h->entries;
git_atomic_inc(&entries->refcount); git_config_entries_incref(entries);
git_mutex_unlock(&h->values_mutex); git_mutex_unlock(&h->values_mutex);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment