don't malloc duplicated cstring
This commit is contained in:
parent
33dfe2b5cc
commit
d8e00f5725
|
@ -470,9 +470,6 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
|
|||
break;
|
||||
}
|
||||
case PIC_TT_SYMBOL: {
|
||||
struct pic_symbol *sym = (struct pic_symbol *)obj;
|
||||
|
||||
gc_mark_object(pic, (struct pic_object *)sym->str);
|
||||
break;
|
||||
}
|
||||
case PIC_TT_REG: {
|
||||
|
@ -721,6 +718,8 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj)
|
|||
break;
|
||||
}
|
||||
case PIC_TT_SYMBOL: {
|
||||
pic_sym *sym = (pic_sym *)obj;
|
||||
pic_free(pic, (void *)sym->cstr);
|
||||
break;
|
||||
}
|
||||
case PIC_TT_REG: {
|
||||
|
@ -751,16 +750,13 @@ gc_sweep_symbols(pic_state *pic)
|
|||
khash_t(s) *h = &pic->syms;
|
||||
khiter_t it;
|
||||
pic_sym *sym;
|
||||
const char *cstr;
|
||||
|
||||
for (it = kh_begin(h); it != kh_end(h); ++it) {
|
||||
if (! kh_exist(h, it))
|
||||
continue;
|
||||
sym = kh_val(h, it);
|
||||
if (! gc_obj_is_marked((struct pic_object *)sym)) {
|
||||
cstr = kh_key(h, it);
|
||||
kh_del(s, h, it);
|
||||
pic_free(pic, (void *)cstr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@ extern "C" {
|
|||
|
||||
struct pic_symbol {
|
||||
PIC_OBJECT_HEADER
|
||||
pic_str *str;
|
||||
const char *cstr;
|
||||
};
|
||||
|
||||
#define pic_sym_p(v) (pic_type(v) == PIC_TT_SYMBOL)
|
||||
|
|
|
@ -411,17 +411,8 @@ void
|
|||
pic_close(pic_state *pic)
|
||||
{
|
||||
khash_t(s) *h = &pic->syms;
|
||||
khiter_t it;
|
||||
pic_allocf allocf = pic->allocf;
|
||||
|
||||
/* free all symbols */
|
||||
for (it = kh_begin(h); it != kh_end(h); ++it) {
|
||||
if (kh_exist(h, it)) {
|
||||
allocf((void *)kh_key(h, it), 0);
|
||||
}
|
||||
}
|
||||
kh_clear(s, h);
|
||||
|
||||
/* clear out root objects */
|
||||
pic->sp = pic->stbase;
|
||||
pic->ci = pic->cibase;
|
||||
|
|
|
@ -6,52 +6,43 @@
|
|||
|
||||
KHASH_DEFINE(s, const char *, pic_sym *, kh_str_hash_func, kh_str_hash_equal)
|
||||
|
||||
static pic_sym *
|
||||
pic_make_symbol(pic_state *pic, pic_str *str)
|
||||
{
|
||||
pic_sym *sym;
|
||||
|
||||
sym = (pic_sym *)pic_obj_alloc(pic, sizeof(struct pic_symbol), PIC_TT_SYMBOL);
|
||||
sym->str = str;
|
||||
return sym;
|
||||
}
|
||||
|
||||
pic_sym *
|
||||
pic_intern(pic_state *pic, pic_str *str)
|
||||
{
|
||||
return pic_intern_cstr(pic, pic_str_cstr(pic, str));
|
||||
}
|
||||
|
||||
pic_sym *
|
||||
pic_intern_cstr(pic_state *pic, const char *cstr)
|
||||
{
|
||||
khash_t(s) *h = &pic->syms;
|
||||
pic_sym *sym;
|
||||
char *cstr;
|
||||
khiter_t it;
|
||||
int ret;
|
||||
char *copy;
|
||||
|
||||
it = kh_put(s, h, pic_str_cstr(pic, str), &ret);
|
||||
it = kh_put(s, h, cstr, &ret);
|
||||
if (ret == 0) { /* if exists */
|
||||
sym = kh_val(h, it);
|
||||
pic_gc_protect(pic, pic_obj_value(sym));
|
||||
return sym;
|
||||
}
|
||||
|
||||
cstr = pic_malloc(pic, pic_str_len(str) + 1);
|
||||
strcpy(cstr, pic_str_cstr(pic, str));
|
||||
kh_key(h, it) = cstr;
|
||||
copy = pic_malloc(pic, strlen(cstr) + 1);
|
||||
strcpy(copy, cstr);
|
||||
kh_key(h, it) = copy;
|
||||
|
||||
sym = pic_make_symbol(pic, str);
|
||||
sym = (pic_sym *)pic_obj_alloc(pic, sizeof(pic_sym), PIC_TT_SYMBOL);
|
||||
sym->cstr = copy;
|
||||
kh_val(h, it) = sym;
|
||||
|
||||
return sym;
|
||||
}
|
||||
|
||||
pic_sym *
|
||||
pic_intern_cstr(pic_state *pic, const char *str)
|
||||
{
|
||||
return pic_intern(pic, pic_make_str(pic, str, strlen(str)));
|
||||
}
|
||||
|
||||
const char *
|
||||
pic_symbol_name(pic_state *pic, pic_sym *sym)
|
||||
pic_symbol_name(pic_state PIC_UNUSED(*pic), pic_sym *sym)
|
||||
{
|
||||
return pic_str_cstr(pic, sym->str);
|
||||
return sym->cstr;
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
@ -90,7 +81,7 @@ pic_symbol_symbol_to_string(pic_state *pic)
|
|||
|
||||
pic_get_args(pic, "m", &sym);
|
||||
|
||||
return pic_obj_value(sym->str);
|
||||
return pic_obj_value(pic_make_str_cstr(pic, sym->cstr));
|
||||
}
|
||||
|
||||
static pic_value
|
||||
|
|
Loading…
Reference in New Issue