2006-12-31 18:02:22 +03:00
|
|
|
# Weak Reference class that does not bother GCing.
|
1998-01-16 15:19:09 +03:00
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
# foo = Object.new
|
1999-08-13 09:45:20 +04:00
|
|
|
# foo = Object.new
|
|
|
|
# p foo.to_s # original's class
|
1998-01-16 15:19:09 +03:00
|
|
|
# foo = WeakRef.new(foo)
|
1999-08-13 09:45:20 +04:00
|
|
|
# p foo.to_s # should be same class
|
1998-01-16 15:19:09 +03:00
|
|
|
# ObjectSpace.garbage_collect
|
1999-08-13 09:45:20 +04:00
|
|
|
# p foo.to_s # should raise exception (recycled)
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2006-12-31 18:02:22 +03:00
|
|
|
require "delegate"
|
|
|
|
require 'thread'
|
|
|
|
|
|
|
|
class WeakRef < Delegator
|
|
|
|
|
|
|
|
class RefError < StandardError
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
2003-12-10 11:26:36 +03:00
|
|
|
@@id_map = {} # obj -> [ref,...]
|
|
|
|
@@id_rev_map = {} # ref -> obj
|
2006-12-31 18:02:22 +03:00
|
|
|
@@mutex = Mutex.new
|
2005-08-12 11:17:36 +04:00
|
|
|
@@final = lambda {|id|
|
2006-12-31 18:02:22 +03:00
|
|
|
@@mutex.synchronize {
|
2003-12-10 11:26:36 +03:00
|
|
|
rids = @@id_map[id]
|
2000-07-17 13:38:10 +04:00
|
|
|
if rids
|
|
|
|
for rid in rids
|
2003-12-10 11:26:36 +03:00
|
|
|
@@id_rev_map.delete(rid)
|
2000-07-17 13:38:10 +04:00
|
|
|
end
|
2003-12-10 11:26:36 +03:00
|
|
|
@@id_map.delete(id)
|
2000-07-17 13:38:10 +04:00
|
|
|
end
|
2003-12-10 11:26:36 +03:00
|
|
|
rid = @@id_rev_map[id]
|
2000-07-17 13:38:10 +04:00
|
|
|
if rid
|
2003-12-10 11:26:36 +03:00
|
|
|
@@id_rev_map.delete(id)
|
|
|
|
@@id_map[rid].delete(id)
|
|
|
|
@@id_map.delete(rid) if @@id_map[rid].empty?
|
2000-07-17 13:38:10 +04:00
|
|
|
end
|
2006-12-31 18:02:22 +03:00
|
|
|
}
|
2000-07-17 13:38:10 +04:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:19:09 +03:00
|
|
|
def initialize(orig)
|
2006-12-31 18:02:22 +03:00
|
|
|
@__id = orig.object_id
|
|
|
|
ObjectSpace.define_finalizer orig, @@final
|
|
|
|
ObjectSpace.define_finalizer self, @@final
|
|
|
|
@@mutex.synchronize {
|
|
|
|
@@id_map[@__id] = [] unless @@id_map[@__id]
|
|
|
|
}
|
|
|
|
@@id_map[@__id].push self.object_id
|
|
|
|
@@id_rev_map[self.object_id] = @__id
|
2005-08-12 11:17:36 +04:00
|
|
|
super
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def __getobj__
|
2004-01-27 09:05:04 +03:00
|
|
|
unless @@id_rev_map[self.object_id] == @__id
|
|
|
|
Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2)
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2001-11-08 09:43:14 +03:00
|
|
|
begin
|
|
|
|
ObjectSpace._id2ref(@__id)
|
|
|
|
rescue RangeError
|
2004-01-27 09:05:04 +03:00
|
|
|
Kernel::raise RefError, "Illegal Reference - probably recycled", Kernel::caller(2)
|
2001-11-08 09:43:14 +03:00
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
2005-08-12 11:17:36 +04:00
|
|
|
def __setobj__(obj)
|
|
|
|
end
|
1998-01-16 15:19:09 +03:00
|
|
|
|
|
|
|
def weakref_alive?
|
2004-01-27 09:05:04 +03:00
|
|
|
@@id_rev_map[self.object_id] == @__id
|
1998-01-16 15:19:09 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
if __FILE__ == $0
|
2005-08-12 11:17:36 +04:00
|
|
|
# require 'thread'
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = Object.new
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # original's class
|
1999-01-20 07:59:39 +03:00
|
|
|
foo = WeakRef.new(foo)
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # should be same class
|
1999-01-20 07:59:39 +03:00
|
|
|
ObjectSpace.garbage_collect
|
2005-08-12 11:17:36 +04:00
|
|
|
ObjectSpace.garbage_collect
|
1999-08-13 09:45:20 +04:00
|
|
|
p foo.to_s # should raise exception (recycled)
|
1999-01-20 07:59:39 +03:00
|
|
|
end
|