diff options
author | Elijah Newren <newren@gmail.com> | 2020-11-06 03:24:53 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2020-11-06 20:33:35 +0300 |
commit | 6abd22065ccbec054db5af85296dd1167c670eda (patch) | |
tree | 84a09ebbf2e3c343956349d5fc241384f059fcca /strmap.c | |
parent | 4fa1d501f775253fe70bf6c6a00fe8156c8c61c7 (diff) |
strmap: split create_entry() out of strmap_put()
This will facilitate adding entries to a strmap subtype in ways that
differ slightly from that of strmap_put().
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'strmap.c')
-rw-r--r-- | strmap.c | 37 |
1 files changed, 23 insertions, 14 deletions
@@ -70,27 +70,36 @@ void strmap_partial_clear(struct strmap *map, int free_values) hashmap_partial_clear(&map->map); } +static struct strmap_entry *create_entry(struct strmap *map, + const char *str, + void *data) +{ + struct strmap_entry *entry; + const char *key = str; + + entry = xmalloc(sizeof(*entry)); + hashmap_entry_init(&entry->ent, strhash(str)); + + if (map->strdup_strings) + key = xstrdup(str); + entry->key = key; + entry->value = data; + return entry; +} + void *strmap_put(struct strmap *map, const char *str, void *data) { struct strmap_entry *entry = find_strmap_entry(map, str); - void *old = NULL; if (entry) { - old = entry->value; + void *old = entry->value; entry->value = data; - } else { - const char *key = str; - - entry = xmalloc(sizeof(*entry)); - hashmap_entry_init(&entry->ent, strhash(str)); - - if (map->strdup_strings) - key = xstrdup(str); - entry->key = key; - entry->value = data; - hashmap_add(&map->map, &entry->ent); + return old; } - return old; + + entry = create_entry(map, str, data); + hashmap_add(&map->map, &entry->ent); + return NULL; } struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str) |