Commit 370ad6fa authored by crimsonwoods's avatar crimsonwoods

Remove 'mrb_state' field from 'kh_xxx_t' structure.

'kh_xxx_t' requires 'mrb_state' to allocate, free, and compute hash value.
But 'mrb_state' should not be held by 'kh_xxx_t' and 'mrb_state' should be
supplied from outside.
parent 83c45e73
......@@ -188,7 +188,7 @@ struct RClass * mrb_class_get_under(mrb_state *mrb, struct RClass *outer, const
mrb_value mrb_obj_dup(mrb_state *mrb, mrb_value obj);
mrb_value mrb_check_to_integer(mrb_state *mrb, mrb_value val, const char *method);
mrb_bool mrb_obj_respond_to(struct RClass* c, mrb_sym mid);
mrb_bool mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid);
struct RClass * mrb_define_class_under(mrb_state *mrb, struct RClass *outer, const char *name, struct RClass *super);
struct RClass * mrb_define_module_under(mrb_state *mrb, struct RClass *outer, const char *name);
......
......@@ -63,17 +63,16 @@ static const uint8_t __m_either[8] = {0x03, 0x0c, 0x30, 0xc0};
khval_t *vals; \
khint_t mask; \
khint_t inc; \
mrb_state *mrb; \
} kh_##name##_t; \
void kh_alloc_##name(kh_##name##_t *h); \
void kh_alloc_##name(mrb_state *mrb, kh_##name##_t *h); \
kh_##name##_t *kh_init_##name##_size(mrb_state *mrb, khint_t size); \
kh_##name##_t *kh_init_##name(mrb_state *mrb); \
void kh_destroy_##name(kh_##name##_t *h); \
void kh_clear_##name(kh_##name##_t *h); \
khint_t kh_get_##name(kh_##name##_t *h, khkey_t key); \
khint_t kh_put_##name(kh_##name##_t *h, khkey_t key); \
void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets); \
void kh_del_##name(kh_##name##_t *h, khint_t x); \
void kh_destroy_##name(mrb_state *mrb, kh_##name##_t *h); \
void kh_clear_##name(mrb_state *mrb, kh_##name##_t *h); \
khint_t kh_get_##name(mrb_state *mrb, kh_##name##_t *h, khkey_t key); \
khint_t kh_put_##name(mrb_state *mrb, kh_##name##_t *h, khkey_t key); \
void kh_resize_##name(mrb_state *mrb, kh_##name##_t *h, khint_t new_n_buckets); \
void kh_del_##name(mrb_state *mrb, kh_##name##_t *h, khint_t x); \
kh_##name##_t *kh_copy_##name(mrb_state *mrb, kh_##name##_t *h);
static inline void
......@@ -94,11 +93,11 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
__hash_equal: hash comparation function
*/
#define KHASH_DEFINE(name, khkey_t, khval_t, kh_is_map, __hash_func, __hash_equal) \
void kh_alloc_##name(kh_##name##_t *h) \
void kh_alloc_##name(mrb_state *mrb, kh_##name##_t *h) \
{ \
khint_t sz = h->n_buckets; \
int len = sizeof(khkey_t) + (kh_is_map ? sizeof(khval_t) : 0); \
uint8_t *p = mrb_malloc(h->mrb, sizeof(uint8_t)*sz/4+len*sz); \
uint8_t *p = mrb_malloc(mrb, sizeof(uint8_t)*sz/4+len*sz); \
h->size = h->n_occupied = 0; \
h->upper_bound = UPPER_BOUND(sz); \
h->keys = (khkey_t *)p; \
......@@ -114,39 +113,38 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
size = KHASH_MIN_SIZE; \
khash_power2(size); \
h->n_buckets = size; \
h->mrb = mrb; \
kh_alloc_##name(h); \
kh_alloc_##name(mrb, h); \
return h; \
} \
kh_##name##_t *kh_init_##name(mrb_state *mrb){ \
return kh_init_##name##_size(mrb, KHASH_DEFAULT_SIZE); \
} \
void kh_destroy_##name(kh_##name##_t *h) \
void kh_destroy_##name(mrb_state *mrb, kh_##name##_t *h) \
{ \
if (h) { \
mrb_free(h->mrb, h->keys); \
mrb_free(h->mrb, h); \
mrb_free(mrb, h->keys); \
mrb_free(mrb, h); \
} \
} \
void kh_clear_##name(kh_##name##_t *h) \
void kh_clear_##name(mrb_state *mrb, kh_##name##_t *h) \
{ \
if (h && h->ed_flags) { \
kh_fill_flags(h->ed_flags, 0xaa, h->n_buckets/4); \
h->size = h->n_occupied = 0; \
} \
} \
khint_t kh_get_##name(kh_##name##_t *h, khkey_t key) \
khint_t kh_get_##name(mrb_state *mrb, kh_##name##_t *h, khkey_t key) \
{ \
khint_t k = __hash_func(h->mrb,key) & (h->mask); \
khint_t k = __hash_func(mrb,key) & (h->mask); \
while (!__ac_isempty(h->ed_flags, k)) { \
if (!__ac_isdel(h->ed_flags, k)) { \
if (__hash_equal(h->mrb,h->keys[k], key)) return k; \
if (__hash_equal(mrb,h->keys[k], key)) return k; \
} \
k = (k+h->inc) & (h->mask); \
} \
return h->n_buckets; \
} \
void kh_resize_##name(kh_##name##_t *h, khint_t new_n_buckets) \
void kh_resize_##name(mrb_state *mrb, kh_##name##_t *h, khint_t new_n_buckets) \
{ \
if (new_n_buckets < KHASH_MIN_SIZE) \
new_n_buckets = KHASH_MIN_SIZE; \
......@@ -158,26 +156,26 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
khint_t old_n_buckets = h->n_buckets; \
khint_t i; \
h->n_buckets = new_n_buckets; \
kh_alloc_##name(h); \
kh_alloc_##name(mrb, h); \
/* relocate */ \
for (i=0 ; i<old_n_buckets ; i++) { \
if (!__ac_iseither(old_ed_flags, i)) { \
khint_t k = kh_put_##name(h, old_keys[i]); \
khint_t k = kh_put_##name(mrb, h, old_keys[i]); \
if (kh_is_map) kh_value(h,k) = old_vals[i]; \
} \
} \
mrb_free(h->mrb, old_keys); \
mrb_free(mrb, old_keys); \
} \
} \
khint_t kh_put_##name(kh_##name##_t *h, khkey_t key) \
khint_t kh_put_##name(mrb_state *mrb, kh_##name##_t *h, khkey_t key) \
{ \
khint_t k; \
if (h->n_occupied >= h->upper_bound) { \
kh_resize_##name(h, h->n_buckets*2); \
kh_resize_##name(mrb, h, h->n_buckets*2); \
} \
k = __hash_func(h->mrb,key) & (h->mask); \
k = __hash_func(mrb,key) & (h->mask); \
while (!__ac_iseither(h->ed_flags, k)) { \
if (__hash_equal(h->mrb,h->keys[k], key)) break; \
if (__hash_equal(mrb,h->keys[k], key)) break; \
k = (k+h->inc) & (h->mask); \
} \
if (__ac_isempty(h->ed_flags, k)) { \
......@@ -194,7 +192,7 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
} \
return k; \
} \
void kh_del_##name(kh_##name##_t *h, khint_t x) \
void kh_del_##name(mrb_state *mrb, kh_##name##_t *h, khint_t x) \
{ \
h->ed_flags[x/4] |= __m_del[x%4]; \
h->size--; \
......@@ -207,7 +205,7 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
h2 = kh_init_##name(mrb); \
for (k = kh_begin(h); k != kh_end(h); k++) { \
if (kh_exist(h, k)) { \
k2 = kh_put_##name(h2, kh_key(h, k)); \
k2 = kh_put_##name(mrb, h2, kh_key(h, k)); \
if(kh_is_map) kh_value(h2, k2) = kh_value(h, k); \
} \
} \
......@@ -219,12 +217,12 @@ kh_fill_flags(uint8_t *p, uint8_t c, size_t len)
#define kh_init_size(name,mrb,size) kh_init_##name##_size(mrb,size)
#define kh_init(name,mrb) kh_init_##name(mrb)
#define kh_destroy(name, h) kh_destroy_##name(h)
#define kh_clear(name, h) kh_clear_##name(h)
#define kh_resize(name, h, s) kh_resize_##name(h, s)
#define kh_put(name, h, k) kh_put_##name(h, k)
#define kh_get(name, h, k) kh_get_##name(h, k)
#define kh_del(name, h, k) kh_del_##name(h, k)
#define kh_destroy(name, mrb, h) kh_destroy_##name(mrb, h)
#define kh_clear(name, mrb, h) kh_clear_##name(mrb, h)
#define kh_resize(name, mrb, h, s) kh_resize_##name(mrb, h, s)
#define kh_put(name, mrb, h, k) kh_put_##name(mrb, h, k)
#define kh_get(name, mrb, h, k) kh_get_##name(mrb, h, k)
#define kh_del(name, mrb, h, k) kh_del_##name(mrb, h, k)
#define kh_copy(name, mrb, h) kh_copy_##name(mrb, h)
#define kh_exist(h, x) (!__ac_iseither((h)->ed_flags, (x)))
......
......@@ -46,7 +46,7 @@ mrb_gc_mark_mt_size(mrb_state *mrb, struct RClass *c)
void
mrb_gc_free_mt(mrb_state *mrb, struct RClass *c)
{
kh_destroy(mt, c->mt);
kh_destroy(mt, mrb, c->mt);
}
void
......@@ -297,7 +297,7 @@ mrb_define_method_raw(mrb_state *mrb, struct RClass *c, mrb_sym mid, struct RPro
khiter_t k;
if (!h) h = c->mt = kh_init(mt, mrb);
k = kh_put(mt, h, mid);
k = kh_put(mt, mrb, h, mid);
kh_value(h, k) = p;
if (p) {
mrb_field_write_barrier(mrb, (struct RBasic *)c, (struct RBasic *)p);
......@@ -330,7 +330,7 @@ mrb_define_method_vm(mrb_state *mrb, struct RClass *c, mrb_sym name, mrb_value b
struct RProc *p;
if (!h) h = c->mt = kh_init(mt, mrb);
k = kh_put(mt, h, name);
k = kh_put(mt, mrb, h, name);
p = mrb_proc_ptr(body);
kh_value(h, k) = p;
if (p) {
......@@ -995,7 +995,7 @@ mrb_method_search_vm(mrb_state *mrb, struct RClass **cp, mrb_sym mid)
khash_t(mt) *h = c->mt;
if (h) {
k = kh_get(mt, h, mid);
k = kh_get(mt, mrb, h, mid);
if (k != kh_end(h)) {
m = kh_value(h, k);
if (!m) break;
......@@ -1180,7 +1180,7 @@ mrb_bob_missing(mrb_state *mrb, mrb_value mod)
}
mrb_bool
mrb_obj_respond_to(struct RClass* c, mrb_sym mid)
mrb_obj_respond_to(mrb_state *mrb, struct RClass* c, mrb_sym mid)
{
khiter_t k;
......@@ -1188,7 +1188,7 @@ mrb_obj_respond_to(struct RClass* c, mrb_sym mid)
khash_t(mt) *h = c->mt;
if (h) {
k = kh_get(mt, h, mid);
k = kh_get(mt, mrb, h, mid);
if (k != kh_end(h)) {
if (kh_value(h, k)) {
return TRUE; /* method exists */
......@@ -1206,7 +1206,7 @@ mrb_obj_respond_to(struct RClass* c, mrb_sym mid)
mrb_bool
mrb_respond_to(mrb_state *mrb, mrb_value obj, mrb_sym mid)
{
return mrb_obj_respond_to(mrb_class(mrb, obj), mid);
return mrb_obj_respond_to(mrb, mrb_class(mrb, obj), mid);
}
mrb_value
......@@ -1442,7 +1442,7 @@ undef_method(mrb_state *mrb, struct RClass *c, mrb_sym a)
{
mrb_value m;
if (!mrb_obj_respond_to(c, a)) {
if (!mrb_obj_respond_to(mrb, c, a)) {
mrb_name_error(mrb, a, "undefined method '%S' for class '%S'", mrb_sym2str(mrb, a), mrb_obj_value(c));
}
else {
......@@ -1713,7 +1713,7 @@ mrb_mod_method_defined(mrb_state *mrb, mrb_value mod)
id = get_sym_or_str_arg(mrb);
if (mrb_symbol_p(id)) {
method_defined_p = mrb_obj_respond_to(mrb_class_ptr(mod), mrb_symbol(id));
method_defined_p = mrb_obj_respond_to(mrb, mrb_class_ptr(mod), mrb_symbol(id));
}
else {
mrb_value sym = mrb_check_intern_str(mrb, id);
......@@ -1721,7 +1721,7 @@ mrb_mod_method_defined(mrb_state *mrb, mrb_value mod)
method_defined_p = FALSE;
}
else {
method_defined_p = mrb_obj_respond_to(mrb_class_ptr(mod), mrb_symbol(sym));
method_defined_p = mrb_obj_respond_to(mrb, mrb_class_ptr(mod), mrb_symbol(sym));
}
}
return mrb_bool_value(method_defined_p);
......@@ -1735,9 +1735,9 @@ remove_method(mrb_state *mrb, mrb_value mod, mrb_sym mid)
khiter_t k;
if (h) {
k = kh_get(mt, h, mid);
k = kh_get(mt, mrb, h, mid);
if (k != kh_end(h)) {
kh_del(mt, h, k);
kh_del(mt, mrb, h, k);
return;
}
}
......
......@@ -73,7 +73,7 @@ mrb_gc_mark_hash_size(mrb_state *mrb, struct RHash *hash)
void
mrb_gc_free_hash(mrb_state *mrb, struct RHash *hash)
{
if (hash->ht) kh_destroy(ht, hash->ht);
if (hash->ht) kh_destroy(ht, mrb, hash->ht);
}
......@@ -85,7 +85,7 @@ mrb_hash_new_capa(mrb_state *mrb, int capa)
h = (struct RHash*)mrb_obj_alloc(mrb, MRB_TT_HASH, mrb->hash_class);
h->ht = kh_init(ht, mrb);
if (capa > 0) {
kh_resize(ht, h->ht, capa);
kh_resize(ht, mrb, h->ht, capa);
}
h->iv = 0;
return mrb_obj_value(h);
......@@ -104,7 +104,7 @@ mrb_hash_get(mrb_state *mrb, mrb_value hash, mrb_value key)
khiter_t k;
if (h) {
k = kh_get(ht, h, key);
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h))
return kh_value(h, k);
}
......@@ -123,7 +123,7 @@ mrb_hash_fetch(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value def)
khiter_t k;
if (h) {
k = kh_get(ht, h, key);
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h))
return kh_value(h, k);
}
......@@ -142,11 +142,11 @@ mrb_hash_set(mrb_state *mrb, mrb_value hash, mrb_value key, mrb_value val) /* mr
h = RHASH_TBL(hash);
if (!h) h = RHASH_TBL(hash) = kh_init(ht, mrb);
k = kh_get(ht, h, key);
k = kh_get(ht, mrb, h, key);
if (k == kh_end(h)) {
/* expand */
int ai = mrb_gc_arena_save(mrb);
k = kh_put(ht, h, KEY(key));
k = kh_put(ht, mrb, h, KEY(key));
mrb_gc_arena_restore(mrb, ai);
}
......@@ -172,7 +172,7 @@ mrb_hash_dup(mrb_state *mrb, mrb_value hash)
for (k = kh_begin(h); k != kh_end(h); k++) {
if (kh_exist(h,k)) {
int ai = mrb_gc_arena_save(mrb);
ret_k = kh_put(ht, ret_h, KEY(kh_key(h,k)));
ret_k = kh_put(ht, mrb, ret_h, KEY(kh_key(h,k)));
mrb_gc_arena_restore(mrb, ai);
kh_val(ret_h, ret_k) = kh_val(h,k);
}
......@@ -490,10 +490,10 @@ mrb_hash_delete_key(mrb_state *mrb, mrb_value hash, mrb_value key)
mrb_value delVal;
if (h) {
k = kh_get(ht, h, key);
k = kh_get(ht, mrb, h, key);
if (k != kh_end(h)) {
delVal = kh_value(h, k);
kh_del(ht, h, k);
kh_del(ht, mrb, h, k);
return delVal;
}
}
......@@ -660,7 +660,7 @@ mrb_hash_clear(mrb_state *mrb, mrb_value hash)
{
khash_t(ht) *h = RHASH_TBL(hash);
if (h) kh_clear(ht, h);
if (h) kh_clear(ht, mrb, h);
return hash;
}
......@@ -920,7 +920,7 @@ mrb_hash_has_keyWithKey(mrb_state *mrb, mrb_value hash, mrb_value key)
khiter_t k;
if (h) {
k = kh_get(ht, h, key);
k = kh_get(ht, mrb, h, key);
return mrb_bool_value(k != kh_end(h));
}
return mrb_false_value();
......@@ -1026,7 +1026,7 @@ hash_equal(mrb_state *mrb, mrb_value hash1, mrb_value hash2, int eql)
for (k1 = kh_begin(h1); k1 != kh_end(h1); k1++) {
if (!kh_exist(h1, k1)) continue;
key = kh_key(h1,k1);
k2 = kh_get(ht, h2, key);
k2 = kh_get(ht, mrb, h2, key);
if (k2 != kh_end(h2)) {
if (mrb_equal(mrb, kh_value(h1,k1), kh_value(h2,k2))) {
continue; /* next key */
......
......@@ -733,7 +733,7 @@ method_entry_loop(mrb_state *mrb, struct RClass* klass, khash_t(st)* set)
if (!h) return;
for (i=0;i<kh_end(h);i++) {
if (kh_exist(h, i)) {
kh_put(st, set, kh_key(h,i));
kh_put(st, mrb, set, kh_key(h,i));
}
}
}
......@@ -765,7 +765,7 @@ class_instance_method_list(mrb_state *mrb, mrb_bool recur, struct RClass* klass,
mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set,i)));
}
}
kh_destroy(st, set);
kh_destroy(st, mrb, set);
return ary;
}
......@@ -797,7 +797,7 @@ mrb_obj_singleton_methods(mrb_state *mrb, mrb_bool recur, mrb_value obj)
mrb_ary_push(mrb, ary, mrb_symbol_value(kh_key(set,i)));
}
}
kh_destroy(st, set);
kh_destroy(st, mrb, set);
return ary;
}
......
......@@ -45,7 +45,7 @@ mrb_intern(mrb_state *mrb, const char *name, size_t len)
sname.len = len;
sname.name = name;
k = kh_get(n2s, h, sname);
k = kh_get(n2s, mrb, h, sname);
if (k != kh_end(h))
return kh_value(h, k);
......@@ -54,7 +54,7 @@ mrb_intern(mrb_state *mrb, const char *name, size_t len)
memcpy(p, name, len);
p[len] = 0;
sname.name = (const char*)p;
k = kh_put(n2s, h, sname);
k = kh_put(n2s, mrb, h, sname);
kh_value(h, k) = sym;
return sym;
......@@ -82,7 +82,7 @@ mrb_check_intern(mrb_state *mrb, const char *name, size_t len)
sname.len = len;
sname.name = name;
k = kh_get(n2s, h, sname);
k = kh_get(n2s, mrb, h, sname);
if (k != kh_end(h)) {
return mrb_symbol_value(kh_value(h, k));
}
......@@ -130,7 +130,7 @@ mrb_free_symtbl(mrb_state *mrb)
for (k = kh_begin(h); k != kh_end(h); k++)
if (kh_exist(h, k)) mrb_free(mrb, (char*)kh_key(h, k).name);
kh_destroy(n2s,mrb->name2sym);
kh_destroy(n2s, mrb, mrb->name2sym);
}
void
......
......@@ -311,7 +311,7 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
khash_t(iv) *h = &t->h;
khiter_t k;
k = kh_put(iv, h, sym);
k = kh_put(iv, mrb, h, sym);
kh_value(h, k) = val;
}
......@@ -321,7 +321,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
khash_t(iv) *h = &t->h;
khiter_t k;
k = kh_get(iv, h, sym);
k = kh_get(iv, mrb, h, sym);
if (k != kh_end(h)) {
if (vp) *vp = kh_value(h, k);
return TRUE;
......@@ -336,10 +336,10 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
khiter_t k;
if (h) {
k = kh_get(iv, h, sym);
k = kh_get(iv, mrb, h, sym);
if (k != kh_end(h)) {
mrb_value val = kh_value(h, k);
kh_del(iv, h, k);
kh_del(iv, mrb, h, k);
if (vp) *vp = val;
return TRUE;
}
......@@ -360,7 +360,7 @@ iv_foreach(mrb_state *mrb, iv_tbl *t, iv_foreach_func *func, void *p)
n = (*func)(mrb, kh_key(h, k), kh_value(h, k), p);
if (n > 0) return FALSE;
if (n < 0) {
kh_del(iv, h, k);
kh_del(iv, mrb, h, k);
}
}
}
......@@ -386,7 +386,7 @@ iv_copy(mrb_state *mrb, iv_tbl *t)
static void
iv_free(mrb_state *mrb, iv_tbl *t)
{
kh_destroy(iv, &t->h);
kh_destroy(iv, mrb, &t->h);
}
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment