lib: adds a method to force invocation of garbage collector
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 6 Mar 2012 03:22:53 +0000 (22:22 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 6 Mar 2012 03:22:53 +0000 (22:22 -0500)
The method Sys::force_garbage_collection is implemented internally.

Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

clib/gc.c
clib/gc.h
lib/standard/gc.nit [new file with mode: 0644]
lib/standard/standard.nit
src/compiling/compiling_icode.nit
tests/sav/test_gc_forced.sav [new file with mode: 0644]
tests/test_gc_forced.nit [new file with mode: 0644]

index 19f2d9e..eb9df80 100644 (file)
--- a/clib/gc.c
+++ b/clib/gc.c
@@ -245,3 +245,7 @@ void GC_add_static_object(val_t *pointer) {
        GC_List_Push(&staticObjects, pointer);
 }
 
+/* Is invoked by intern method Sys:force_garbage_collection */
+void Nit_gc_force_garbage_collection( void ) {
+       GC_enlarge_and_collect( 0 );
+}
index 142ece0..18395a5 100644 (file)
--- a/clib/gc.h
+++ b/clib/gc.h
@@ -25,4 +25,6 @@ void *Nit_gc_malloc(size_t size);
 
 void GC_add_static_object(val_t *pointer);
 
+void Nit_gc_force_garbage_collection( void );
+
 #endif
diff --git a/lib/standard/gc.nit b/lib/standard/gc.nit
new file mode 100644 (file)
index 0000000..4778bd8
--- /dev/null
@@ -0,0 +1,5 @@
+import kernel
+
+redef class Sys
+       fun force_garbage_collection is intern
+end
index 2c5f73a..7c62c1b 100644 (file)
@@ -26,3 +26,4 @@ import string
 import collection
 import math
 import kernel
+import gc
index aaa49f0..169d411 100644 (file)
@@ -868,6 +868,10 @@ redef class INative
                        else if n == once "copy_to".to_symbol then
                                s = "(void)memcpy(UNBOX_NativeString({regs[1]})+UNTAG_Int({regs[4]}), UNBOX_NativeString({regs[0]})+UNTAG_Int({regs[3]}), UNTAG_Int({regs[2]}));"
                        end
+               else if c == once "Sys".to_symbol then
+                       if n == once "force_garbage_collection".to_symbol then
+                               s = "Nit_gc_force_garbage_collection()"
+                       end
                else if n == once "object_id".to_symbol then
                        s = "TAG_Int((bigint)((obj_t){regs[0]})[1].object_id)"
                else if n == once "sys".to_symbol then
diff --git a/tests/sav/test_gc_forced.sav b/tests/sav/test_gc_forced.sav
new file mode 100644 (file)
index 0000000..3bd1f0e
--- /dev/null
@@ -0,0 +1,2 @@
+foo
+bar
diff --git a/tests/test_gc_forced.nit b/tests/test_gc_forced.nit
new file mode 100644 (file)
index 0000000..2ddf08e
--- /dev/null
@@ -0,0 +1,22 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2012 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+var x = "foo"
+var y = "bar"
+sys.force_garbage_collection
+print x
+print y
+