Skip to content

Commit a208ec1

Browse files
newrengitster
authored andcommitted
strmap: enable allocations to come from a mem_pool
For heavy users of strmaps, allowing the keys and entries to be allocated from a memory pool can provide significant overhead savings. Add an option to strmap_init_with_options() to specify a memory pool. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1201eb6 commit a208ec1

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

strmap.c

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "git-compat-util.h"
22
#include "strmap.h"
3+
#include "mem-pool.h"
34

45
int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
56
const struct hashmap_entry *entry1,
@@ -24,13 +25,15 @@ static struct strmap_entry *find_strmap_entry(struct strmap *map,
2425

2526
void strmap_init(struct strmap *map)
2627
{
27-
strmap_init_with_options(map, 1);
28+
strmap_init_with_options(map, NULL, 1);
2829
}
2930

3031
void strmap_init_with_options(struct strmap *map,
32+
struct mem_pool *pool,
3133
int strdup_strings)
3234
{
3335
hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);
36+
map->pool = pool;
3437
map->strdup_strings = strdup_strings;
3538
}
3639

@@ -42,6 +45,10 @@ static void strmap_free_entries_(struct strmap *map, int free_values)
4245
if (!map)
4346
return;
4447

48+
if (!free_values && map->pool)
49+
/* Memory other than util is owned by and freed with the pool */
50+
return;
51+
4552
/*
4653
* We need to iterate over the hashmap entries and free
4754
* e->key and e->value ourselves; hashmap has no API to
@@ -52,9 +59,11 @@ static void strmap_free_entries_(struct strmap *map, int free_values)
5259
hashmap_for_each_entry(&map->map, &iter, e, ent) {
5360
if (free_values)
5461
free(e->value);
55-
if (map->strdup_strings)
56-
free((char*)e->key);
57-
free(e);
62+
if (!map->pool) {
63+
if (map->strdup_strings)
64+
free((char*)e->key);
65+
free(e);
66+
}
5867
}
5968
}
6069

@@ -77,11 +86,13 @@ static struct strmap_entry *create_entry(struct strmap *map,
7786
struct strmap_entry *entry;
7887
const char *key = str;
7988

80-
entry = xmalloc(sizeof(*entry));
89+
entry = map->pool ? mem_pool_alloc(map->pool, sizeof(*entry))
90+
: xmalloc(sizeof(*entry));
8191
hashmap_entry_init(&entry->ent, strhash(str));
8292

8393
if (map->strdup_strings)
84-
key = xstrdup(str);
94+
key = map->pool ? mem_pool_strdup(map->pool, str)
95+
: xstrdup(str);
8596
entry->key = key;
8697
entry->value = data;
8798
return entry;
@@ -128,9 +139,11 @@ void strmap_remove(struct strmap *map, const char *str, int free_value)
128139
return;
129140
if (free_value)
130141
free(ret->value);
131-
if (map->strdup_strings)
132-
free((char*)ret->key);
133-
free(ret);
142+
if (!map->pool) {
143+
if (map->strdup_strings)
144+
free((char*)ret->key);
145+
free(ret);
146+
}
134147
}
135148

136149
void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)

strmap.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#include "hashmap.h"
55

6+
struct mem_pool;
67
struct strmap {
78
struct hashmap map;
9+
struct mem_pool *pool;
810
unsigned int strdup_strings:1;
911
};
1012

@@ -37,9 +39,10 @@ void strmap_init(struct strmap *map);
3739

3840
/*
3941
* Same as strmap_init, but for those who want to control the memory management
40-
* carefully instead of using the default of strdup_strings=1.
42+
* carefully instead of using the default of strdup_strings=1 and pool=NULL.
4143
*/
4244
void strmap_init_with_options(struct strmap *map,
45+
struct mem_pool *pool,
4346
int strdup_strings);
4447

4548
/*
@@ -137,9 +140,10 @@ static inline void strintmap_init(struct strintmap *map, int default_value)
137140

138141
static inline void strintmap_init_with_options(struct strintmap *map,
139142
int default_value,
143+
struct mem_pool *pool,
140144
int strdup_strings)
141145
{
142-
strmap_init_with_options(&map->map, strdup_strings);
146+
strmap_init_with_options(&map->map, pool, strdup_strings);
143147
map->default_value = default_value;
144148
}
145149

@@ -221,9 +225,10 @@ static inline void strset_init(struct strset *set)
221225
}
222226

223227
static inline void strset_init_with_options(struct strset *set,
228+
struct mem_pool *pool,
224229
int strdup_strings)
225230
{
226-
strmap_init_with_options(&set->map, strdup_strings);
231+
strmap_init_with_options(&set->map, pool, strdup_strings);
227232
}
228233

229234
static inline void strset_clear(struct strset *set)

0 commit comments

Comments
 (0)