summaryrefslogtreecommitdiff
path: root/gc.c
diff options
context:
space:
mode:
authorkazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-15 07:54:11 +0000
committerkazu <kazu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2008-04-15 07:54:11 +0000
commitb54786ffbce89d8bd3320eeed0eb385c647c744d (patch)
treed149585f46fcb689b654de9550c09941941a859e /gc.c
parentaffe3e16464e23cf79d49508bb2d0d17f75bac38 (diff)
* signal.c, gc.c: New methods: GC.stress, GC.stress=;
backported from 1.9. a patch from Tadashi Saito in [ruby-dev:34394] and bug#19000 git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/branches/ruby_1_8@16030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c44
1 files changed, 42 insertions, 2 deletions
diff --git a/gc.c b/gc.c
index 5802f0a945..daf3bcd51f 100644
--- a/gc.c
+++ b/gc.c
@@ -76,6 +76,8 @@ static void run_final();
static VALUE nomem_error;
static void garbage_collect();
+int ruby_gc_stress = 0;
+
void
rb_memerror()
{
@@ -89,6 +91,41 @@ rb_memerror()
rb_exc_raise(nomem_error);
}
+/*
+ * call-seq:
+ * GC.stress => true or false
+ *
+ * returns current status of GC stress mode.
+ */
+
+static VALUE
+gc_stress_get(self)
+ VALUE self;
+{
+ return ruby_gc_stress ? Qtrue : Qfalse;
+}
+
+/*
+ * call-seq:
+ * GC.stress = bool => bool
+ *
+ * updates GC stress mode.
+ *
+ * When GC.stress = true, GC is invoked for all GC opportunity:
+ * all memory and object allocation.
+ *
+ * Since it makes Ruby very slow, it is only for debugging.
+ */
+
+static VALUE
+gc_stress_set(self, bool)
+ VALUE self, bool;
+{
+ rb_secure(2);
+ ruby_gc_stress = RTEST(bool);
+ return bool;
+}
+
void *
ruby_xmalloc(size)
long size;
@@ -101,7 +138,7 @@ ruby_xmalloc(size)
if (size == 0) size = 1;
malloc_increase += size;
- if (malloc_increase > malloc_limit) {
+ if (ruby_gc_stress || malloc_increase > malloc_limit) {
garbage_collect();
}
RUBY_CRITICAL(mem = malloc(size));
@@ -141,6 +178,7 @@ ruby_xrealloc(ptr, size)
if (!ptr) return xmalloc(size);
if (size == 0) size = 1;
malloc_increase += size;
+ if (ruby_gc_stress) garbage_collect();
RUBY_CRITICAL(mem = realloc(ptr, size));
if (!mem) {
garbage_collect();
@@ -383,7 +421,7 @@ rb_newobj()
if (during_gc)
rb_bug("object allocation during garbage collection phase");
- if (!freelist) garbage_collect();
+ if (ruby_gc_stress || !freelist) garbage_collect();
obj = (VALUE)freelist;
freelist = freelist->as.free.next;
@@ -2037,6 +2075,8 @@ Init_GC()
rb_define_singleton_method(rb_mGC, "start", rb_gc_start, 0);
rb_define_singleton_method(rb_mGC, "enable", rb_gc_enable, 0);
rb_define_singleton_method(rb_mGC, "disable", rb_gc_disable, 0);
+ rb_define_singleton_method(rb_mGC, "stress", gc_stress_get, 0);
+ rb_define_singleton_method(rb_mGC, "stress=", gc_stress_set, 1);
rb_define_method(rb_mGC, "garbage_collect", rb_gc_start, 0);
rb_mObSpace = rb_define_module("ObjectSpace");