mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
class.c, variable,c: replace size_t by int.
That reduce memory consumption by iv/mt tables.
This commit is contained in:
+13
-14
@@ -36,8 +36,8 @@ union mt_ptr {
|
||||
|
||||
/* method table structure */
|
||||
typedef struct mt_tbl {
|
||||
size_t size;
|
||||
size_t alloc;
|
||||
int size;
|
||||
int alloc;
|
||||
union mt_ptr *ptr;
|
||||
} mt_tbl;
|
||||
|
||||
@@ -65,8 +65,8 @@ static void mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union
|
||||
static void
|
||||
mt_rehash(mrb_state *mrb, mt_tbl *t)
|
||||
{
|
||||
size_t old_alloc = t->alloc;
|
||||
size_t new_alloc = old_alloc+8;
|
||||
int old_alloc = t->alloc;
|
||||
int new_alloc = old_alloc+8;
|
||||
union mt_ptr *old_ptr = t->ptr;
|
||||
|
||||
khash_power2(new_alloc);
|
||||
@@ -79,7 +79,7 @@ mt_rehash(mrb_state *mrb, mt_tbl *t)
|
||||
|
||||
mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
|
||||
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];
|
||||
if (MT_KEY_P(key)) {
|
||||
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)
|
||||
static void
|
||||
mt_put(mrb_state *mrb, mt_tbl *t, mrb_sym sym, mrb_sym flags, union mt_ptr ptr)
|
||||
{
|
||||
size_t hash, pos, start;
|
||||
ssize_t dpos = -1;
|
||||
int hash, pos, start, dpos = -1;
|
||||
|
||||
if (t->alloc == 0) {
|
||||
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)
|
||||
static mrb_sym
|
||||
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->alloc == 0) return 0;
|
||||
@@ -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;
|
||||
hash = kh_int_hash_func(mrb, sym);
|
||||
#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];
|
||||
if (cpos < t->alloc && t->table[cpos].key == sym) {
|
||||
return &t->table[cpos];
|
||||
@@ -182,7 +181,7 @@ mt_get(mrb_state *mrb, mt_tbl *t, mrb_sym sym, union mt_ptr *pp)
|
||||
static mrb_bool
|
||||
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->alloc == 0) return FALSE;
|
||||
@@ -213,7 +212,7 @@ static struct mt_tbl*
|
||||
mt_copy(mrb_state *mrb, mt_tbl *t)
|
||||
{
|
||||
mt_tbl *t2;
|
||||
size_t i;
|
||||
int i;
|
||||
|
||||
if (t == NULL) return NULL;
|
||||
if (t->alloc == 0) return NULL;
|
||||
@@ -242,7 +241,7 @@ MRB_API void
|
||||
mrb_mt_foreach(mrb_state *mrb, struct RClass *c, mrb_mt_foreach_func *fn, void *p)
|
||||
{
|
||||
mt_tbl *t = c->mt;
|
||||
size_t i;
|
||||
int i;
|
||||
|
||||
if (t == NULL) return;
|
||||
if (t->alloc == 0) return;
|
||||
@@ -276,7 +275,7 @@ void
|
||||
mrb_gc_mark_mt(mrb_state *mrb, struct RClass *c)
|
||||
{
|
||||
mt_tbl *t = c->mt;
|
||||
size_t i;
|
||||
int i;
|
||||
|
||||
if (t == NULL) return;
|
||||
if (t->alloc == 0) return;
|
||||
@@ -299,7 +298,7 @@ mrb_gc_mark_mt_size(mrb_state *mrb, struct RClass *c)
|
||||
struct mt_tbl *h = c->mt;
|
||||
|
||||
if (!h) return 0;
|
||||
return h->size;
|
||||
return (size_t)h->size;
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
+13
-14
@@ -14,7 +14,7 @@
|
||||
|
||||
/* Instance variable table structure */
|
||||
typedef struct iv_tbl {
|
||||
size_t size, alloc;
|
||||
int size, alloc;
|
||||
mrb_value *ptr;
|
||||
} iv_tbl;
|
||||
|
||||
@@ -41,8 +41,8 @@ static void iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val);
|
||||
static void
|
||||
iv_rehash(mrb_state *mrb, iv_tbl *t)
|
||||
{
|
||||
size_t old_alloc = t->alloc;
|
||||
size_t new_alloc = old_alloc+4;
|
||||
int old_alloc = t->alloc;
|
||||
int new_alloc = old_alloc+4;
|
||||
mrb_value *old_ptr = t->ptr;
|
||||
|
||||
khash_power2(new_alloc);
|
||||
@@ -55,7 +55,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
|
||||
|
||||
mrb_sym *keys = (mrb_sym*)&old_ptr[old_alloc];
|
||||
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])) {
|
||||
iv_put(mrb, t, keys[i], vals[i]);
|
||||
}
|
||||
@@ -67,8 +67,7 @@ iv_rehash(mrb_state *mrb, iv_tbl *t)
|
||||
static void
|
||||
iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
|
||||
{
|
||||
size_t hash, pos, start;
|
||||
int dpos = -1;
|
||||
int hash, pos, start, dpos = -1;
|
||||
|
||||
if (t == NULL) return;
|
||||
if (t->alloc == 0) {
|
||||
@@ -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. */
|
||||
static size_t
|
||||
static int
|
||||
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->alloc == 0) return FALSE;
|
||||
@@ -145,7 +144,7 @@ iv_get(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
|
||||
static mrb_bool
|
||||
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->alloc == 0) return FALSE;
|
||||
@@ -177,7 +176,7 @@ iv_del(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value *vp)
|
||||
static void
|
||||
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->alloc == 0) return;
|
||||
@@ -201,7 +200,7 @@ static size_t
|
||||
iv_size(mrb_state *mrb, iv_tbl *t)
|
||||
{
|
||||
if (t == NULL) return 0;
|
||||
return t->size;
|
||||
return (size_t)t->size;
|
||||
}
|
||||
|
||||
/* Copy the instance variable table. */
|
||||
@@ -209,7 +208,7 @@ static iv_tbl*
|
||||
iv_copy(mrb_state *mrb, iv_tbl *t)
|
||||
{
|
||||
iv_tbl *t2;
|
||||
size_t i;
|
||||
int i;
|
||||
|
||||
if (t == NULL) return NULL;
|
||||
if (t->alloc == 0) return NULL;
|
||||
@@ -486,7 +485,7 @@ mrb_value
|
||||
mrb_obj_iv_inspect(mrb_state *mrb, struct RObject *obj)
|
||||
{
|
||||
iv_tbl *t = obj->iv;
|
||||
size_t len = iv_size(mrb, t);
|
||||
int len = iv_size(mrb, t);
|
||||
|
||||
if (len > 0) {
|
||||
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)
|
||||
|
||||
while (c) {
|
||||
iv_tbl *t = c->iv;
|
||||
size_t pos = iv_get(mrb, t, sym, NULL);
|
||||
int pos = iv_get(mrb, t, sym, NULL);
|
||||
|
||||
if (pos) {
|
||||
mrb_check_frozen(mrb, c);
|
||||
|
||||
Reference in New Issue
Block a user