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