class.c, variable,c: replace `size_t` by `int`.

That reduce memory consumption by iv/mt tables.
parent 77f4a8b6
...@@ -36,8 +36,8 @@ union mt_ptr { ...@@ -36,8 +36,8 @@ union mt_ptr {
/* method table structure */ /* method table structure */
typedef struct mt_tbl { typedef struct mt_tbl {
size_t size; int size;
size_t alloc; int alloc;
union mt_ptr *ptr; union mt_ptr *ptr;
} mt_tbl; } mt_tbl;
...@@ -65,8 +65,8 @@ static void mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union ...@@ -65,8 +65,8 @@ static void mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union
static void static void
mt_rehash(mrb_state *mrb, mt_tbl *t) mt_rehash(mrb_state *mrb, mt_tbl *t)
{ {
size_t old_alloc = t->alloc; int old_alloc = t->alloc;
size_t new_alloc = old_alloc+8; int new_alloc = old_alloc+8;
union mt_ptr *old_ptr = t->ptr; union mt_ptr *old_ptr = t->ptr;
khash_power2(new_alloc); khash_power2(new_alloc);
...@@ -79,7 +79,7 @@ mt_rehash(mrb_state *mrb, mt_tbl *t) ...@@ -79,7 +79,7 @@ mt_rehash(mrb_state *mrb, mt_tbl *t)
mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc]; mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
union mt_ptr *vals = old_ptr; union mt_ptr *vals = old_ptr;
for (size_t i = 0; i < old_alloc; i++) { for (int i = 0; i < old_alloc; i++) {
mrb_sym key = keys[i]; mrb_sym key = keys[i];
if (MT_KEY_P(key)) { if (MT_KEY_P(key)) {
mt_put(mrb, t, MT_KEY_SYM(key), MT_KEY_FLG(key), vals[i]); mt_put(mrb, t, MT_KEY_SYM(key), MT_KEY_FLG(key), vals[i]);
...@@ -94,8 +94,7 @@ mt_rehash(mrb_state *mrb, mt_tbl *t) ...@@ -94,8 +94,7 @@ mt_rehash(mrb_state *mrb, mt_tbl *t)
static void static void
mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union mt_ptr ptr) mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union mt_ptr ptr)
{ {
size_t hash, pos, start; int hash, pos, start, dpos = -1;
ssize_t dpos = -1;
if (t->alloc == 0) { if (t->alloc == 0) {
mt_rehash(mrb, t); mt_rehash(mrb, t);
...@@ -140,7 +139,7 @@ mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union mt_ptr ptr) ...@@ -140,7 +139,7 @@ mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union mt_ptr ptr)
static mrb_sym static mrb_sym
mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp) mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp)
{ {
size_t hash, pos, start; int hash, pos, start;
if (t == NULL) return 0; if (t == NULL) return 0;
if (t->alloc == 0) return 0; if (t->alloc == 0) return 0;
...@@ -150,7 +149,7 @@ mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp) ...@@ -150,7 +149,7 @@ mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp)
union mt_ptr *vals = t->ptr; union mt_ptr *vals = t->ptr;
hash = kh_int_hash_func(mrb, sym); hash = kh_int_hash_func(mrb, sym);
#ifdef MRB_USE_INLINE_METHOD_CACHE #ifdef MRB_USE_INLINE_METHOD_CACHE
size_t cpos = (hash^(uintptr_t)t) % MT_CACHE_SIZE; int cpos = (hash^(uintptr_t)t) % MT_CACHE_SIZE;
pos = mt_cache[cpos]; pos = mt_cache[cpos];
if (cpos < t->alloc && t->table[cpos].key == sym) { if (cpos < t->alloc && t->table[cpos].key == sym) {
return &t->table[cpos]; return &t->table[cpos];
...@@ -182,7 +181,7 @@ mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp) ...@@ -182,7 +181,7 @@ mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp)
static mrb_bool static mrb_bool
mt_del(mrb_state *mrb, mt_tbl *t, mrb_sym sym) mt_del(mrb_state *mrb, mt_tbl *t, mrb_sym sym)
{ {
size_t hash, pos, start; int hash, pos, start;
if (t == NULL) return FALSE; if (t == NULL) return FALSE;
if (t->alloc == 0) return FALSE; if (t->alloc == 0) return FALSE;
...@@ -213,7 +212,7 @@ static struct mt_tbl* ...@@ -213,7 +212,7 @@ static struct mt_tbl*
mt_copy(mrb_state *mrb, mt_tbl *t) mt_copy(mrb_state *mrb, mt_tbl *t)
{ {
mt_tbl *t2; mt_tbl *t2;
size_t i; int i;
if (t == NULL) return NULL; if (t == NULL) return NULL;
if (t->alloc == 0) return NULL; if (t->alloc == 0) return NULL;
...@@ -242,7 +241,7 @@ MRB_API void ...@@ -242,7 +241,7 @@ MRB_API void
mrb_mt_foreach(mrb_state *mrb, struct RClass *c, mrb_mt_foreach_func *fn, void *p) mrb_mt_foreach(mrb_state *mrb, struct RClass *c, mrb_mt_foreach_func *fn, void *p)
{ {
mt_tbl *t = c->mt; mt_tbl *t = c->mt;
size_t i; int i;
if (t == NULL) return; if (t == NULL) return;
if (t->alloc == 0) return; if (t->alloc == 0) return;
...@@ -276,7 +275,7 @@ void ...@@ -276,7 +275,7 @@ void
mrb_gc_mark_mt(mrb_state *mrb, struct RClass *c) mrb_gc_mark_mt(mrb_state *mrb, struct RClass *c)
{ {
mt_tbl *t = c->mt; mt_tbl *t = c->mt;
size_t i; int i;
if (t == NULL) return; if (t == NULL) return;
if (t->alloc == 0) return; if (t->alloc == 0) return;
...@@ -299,7 +298,7 @@ mrb_gc_mark_mt_size(mrb_state *mrb, struct RClass *c) ...@@ -299,7 +298,7 @@ mrb_gc_mark_mt_size(mrb_state *mrb, struct RClass *c)
struct mt_tbl *h = c->mt; struct mt_tbl *h = c->mt;
if (!h) return 0; if (!h) return 0;
return h->size; return (size_t)h->size;
} }
void void
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
/* Instance variable table structure */ /* Instance variable table structure */
typedef struct iv_tbl { typedef struct iv_tbl {
size_t size, alloc; int size, alloc;
mrb_value *ptr; mrb_value *ptr;
} iv_tbl; } iv_tbl;
...@@ -41,8 +41,8 @@ static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val); ...@@ -41,8 +41,8 @@ static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val);
static void static void
iv_rehash(mrb_state *mrb, iv_tbl *t) iv_rehash(mrb_state *mrb, iv_tbl *t)
{ {
size_t old_alloc = t->alloc; int old_alloc = t->alloc;
size_t new_alloc = old_alloc+4; int new_alloc = old_alloc+4;
mrb_value *old_ptr = t->ptr; mrb_value *old_ptr = t->ptr;
khash_power2(new_alloc); khash_power2(new_alloc);
...@@ -55,7 +55,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t) ...@@ -55,7 +55,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc]; mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
mrb_value *vals = old_ptr; mrb_value *vals = old_ptr;
for (size_t i = 0; i < old_alloc; i++) { for (int i = 0; i < old_alloc; i++) {
if (IV_KEY_P(keys[i])) { if (IV_KEY_P(keys[i])) {
iv_put(mrb, t, keys[i], vals[i]); iv_put(mrb, t, keys[i], vals[i]);
} }
...@@ -67,8 +67,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t) ...@@ -67,8 +67,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
static void static void
iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
{ {
size_t hash, pos, start; int hash, pos, start, dpos = -1;
int dpos = -1;
if (t == NULL) return; if (t == NULL) return;
if (t->alloc == 0) { if (t->alloc == 0) {
...@@ -112,10 +111,10 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val) ...@@ -112,10 +111,10 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
} }
/* Get a value for a symbol from the instance variable table. */ /* Get a value for a symbol from the instance variable table. */
static size_t static int
iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{ {
size_t hash, pos, start; int hash, pos, start;
if (t == NULL) return FALSE; if (t == NULL) return FALSE;
if (t->alloc == 0) return FALSE; if (t->alloc == 0) return FALSE;
...@@ -145,7 +144,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) ...@@ -145,7 +144,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
static mrb_bool static mrb_bool
iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
{ {
size_t hash, pos, start; int hash, pos, start;
if (t == NULL) return FALSE; if (t == NULL) return FALSE;
if (t->alloc == 0) return FALSE; if (t->alloc == 0) return FALSE;
...@@ -177,7 +176,7 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp) ...@@ -177,7 +176,7 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
static void static void
iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p) iv_foreach(mrb_state *mrb, iv_tbl *t, mrb_iv_foreach_func *func, void *p)
{ {
size_t i; int i;
if (t == NULL) return; if (t == NULL) return;
if (t->alloc == 0) return; if (t->alloc == 0) return;
...@@ -201,7 +200,7 @@ static size_t ...@@ -201,7 +200,7 @@ static size_t
iv_size(mrb_state *mrb, iv_tbl *t) iv_size(mrb_state *mrb, iv_tbl *t)
{ {
if (t == NULL) return 0; if (t == NULL) return 0;
return t->size; return (size_t)t->size;
} }
/* Copy the instance variable table. */ /* Copy the instance variable table. */
...@@ -209,7 +208,7 @@ static iv_tbl* ...@@ -209,7 +208,7 @@ static iv_tbl*
iv_copy(mrb_state *mrb, iv_tbl *t) iv_copy(mrb_state *mrb, iv_tbl *t)
{ {
iv_tbl *t2; iv_tbl *t2;
size_t i; int i;
if (t == NULL) return NULL; if (t == NULL) return NULL;
if (t->alloc == 0) return NULL; if (t->alloc == 0) return NULL;
...@@ -486,7 +485,7 @@ mrb_value ...@@ -486,7 +485,7 @@ mrb_value
mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj) mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj)
{ {
iv_tbl *t = obj->iv; iv_tbl *t = obj->iv;
size_t len = iv_size(mrb, t); int len = iv_size(mrb, t);
if (len > 0) { if (len > 0) {
const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj)); const char *cn = mrb_obj_classname(mrb, mrb_obj_value(obj));
...@@ -660,7 +659,7 @@ mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v) ...@@ -660,7 +659,7 @@ mrb_mod_cv_set(mrb_state *mrb, struct RClass *c, mrb_sym sym, mrb_value v)
while (c) { while (c) {
iv_tbl *t = c->iv; iv_tbl *t = c->iv;
size_t pos = iv_get(mrb, t, sym, NULL); int pos = iv_get(mrb, t, sym, NULL);
if (pos) { if (pos) {
mrb_check_frozen(mrb, c); mrb_check_frozen(mrb, c);
......
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