diff --git a/sys/src/cmd/ktrans/hash.c b/sys/src/cmd/ktrans/hash.c index dcfa67cdd..b8b8c81f7 100644 --- a/sys/src/cmd/ktrans/hash.c +++ b/sys/src/cmd/ktrans/hash.c @@ -47,7 +47,7 @@ hmapalloc(int nbuckets, int size) } int -hmapset(Hmap **store, char *key, void *new, void *old) +hmaprepl(Hmap **store, char *key, void *new, void *old, int freekeys) { Hnode *n; uchar *v; @@ -66,6 +66,8 @@ hmapset(Hmap **store, char *key, void *new, void *old) if(n->filled == 0) goto replace; if(strcmp(n->key, key) == 0){ + if(freekeys) + free(n->key); oldv = v + Tagsize; goto replace; } @@ -105,6 +107,18 @@ replace: 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* _hmapget(Hmap *h, char *key) { @@ -184,7 +198,7 @@ hmaprehash(Hmap *old, int buckets) for(i=0 ; i < old->len; i++){ v = old->nodes + i*old->nsz; n = (Hnode*)v; - hmapset(&new, n->key, v + Tagsize, nil); + hmaprepl(&new, n->key, v + Tagsize, nil, 0); } free(old); return new; diff --git a/sys/src/cmd/ktrans/hash.h b/sys/src/cmd/ktrans/hash.h index 66e9e78db..bf9daa8c6 100644 --- a/sys/src/cmd/ktrans/hash.h +++ b/sys/src/cmd/ktrans/hash.h @@ -16,8 +16,8 @@ struct Hmap { Hmap* hmapalloc(int nbuckets, int size); 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); -void hmapfree(Hmap *h, int freekeys); char* hmapkey(Hmap *h, char *key); void hmapreset(Hmap *h, int freekeys); diff --git a/sys/src/cmd/ktrans/main.c b/sys/src/cmd/ktrans/main.c index 3757096ee..fb0cfe230 100644 --- a/sys/src/cmd/ktrans/main.c +++ b/sys/src/cmd/ktrans/main.c @@ -122,14 +122,13 @@ initmap(Map *m, int n) //confict; partial & valid input prev = m[i]; prev.leadstomore = 1; - free(hmapkey(h, buf)); } } if(s[1] == '\0'){ - hmapset(&h, strdup(buf), &prev, nil); + hmaprepl(&h, strdup(buf), &prev, nil, 1); } else { - hmapset(&h, strdup(buf), &signalmore, nil); + hmaprepl(&h, strdup(buf), &signalmore, nil, 1); } j++; } @@ -425,7 +424,7 @@ opendict(Hmap *h, char *name) kouho[i] = rest; /* key is the base pointer; overwrites clean up for us */ - hmapset(&h, p, kouho, nil); + hmaprepl(&h, p, kouho, nil, 1); } Bterm(b); return h; @@ -537,7 +536,7 @@ dotrans(Hmap *dic) p = kouho[0]; kouho[0] = kouho[i]; kouho[i] = p; - hmapset(&dic, hmapkey(dic, v), kouho, nil); + hmapupd(&dic, v, kouho); } olen = okuri = joshi = 0;