Skip to content
This repository has been archived by the owner on Mar 9, 2024. It is now read-only.

Commit

Permalink
Implement GC methods
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hoanga committed Mar 16, 2009
1 parent 6f13ef1 commit 6e3c750
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
5 changes: 3 additions & 2 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

2 changes: 2 additions & 0 deletions lib/rufus/lua/lib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 47 additions & 0 deletions lib/rufus/lua/state.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions test/gc0.rb
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 6e3c750

Please sign in to comment.