Coverage Report

Created: 2024-09-08 06:23

/src/git/strmap.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "strmap.h"
3
#include "mem-pool.h"
4
5
int cmp_strmap_entry(const void *hashmap_cmp_fn_data UNUSED,
6
         const struct hashmap_entry *entry1,
7
         const struct hashmap_entry *entry2,
8
         const void *keydata UNUSED)
9
0
{
10
0
  const struct strmap_entry *e1, *e2;
11
12
0
  e1 = container_of(entry1, const struct strmap_entry, ent);
13
0
  e2 = container_of(entry2, const struct strmap_entry, ent);
14
0
  return strcmp(e1->key, e2->key);
15
0
}
16
17
static struct strmap_entry *find_strmap_entry(struct strmap *map,
18
                const char *str)
19
0
{
20
0
  struct strmap_entry entry;
21
0
  hashmap_entry_init(&entry.ent, strhash(str));
22
0
  entry.key = str;
23
0
  return hashmap_get_entry(&map->map, &entry, ent, NULL);
24
0
}
25
26
void strmap_init(struct strmap *map)
27
0
{
28
0
  struct strmap blank = STRMAP_INIT;
29
0
  memcpy(map, &blank, sizeof(*map));
30
0
}
31
32
void strmap_init_with_options(struct strmap *map,
33
            struct mem_pool *pool,
34
            int strdup_strings)
35
0
{
36
0
  hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);
37
0
  map->pool = pool;
38
0
  map->strdup_strings = strdup_strings;
39
0
}
40
41
static void strmap_free_entries_(struct strmap *map, int free_values)
42
970
{
43
970
  struct hashmap_iter iter;
44
970
  struct strmap_entry *e;
45
46
970
  if (!map)
47
0
    return;
48
49
970
  if (!free_values && map->pool)
50
    /* Memory other than util is owned by and freed with the pool */
51
0
    return;
52
53
  /*
54
   * We need to iterate over the hashmap entries and free
55
   * e->key and e->value ourselves; hashmap has no API to
56
   * take care of that for us.  Since we're already iterating over
57
   * the hashmap, though, might as well free e too and avoid the need
58
   * to make some call into the hashmap API to do that.
59
   */
60
970
  hashmap_for_each_entry(&map->map, &iter, e, ent) {
61
0
    if (free_values)
62
0
      free(e->value);
63
0
    if (!map->pool)
64
0
      free(e);
65
0
  }
66
970
}
67
68
void strmap_clear(struct strmap *map, int free_values)
69
970
{
70
970
  strmap_free_entries_(map, free_values);
71
970
  hashmap_clear(&map->map);
72
970
}
73
74
void strmap_partial_clear(struct strmap *map, int free_values)
75
0
{
76
0
  strmap_free_entries_(map, free_values);
77
0
  hashmap_partial_clear(&map->map);
78
0
}
79
80
static struct strmap_entry *create_entry(struct strmap *map,
81
           const char *str,
82
           void *data)
83
0
{
84
0
  struct strmap_entry *entry;
85
86
0
  if (map->strdup_strings) {
87
0
    if (!map->pool) {
88
0
      FLEXPTR_ALLOC_STR(entry, key, str);
89
0
    } else {
90
0
      size_t len = st_add(strlen(str), 1); /* include NUL */
91
0
      entry = mem_pool_alloc(map->pool,
92
0
                 st_add(sizeof(*entry), len));
93
0
      memcpy(entry + 1, str, len);
94
0
      entry->key = (void *)(entry + 1);
95
0
    }
96
0
  } else if (!map->pool) {
97
0
    entry = xmalloc(sizeof(*entry));
98
0
  } else {
99
0
    entry = mem_pool_alloc(map->pool, sizeof(*entry));
100
0
  }
101
0
  hashmap_entry_init(&entry->ent, strhash(str));
102
0
  if (!map->strdup_strings)
103
0
    entry->key = str;
104
0
  entry->value = data;
105
0
  return entry;
106
0
}
107
108
void *strmap_put(struct strmap *map, const char *str, void *data)
109
0
{
110
0
  struct strmap_entry *entry = find_strmap_entry(map, str);
111
112
0
  if (entry) {
113
0
    void *old = entry->value;
114
0
    entry->value = data;
115
0
    return old;
116
0
  }
117
118
0
  entry = create_entry(map, str, data);
119
0
  hashmap_add(&map->map, &entry->ent);
120
0
  return NULL;
121
0
}
122
123
struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)
124
0
{
125
0
  return find_strmap_entry(map, str);
126
0
}
127
128
void *strmap_get(struct strmap *map, const char *str)
129
0
{
130
0
  struct strmap_entry *entry = find_strmap_entry(map, str);
131
0
  return entry ? entry->value : NULL;
132
0
}
133
134
int strmap_contains(struct strmap *map, const char *str)
135
0
{
136
0
  return find_strmap_entry(map, str) != NULL;
137
0
}
138
139
void strmap_remove(struct strmap *map, const char *str, int free_value)
140
0
{
141
0
  struct strmap_entry entry, *ret;
142
0
  hashmap_entry_init(&entry.ent, strhash(str));
143
0
  entry.key = str;
144
0
  ret = hashmap_remove_entry(&map->map, &entry, ent, NULL);
145
0
  if (!ret)
146
0
    return;
147
0
  if (free_value)
148
0
    free(ret->value);
149
0
  if (!map->pool)
150
0
    free(ret);
151
0
}
152
153
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
154
0
{
155
0
  struct strmap_entry *entry = find_strmap_entry(&map->map, str);
156
0
  if (entry) {
157
0
    intptr_t *whence = (intptr_t*)&entry->value;
158
0
    *whence += amt;
159
0
  }
160
0
  else
161
0
    strintmap_set(map, str, map->default_value + amt);
162
0
}
163
164
int strset_add(struct strset *set, const char *str)
165
0
{
166
  /*
167
   * Cannot use strmap_put() because it'll return NULL in both cases:
168
   *   - cannot find str: NULL means "not found"
169
   *   - does find str: NULL is the value associated with str
170
   */
171
0
  struct strmap_entry *entry = find_strmap_entry(&set->map, str);
172
173
0
  if (entry)
174
0
    return 0;
175
176
0
  entry = create_entry(&set->map, str, NULL);
177
0
  hashmap_add(&set->map.map, &entry->ent);
178
0
  return 1;
179
0
}