summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorRyan Scott <[email protected]>2013-05-17 07:41:13 +1000
committerRyan Scott <[email protected]>2013-05-17 07:41:13 +1000
commitf62cc5b1eebb29d244977c6030c04d9cb8ebefba (patch)
tree188d07664a7a32ae8e229bfb75028d9a5afcc598
parent222918deae84ae8dda26aa769bf3cc6b68e3aeec (diff)
downloadmruby-f62cc5b1eebb29d244977c6030c04d9cb8ebefba.tar.gz
mruby-f62cc5b1eebb29d244977c6030c04d9cb8ebefba.zip
Changed the object_count so that it only iterates over the RBasic object, not the full RVALUE known by the GC
-rw-r--r--include/mruby/gc.h33
-rw-r--r--mrbgems/mruby-objectspace/src/mruby_objectspace.c24
-rw-r--r--mrbgems/mruby-objectspace/test/objectspace.rb11
-rw-r--r--src/gc.c22
4 files changed, 52 insertions, 38 deletions
diff --git a/include/mruby/gc.h b/include/mruby/gc.h
index 00564c4fa..552321be2 100644
--- a/include/mruby/gc.h
+++ b/include/mruby/gc.h
@@ -12,38 +12,9 @@ extern "C" {
#endif
#include "mruby.h"
-#include "mruby/array.h"
-#include "mruby/class.h"
-#include "mruby/data.h"
-#include "mruby/hash.h"
-#include "mruby/proc.h"
-#include "mruby/range.h"
-#include "mruby/string.h"
-#include "mruby/variable.h"
+#include "mruby/value.h"
-struct free_obj {
- MRB_OBJECT_HEADER;
- struct RBasic *next;
-};
-
-struct RVALUE {
- union {
- struct free_obj free;
- struct RBasic basic;
- struct RObject object;
- struct RClass klass;
- struct RString string;
- struct RArray array;
- struct RHash hash;
- struct RRange range;
- struct RData data;
- struct RProc proc;
- } as;
-};
-
-typedef struct RVALUE RVALUE;
-
-typedef int each_object_callback(RVALUE *obj, void *data);
+typedef int each_object_callback(mrb_state *mrb, struct RBasic obj, void *data);
void mrb_objspace_each_objects(mrb_state *mrb, each_object_callback* callback, void *data);
#if defined(__cplusplus)
diff --git a/mrbgems/mruby-objectspace/src/mruby_objectspace.c b/mrbgems/mruby-objectspace/src/mruby_objectspace.c
index 2d064044d..cf3cf631f 100644
--- a/mrbgems/mruby-objectspace/src/mruby_objectspace.c
+++ b/mrbgems/mruby-objectspace/src/mruby_objectspace.c
@@ -1,7 +1,9 @@
+#include <stdio.h>
+
#include <mruby.h>
#include <mruby/gc.h>
#include <mruby/hash.h>
-#include <stdio.h>
+#include <mruby/value.h>
/*
* call-seq:
@@ -25,22 +27,30 @@
struct os_count_struct {
size_t total;
+ size_t freed;
size_t counts[MRB_TT_MAXDEFINE+1];
};
-void os_count_object_type(RVALUE *obj, void *data)
+void os_count_object_type(mrb_state *mrb, struct RBasic obj, void *data)
{
struct os_count_struct* obj_count;
obj_count = (struct os_count_struct*)(data);
- obj_count->counts[mrb_type(obj->as.basic)]++;
- obj_count->total++;
+ obj_count->counts[mrb_type(obj)]++;
+ if (is_dead(mrb, &obj))
+ {
+ obj_count->freed++;
+ }
+ else
+ {
+ obj_count->total++;
+ }
+
}
mrb_value
os_count_objects(mrb_state *mrb, mrb_value self)
{
struct os_count_struct obj_count;
- size_t freed = 0;
size_t i;
mrb_value hash;
struct heap_page* page = mrb->heaps;
@@ -56,11 +66,13 @@ os_count_objects(mrb_state *mrb, mrb_value self)
for (i = 0; i <= MRB_TT_MAXDEFINE; i++) {
obj_count.counts[i] = 0;
}
+ obj_count.total = 0;
+ obj_count.freed = 0;
mrb_objspace_each_objects(mrb, os_count_object_type, &obj_count);
mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "TOTAL")), mrb_fixnum_value(obj_count.total));
- mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "FREE")), mrb_fixnum_value(freed));
+ mrb_hash_set(mrb, hash, mrb_symbol_value(mrb_intern_cstr(mrb, "FREE")), mrb_fixnum_value(obj_count.freed));
for (i = 0; i < MRB_TT_MAXDEFINE; i++) {
mrb_value type;
diff --git a/mrbgems/mruby-objectspace/test/objectspace.rb b/mrbgems/mruby-objectspace/test/objectspace.rb
index dbf3bcbb8..4a362740c 100644
--- a/mrbgems/mruby-objectspace/test/objectspace.rb
+++ b/mrbgems/mruby-objectspace/test/objectspace.rb
@@ -5,6 +5,11 @@ assert('ObjectSpace.count_objects') do
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
assert_true(h.values.all? {|x| x.is_a?(Integer) })
+ assert_true(h.has_key?(:TOTAL))
+ assert_true(h.has_key?(:FREE))
+
+ p h.inspect
+
h = ObjectSpace.count_objects
assert_kind_of(Hash, h)
assert_true(h.keys.all? {|x| x.is_a?(Symbol) || x.is_a?(Integer) })
@@ -12,6 +17,8 @@ assert('ObjectSpace.count_objects') do
assert_raise(TypeError) { ObjectSpace.count_objects(1) }
+ p h.inspect
+
h0 = {:MRB_TT_FOO=>1000}
h = ObjectSpace.count_objects(h0)
assert_false(h0.has_key?(:MRB_TT_FOO))
@@ -31,4 +38,8 @@ assert('ObjectSpace.count_objects') do
assert_equal(h_before[:MRB_TT_HASH] + 1000, h[:MRB_TT_HASH])
assert_equal(h_before[:MRB_TT_HASH], h_after[:MRB_TT_HASH])
+
+ p h.inspect
+ p h_after.inspect
+
end \ No newline at end of file
diff --git a/src/gc.c b/src/gc.c
index 199551c55..f824ee9d4 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -73,6 +73,26 @@
*/
+struct free_obj {
+ MRB_OBJECT_HEADER;
+ struct RBasic *next;
+};
+
+typedef struct {
+ union {
+ struct free_obj free;
+ struct RBasic basic;
+ struct RObject object;
+ struct RClass klass;
+ struct RString string;
+ struct RArray array;
+ struct RHash hash;
+ struct RRange range;
+ struct RData data;
+ struct RProc proc;
+ } as;
+} RVALUE;
+
#ifdef GC_PROFILE
#include <stdio.h>
#include <sys/time.h>
@@ -1133,7 +1153,7 @@ mrb_objspace_each_objects(mrb_state *mrb, each_object_callback* callback, void *
p = page->objects;
pend = p + MRB_HEAP_PAGE_SIZE;
for (;p < pend; p++) {
- callback(p, data);
+ callback(mrb, p->as.basic, data);
}
page = page->next;