From 6e3c750a23feac9da165553473e09f876620ca9f Mon Sep 17 00:00:00 2001 From: Al Hoang Date: Mon, 16 Mar 2009 22:53:25 +0900 Subject: [PATCH] Implement GC methods * The GC methods are attached to a Rufus::Lua::State object. * Implemented ** stopping and resuming GC ** Get the count in KB in use by Lua ** Manual GC --- TODO.txt | 5 +++-- lib/rufus/lua/lib.rb | 2 ++ lib/rufus/lua/state.rb | 47 ++++++++++++++++++++++++++++++++++++++++++ test/gc0.rb | 19 +++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 test/gc0.rb diff --git a/TODO.txt b/TODO.txt index 2230aa6..97c4496 100644 --- a/TODO.txt +++ b/TODO.txt @@ -12,8 +12,9 @@ [o] fib.lua, use local, bench ! (2 times faster almost) [ ] add GC control methods (Alain) -[ ] Add method to disable GC -[ ] Add method to enable GC +[X] Add method to disable GC +[X] Add method to enable GC [ ] Look at parameters in Lua GC that can be tweaked [ ] Add method to tune GC parameters +[ ] Test whether GC is truly independent for each state diff --git a/lib/rufus/lua/lib.rb b/lib/rufus/lua/lib.rb index 1e909d3..2931ab3 100644 --- a/lib/rufus/lua/lib.rb +++ b/lib/rufus/lua/lib.rb @@ -91,6 +91,8 @@ module Lib attach_function :luaL_loadbuffer, [ :pointer, :string, :int, :string ], :int attach_function :luaL_ref, [ :pointer, :int ], :int attach_function :luaL_unref, [ :pointer, :int, :int ], :void + + attach_function :lua_gc, [ :pointer, :int, :int ], :int end end end diff --git a/lib/rufus/lua/state.rb b/lib/rufus/lua/state.rb index 2a9d074..909b3c3 100644 --- a/lib/rufus/lua/state.rb +++ b/lib/rufus/lua/state.rb @@ -46,6 +46,16 @@ module StateMixin LUA_NOREF = -2 LUA_REFNIL = -1 + # Lua GC constants + LUA_GCSTOP = 0 + LUA_GCRESTART = 1 + LUA_GCCOLLECT = 2 + LUA_GCCOUNT = 3 + LUA_GCCOUNTB = 4 + LUA_GCSTEP = 5 + LUA_GCSETPAUSE = 6 + LUA_GCSETSTEPMUL = 7 + TNONE = -1 TNIL = 0 TBOOLEAN = 1 @@ -361,6 +371,43 @@ def [] (k) def close Lib.lua_close(@pointer) + @pointer = nil + end + + # + # Returns current amount of memory in KB in use by Lua + # + def gc_count + + raise "closed" unless @pointer + Lib.lua_gc(@pointer, LUA_GCCOUNT, 0) + end + + # + # Runs garbage collection + # + def gc_collect! + + raise "closed" unless @pointer + Lib.lua_gc(@pointer, LUA_GCCOLLECT, 0) + end + + # + # Stop garbage collection for this state + # + def gc_stop + + raise "closed" unless @pointer + Lib.lua_gc(@pointer, LUA_GCSTOP, 0) + end + + # + # Restart garbage collection for this state + # + def gc_resume + + raise "closed" unless @pointer + Lib.lua_gc(@pointer, LUA_GCRESTART, 0) end end end diff --git a/test/gc0.rb b/test/gc0.rb new file mode 100644 index 0000000..ab3232d --- /dev/null +++ b/test/gc0.rb @@ -0,0 +1,19 @@ +# +# Test garbage collection API from rufus-lua +# + +$:.unshift('lib') + +require 'rubygems' +require 'rufus/lua' + + +puts "Creating a new state..." +s = Rufus::Lua::State.new +puts " #{s.gc_count} KB in use by Lua interpreter" +puts " Calling into Lua..." +puts s.eval("return table.concat({ ' hello', 'from', 'Lua' }, ' ')") +puts " #{s.gc_count} KB in use by Lua interpreter" +puts "Performing forced garbage collection..." +s.gc_collect! +puts " #{s.gc_count} KB in use by Lua interpreter"