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
|
|
|
|
1999-08-13 01:45:20 -04:00
|
|
|
ID_MAP = {} # obj -> [ref,...]
|
|
|
|
ID_REV_MAP = {} # ref -> obj
|
1998-01-16 07:19:09 -05:00
|
|
|
ObjectSpace.add_finalizer(lambda{|id|
|
1999-08-13 01:45:20 -04:00
|
|
|
rids = ID_MAP[id]
|
|
|
|
if rids
|
|
|
|
for rid in rids
|
|
|
|
ID_REV_MAP[rid] = nil
|
|
|
|
end
|
1998-01-16 07:19:09 -05:00
|
|
|
ID_MAP[id] = nil
|
|
|
|
end
|
|
|
|
rid = ID_REV_MAP[id]
|
|
|
|
if rid
|
|
|
|
ID_REV_MAP[id] = nil
|
1999-08-13 01:45:20 -04:00
|
|
|
ID_MAP[rid].delete(id)
|
|
|
|
ID_MAP[rid] = nil if ID_MAP[rid].empty?
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
})
|
1999-08-13 01:45:20 -04:00
|
|
|
|
1998-01-16 07:19:09 -05:00
|
|
|
def initialize(orig)
|
|
|
|
super
|
1999-01-19 23:59:39 -05:00
|
|
|
@__id = orig.__id__
|
1998-01-16 07:19:09 -05:00
|
|
|
ObjectSpace.call_finalizer orig
|
1999-01-19 23:59:39 -05:00
|
|
|
ObjectSpace.call_finalizer self
|
1999-08-13 01:45:20 -04:00
|
|
|
ID_MAP[@__id] = [] unless ID_MAP[@__id]
|
2000-03-05 23:15:42 -05:00
|
|
|
ID_MAP[@__id].push self.__id__
|
1999-01-19 23:59:39 -05:00
|
|
|
ID_REV_MAP[self.id] = @__id
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def __getobj__
|
1999-01-19 23:59:39 -05:00
|
|
|
unless ID_MAP[@__id]
|
|
|
|
raise RefError, "Illegal Reference - probably recycled", caller(2)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
1999-01-19 23:59:39 -05:00
|
|
|
ObjectSpace._id2ref(@__id)
|
1998-01-16 07:19:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def weakref_alive?
|
1999-01-19 23:59:39 -05:00
|
|
|
if ID_MAP[@__id]
|
1998-01-16 07:19:09 -05:00
|
|
|
true
|
|
|
|
else
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def []
|
|
|
|
__getobj__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
1999-01-19 23:59:39 -05:00
|
|
|
if __FILE__ == $0
|
1999-08-13 01:45:20 -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
|
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
|