ktrans: don't leak on conflicting jisho lines

Tidy up the hash interface to make cleanup
a bit more explicit vs non cleanup.
This commit is contained in:
Jacob Moody 2022-07-17 17:51:11 +00:00
parent 23620b2e70
commit 0ec0154bc9
3 changed files with 22 additions and 9 deletions

View file

@ -47,7 +47,7 @@ hmapalloc(int nbuckets, int size)
} }
int int
hmapset(Hmap **store, char *key, void *new, void *old) hmaprepl(Hmap **store, char *key, void *new, void *old, int freekeys)
{ {
Hnode *n; Hnode *n;
uchar *v; uchar *v;
@ -66,6 +66,8 @@ hmapset(Hmap **store, char *key, void *new, void *old)
if(n->filled == 0) if(n->filled == 0)
goto replace; goto replace;
if(strcmp(n->key, key) == 0){ if(strcmp(n->key, key) == 0){
if(freekeys)
free(n->key);
oldv = v + Tagsize; oldv = v + Tagsize;
goto replace; goto replace;
} }
@ -105,6 +107,18 @@ replace:
return 0; return 0;
} }
int
hmapupd(Hmap **h, char *key, void *new)
{
char *prev;
prev = hmapkey(*h, key);
if(prev == nil)
prev = key;
return hmaprepl(h, prev, new, nil, 0);
}
void* void*
_hmapget(Hmap *h, char *key) _hmapget(Hmap *h, char *key)
{ {
@ -184,7 +198,7 @@ hmaprehash(Hmap *old, int buckets)
for(i=0 ; i < old->len; i++){ for(i=0 ; i < old->len; i++){
v = old->nodes + i*old->nsz; v = old->nodes + i*old->nsz;
n = (Hnode*)v; n = (Hnode*)v;
hmapset(&new, n->key, v + Tagsize, nil); hmaprepl(&new, n->key, v + Tagsize, nil, 0);
} }
free(old); free(old);
return new; return new;

View file

@ -16,8 +16,8 @@ struct Hmap {
Hmap* hmapalloc(int nbuckets, int size); Hmap* hmapalloc(int nbuckets, int size);
int hmapget(Hmap *h, char *key, void *dst); int hmapget(Hmap *h, char *key, void *dst);
int hmapset(Hmap **h, char *key, void *new, void *old); int hmaprepl(Hmap **h, char *key, void *new, void *old, int freekeys);
int hmapupd(Hmap **h, char *key, void *new);
int hmapdel(Hmap *h, char *key, void *dst, int freekey); int hmapdel(Hmap *h, char *key, void *dst, int freekey);
void hmapfree(Hmap *h, int freekeys);
char* hmapkey(Hmap *h, char *key); char* hmapkey(Hmap *h, char *key);
void hmapreset(Hmap *h, int freekeys); void hmapreset(Hmap *h, int freekeys);

View file

@ -122,14 +122,13 @@ initmap(Map *m, int n)
//confict; partial & valid input //confict; partial & valid input
prev = m[i]; prev = m[i];
prev.leadstomore = 1; prev.leadstomore = 1;
free(hmapkey(h, buf));
} }
} }
if(s[1] == '\0'){ if(s[1] == '\0'){
hmapset(&h, strdup(buf), &prev, nil); hmaprepl(&h, strdup(buf), &prev, nil, 1);
} else { } else {
hmapset(&h, strdup(buf), &signalmore, nil); hmaprepl(&h, strdup(buf), &signalmore, nil, 1);
} }
j++; j++;
} }
@ -425,7 +424,7 @@ opendict(Hmap *h, char *name)
kouho[i] = rest; kouho[i] = rest;
/* key is the base pointer; overwrites clean up for us */ /* key is the base pointer; overwrites clean up for us */
hmapset(&h, p, kouho, nil); hmaprepl(&h, p, kouho, nil, 1);
} }
Bterm(b); Bterm(b);
return h; return h;
@ -537,7 +536,7 @@ dotrans(Hmap *dic)
p = kouho[0]; p = kouho[0];
kouho[0] = kouho[i]; kouho[0] = kouho[i];
kouho[i] = p; kouho[i] = p;
hmapset(&dic, hmapkey(dic, v), kouho, nil); hmapupd(&dic, v, kouho);
} }
olen = okuri = joshi = 0; olen = okuri = joshi = 0;