fix core text and map ownership
This commit is contained in:
55
hash.c
55
hash.c
@@ -18,13 +18,15 @@ hash(Str *s)
|
||||
}
|
||||
|
||||
Hmap*
|
||||
hmapalloc(int nbuckets, int size)
|
||||
hmapalloc(int nbuckets)
|
||||
{
|
||||
void *store;
|
||||
Hmap *h;
|
||||
int nsz;
|
||||
|
||||
nsz = Tagsize + size;
|
||||
if(nbuckets < 1)
|
||||
die("hmapalloc: no buckets");
|
||||
nsz = Tagsize;
|
||||
store = emalloc(sizeof(*h) + nbuckets * nsz);
|
||||
h = store;
|
||||
h->nbs = nbuckets;
|
||||
@@ -37,7 +39,7 @@ hmapalloc(int nbuckets, int size)
|
||||
static int
|
||||
keycmp(Hnode *n, Str *key)
|
||||
{
|
||||
char buf[256];
|
||||
char buf[Maxutf];
|
||||
int len;
|
||||
|
||||
len = stoutf(key, buf, sizeof(buf));
|
||||
@@ -67,7 +69,7 @@ hmapget(Hmap *h, Str *key)
|
||||
static char*
|
||||
sdup(Str *s)
|
||||
{
|
||||
char buf[256];
|
||||
char buf[Maxutf];
|
||||
char *p;
|
||||
int n;
|
||||
|
||||
@@ -78,15 +80,48 @@ sdup(Str *s)
|
||||
return p;
|
||||
}
|
||||
|
||||
int
|
||||
hmapset(Hmap **store, Str *key, Str *val)
|
||||
static char*
|
||||
memdup(const char *src, int n)
|
||||
{
|
||||
char *p;
|
||||
|
||||
if(n == 0)
|
||||
return nil;
|
||||
p = emalloc(n + 1);
|
||||
memmove(p, src, n);
|
||||
p[n] = '\0';
|
||||
return p;
|
||||
}
|
||||
|
||||
void
|
||||
hmapfree(Hmap *h)
|
||||
{
|
||||
Hnode *n;
|
||||
int i;
|
||||
|
||||
if(h == nil)
|
||||
return;
|
||||
for(i = 0; i < h->len; i++){
|
||||
n = (Hnode*)(h->nodes + i * h->nsz);
|
||||
if(!n->filled)
|
||||
continue;
|
||||
free(n->key);
|
||||
free(n->val);
|
||||
}
|
||||
free(h);
|
||||
}
|
||||
|
||||
void
|
||||
hmapset(Hmap **store, Str *key, const char *val, int vlen)
|
||||
{
|
||||
char *newval;
|
||||
Hnode *n;
|
||||
uchar *v;
|
||||
Hmap *h;
|
||||
int next;
|
||||
vlong diff;
|
||||
|
||||
newval = memdup(val, vlen);
|
||||
h = *store;
|
||||
v = h->nodes + (hash(key) % h->nbs) * h->nsz;
|
||||
for(;;){
|
||||
@@ -121,9 +156,7 @@ replace:
|
||||
n->filled = 1;
|
||||
}
|
||||
n->next = next;
|
||||
if(val->n > 0){
|
||||
n->val = sdup(val);
|
||||
n->vlen = strlen(n->val);
|
||||
}
|
||||
return 0;
|
||||
free(n->val);
|
||||
n->val = newval;
|
||||
n->vlen = vlen;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user