From a8c43a4db22cac3ed5b08b3be1e793dc083169d5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alexis=20Laferri=C3=A8re?= Date: Mon, 5 Mar 2012 22:22:53 -0500 Subject: [PATCH] lib: adds a method to force invocation of garbage collector MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The method Sys::force_garbage_collection is implemented internally. Signed-off-by: Alexis Laferrière --- clib/gc.c | 4 ++++ clib/gc.h | 2 ++ lib/standard/gc.nit | 5 +++++ lib/standard/standard.nit | 1 + src/compiling/compiling_icode.nit | 4 ++++ tests/sav/test_gc_forced.sav | 2 ++ tests/test_gc_forced.nit | 22 ++++++++++++++++++++++ 7 files changed, 40 insertions(+) create mode 100644 lib/standard/gc.nit create mode 100644 tests/sav/test_gc_forced.sav create mode 100644 tests/test_gc_forced.nit diff --git a/clib/gc.c b/clib/gc.c index 19f2d9e..eb9df80 100644 --- 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 ); +} diff --git a/clib/gc.h b/clib/gc.h index 142ece0..18395a5 100644 --- 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 index 0000000..4778bd8 --- /dev/null +++ b/lib/standard/gc.nit @@ -0,0 +1,5 @@ +import kernel + +redef class Sys + fun force_garbage_collection is intern +end diff --git a/lib/standard/standard.nit b/lib/standard/standard.nit index 2c5f73a..7c62c1b 100644 --- a/lib/standard/standard.nit +++ b/lib/standard/standard.nit @@ -26,3 +26,4 @@ import string import collection import math import kernel +import gc diff --git a/src/compiling/compiling_icode.nit b/src/compiling/compiling_icode.nit index aaa49f0..169d411 100644 --- a/src/compiling/compiling_icode.nit +++ b/src/compiling/compiling_icode.nit @@ -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 index 0000000..3bd1f0e --- /dev/null +++ b/tests/sav/test_gc_forced.sav @@ -0,0 +1,2 @@ +foo +bar diff --git a/tests/test_gc_forced.nit b/tests/test_gc_forced.nit new file mode 100644 index 0000000..2ddf08e --- /dev/null +++ b/tests/test_gc_forced.nit @@ -0,0 +1,22 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2012 Alexis Laferrière +# +# 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 + -- 1.7.9.5