Use size of hash's table in calculation
MODIFY
include/mruby/hash.h
include/mruby/hash.h
@@ -23,6 +23,7 @@ struct RHash {
#define mrb_hash_ptr(v) ((struct RHash*)(mrb_ptr(v)))
#define mrb_hash_value(p) mrb_obj_value((void*)(p))
+mrb_int os_memsize_of_hash_table(mrb_value obj);
MRB_API mrb_value mrb_hash_new_capa(mrb_state *mrb, mrb_int capa);
MRB_API mrb_value mrb_ensure_hash_type(mrb_state *mrb, mrb_value hash);
MRB_API mrb_value mrb_check_hash_type(mrb_state *mrb, mrb_value hash);

MODIFY
mrbgems/mruby-objectspace/src/mruby_objectspace.c
mrbgems/mruby-objectspace/src/mruby_objectspace.c
@@ -262,8 +262,12 @@ os_memsize_of_object(mrb_state* mrb, mrb_value obj, mrb_bool recurse, mrb_int* t
break;
}
case MRB_TT_HASH: {
- /*struct htable* htable = RHASH_TBL(obj);
- * Need htable & segment struct defs */
+ (*t) += mrb_objspace_page_slot_size() +
+ os_memsize_of_hash_table(obj);
+ if(recurse) {
+ os_memsize_of_object(mrb, mrb_hash_keys(mrb, obj), recurse, t);
+ os_memsize_of_object(mrb, mrb_hash_values(mrb, obj), recurse, t);
+ }
break;
}
case MRB_TT_ARRAY: {
@@ -325,7 +329,6 @@ os_memsize_of_object(mrb_state* mrb, mrb_value obj, mrb_bool recurse, mrb_int* t
case MRB_TT_FREE:
case MRB_TT_UNDEF:
case MRB_TT_ENV:
- case MRB_TT_ISTRUCT:
/* never used, silences compiler warning
* not having a default: clause lets the compiler tell us when there is a new
* TT not accounted for */

MODIFY
src/hash.c
src/hash.c
@@ -518,6 +518,20 @@ ht_foreach(mrb_state *mrb, htable *t, mrb_hash_foreach_func *func, void *p)
}
}
+mrb_int
+os_memsize_of_hash_table(mrb_value obj)
+{
+ struct htable *h = mrb_hash_ptr(obj)->ht;
+ mrb_int segkv_size = 0;
+
+ if(h->index) segkv_size = (sizeof(struct segkv) * h->index->capa);
+
+ return sizeof(htable) +
+ sizeof(segindex) +
+ (sizeof(segment) * h->size) +
+ segkv_size;
+}
+
/* Iterates over the hash table. */
MRB_API void
mrb_hash_foreach(mrb_state *mrb, struct RHash *hash, mrb_hash_foreach_func *func, void *p)

Use size of hash's table in calculation