summaryrefslogtreecommitdiff
path: root/lib/weakref.rb
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>1999-08-13 05:45:20 +0000
commit65a5162550f58047974793cdc8067a970b2435c0 (patch)
tree082bb7d5568f3b2e36e3fe166e9f3039394fcf44 /lib/weakref.rb
parentfcd020c83028f5610d382e85a2df00223e12bd7e (diff)
1.4.0
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@520 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/weakref.rb')
-rw-r--r--lib/weakref.rb34
1 files changed, 20 insertions, 14 deletions
diff --git a/lib/weakref.rb b/lib/weakref.rb
index c31e959..c6fe8cd 100644
--- a/lib/weakref.rb
+++ b/lib/weakref.rb
@@ -2,11 +2,12 @@
#
# Usage:
# foo = Object.new
-# foo.hash
+# foo = Object.new
+# p foo.to_s # original's class
# foo = WeakRef.new(foo)
-# foo.hash
+# p foo.to_s # should be same class
# ObjectSpace.garbage_collect
-# foo.hash # => Raises WeakRef::RefError (because original GC'ed)
+# p foo.to_s # should raise exception (recycled)
require "delegate"
@@ -15,27 +16,31 @@ class WeakRef<Delegator
class RefError<StandardError
end
- ID_MAP = {}
- ID_REV_MAP = {}
+ ID_MAP = {} # obj -> [ref,...]
+ ID_REV_MAP = {} # ref -> obj
ObjectSpace.add_finalizer(lambda{|id|
- rid = ID_MAP[id]
- if rid
- ID_REV_MAP[rid] = nil
+ rids = ID_MAP[id]
+ if rids
+ for rid in rids
+ ID_REV_MAP[rid] = nil
+ end
ID_MAP[id] = nil
end
rid = ID_REV_MAP[id]
if rid
ID_REV_MAP[id] = nil
- ID_MAP[rid] = nil
+ ID_MAP[rid].delete(id)
+ ID_MAP[rid] = nil if ID_MAP[rid].empty?
end
})
-
+
def initialize(orig)
super
@__id = orig.__id__
ObjectSpace.call_finalizer orig
ObjectSpace.call_finalizer self
- ID_MAP[@__id] = self.__id__
+ ID_MAP[@__id] = [] unless ID_MAP[@__id]
+ ID_MAP[@__id].concat self.__id__
ID_REV_MAP[self.id] = @__id
end
@@ -60,10 +65,11 @@ class WeakRef<Delegator
end
if __FILE__ == $0
+ require 'thread'
foo = Object.new
- p foo.hash # original's hash value
+ p foo.to_s # original's class
foo = WeakRef.new(foo)
- p foo.hash # should be same hash value
+ p foo.to_s # should be same class
ObjectSpace.garbage_collect
- p foo.hash # should raise exception (recycled)
+ p foo.to_s # should raise exception (recycled)
end