From 5cbb44d6b823852b24db5f51e2c600330140a0bc Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Thu, 25 Jun 2015 07:26:48 +0900 Subject: [PATCH] use khash for env --- extlib/benz/codegen.c | 7 ++++--- extlib/benz/gc.c | 13 ++++++++----- extlib/benz/include/picrin/macro.h | 4 +++- extlib/benz/macro.c | 17 ++++++++++++----- 4 files changed, 27 insertions(+), 14 deletions(-) diff --git a/extlib/benz/codegen.c b/extlib/benz/codegen.c index daa2a234..3a0eb8c6 100644 --- a/extlib/benz/codegen.c +++ b/extlib/benz/codegen.c @@ -11,13 +11,14 @@ static pic_sym * lookup(pic_state PIC_UNUSED(*pic), pic_value var, struct pic_env *env) { - xh_entry *e; + khiter_t it; assert(pic_var_p(var)); while (env != NULL) { - if ((e = xh_get_ptr(&env->map, pic_ptr(var))) != NULL) { - return xh_val(e, pic_sym *); + it = kh_get(env, &env->map, pic_ptr(var)); + if (it != kh_end(&env->map)) { + return kh_val(&env->map, it); } env = env->up; } diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index ff56d26d..fd0b048e 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -405,14 +405,17 @@ gc_mark_object(pic_state *pic, struct pic_object *obj) } case PIC_TT_ENV: { struct pic_env *env = (struct pic_env *)obj; - xh_entry *it; + khash_t(env) *h = &env->map; + khiter_t it; if (env->up) { gc_mark_object(pic, (struct pic_object *)env->up); } - for (it = xh_begin(&env->map); it != NULL; it = xh_next(it)) { - gc_mark_object(pic, xh_key(it, struct pic_object *)); - gc_mark_object(pic, xh_val(it, struct pic_object *)); + for (it = kh_begin(h); it != kh_end(h); ++it) { + if (kh_exist(h, it)) { + gc_mark_object(pic, kh_key(h, it)); + gc_mark_object(pic, (struct pic_object *)kh_val(h, it)); + } } break; } @@ -688,7 +691,7 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) } case PIC_TT_ENV: { struct pic_env *env = (struct pic_env *)obj; - xh_destroy(&env->map); + kh_destroy(env, &env->map); break; } case PIC_TT_LIB: { diff --git a/extlib/benz/include/picrin/macro.h b/extlib/benz/include/picrin/macro.h index f6baebbb..65b8e3bd 100644 --- a/extlib/benz/include/picrin/macro.h +++ b/extlib/benz/include/picrin/macro.h @@ -9,6 +9,8 @@ extern "C" { #endif +KHASH_DECLARE(env, void *, pic_sym *) + struct pic_id { PIC_OBJECT_HEADER pic_value var; @@ -17,7 +19,7 @@ struct pic_id { struct pic_env { PIC_OBJECT_HEADER - xhash map; + khash_t(env) map; struct pic_env *up; }; diff --git a/extlib/benz/macro.c b/extlib/benz/macro.c index 71b70a55..cac07fd5 100644 --- a/extlib/benz/macro.c +++ b/extlib/benz/macro.c @@ -4,6 +4,8 @@ #include "picrin.h" +KHASH_DEFINE(env, void *, pic_sym *, kh_ptr_hash_func, kh_ptr_hash_equal) + bool pic_var_p(pic_value obj) { @@ -30,7 +32,7 @@ pic_make_env(pic_state *pic, struct pic_env *up) env = (struct pic_env *)pic_obj_alloc(pic, sizeof(struct pic_env), PIC_TT_ENV); env->up = up; - xh_init_ptr(&env->map, sizeof(pic_sym *)); + kh_init(env, &env->map); return env; } @@ -74,22 +76,27 @@ pic_add_variable(pic_state *pic, struct pic_env *env, pic_value var) void pic_put_variable(pic_state PIC_UNUSED(*pic), struct pic_env *env, pic_value var, pic_sym *uid) { + khiter_t it; + int ret; + assert(pic_var_p(var)); - xh_put_ptr(&env->map, pic_ptr(var), &uid); + it = kh_put(env, &env->map, pic_ptr(var), &ret); + kh_val(&env->map, it) = uid; } pic_sym * pic_find_variable(pic_state PIC_UNUSED(*pic), struct pic_env *env, pic_value var) { - xh_entry *e; + khiter_t it; assert(pic_var_p(var)); - if ((e = xh_get_ptr(&env->map, pic_ptr(var))) == NULL) { + it = kh_get(env, &env->map, pic_ptr(var)); + if (it == kh_end(&env->map)) { return NULL; } - return xh_val(e, pic_sym *); + return kh_val(&env->map, it); } static pic_value