Skip to content

Commit dcc4e13

Browse files
peffgitster
authored andcommitted
fast-export: factor out anonymized_entry creation
When anonymizing output, there's only one spot where we generate new entries to add to our hashmap: when anonymize_str() doesn't find an entry, we use the generate() callback to make one and add it. Let's pull that into its own function in preparation for another caller. Note that we'll add one extra feature. In anonymize_str(), we know that we won't find an existing entry in the hashmap (since it will only try to add after failing to find one). But other callers won't have the same behavior, so we should catch this case and free the now-dangling entry. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d6484e9 commit dcc4e13

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

builtin/fast-export.c

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,29 @@ static int anonymized_entry_cmp(const void *cmp_data UNUSED,
139139
return strcmp(a->orig, b->orig);
140140
}
141141

142+
static struct anonymized_entry *add_anonymized_entry(struct hashmap *map,
143+
unsigned hash,
144+
const char *orig, size_t len,
145+
char *anon)
146+
{
147+
struct anonymized_entry *ret, *old;
148+
149+
if (!map->cmpfn)
150+
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
151+
152+
FLEX_ALLOC_MEM(ret, orig, orig, len);
153+
hashmap_entry_init(&ret->hash, hash);
154+
ret->anon = anon;
155+
old = hashmap_put_entry(map, ret, hash);
156+
157+
if (old) {
158+
free(old->anon);
159+
free(old);
160+
}
161+
162+
return ret;
163+
}
164+
142165
/*
143166
* Basically keep a cache of X->Y so that we can repeatedly replace
144167
* the same anonymized string with another. The actual generation
@@ -164,15 +187,9 @@ static const char *anonymize_str(struct hashmap *map,
164187
ret = hashmap_get_entry(map, &key, hash, &key);
165188

166189
/* ...and finally generate a new mapping if necessary */
167-
if (!ret) {
168-
if (!map->cmpfn)
169-
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
170-
171-
FLEX_ALLOC_MEM(ret, orig, orig, len);
172-
hashmap_entry_init(&ret->hash, key.hash.hash);
173-
ret->anon = generate(data);
174-
hashmap_put(map, &ret->hash);
175-
}
190+
if (!ret)
191+
ret = add_anonymized_entry(map, key.hash.hash,
192+
orig, len, generate(data));
176193

177194
return ret->anon;
178195
}

0 commit comments

Comments
 (0)