aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorEmilio Tagua <miloops@gmail.com>2010-10-14 13:24:26 -0300
committerEmilio Tagua <miloops@gmail.com>2010-11-19 19:08:28 -0300
commitada014929b01af5ce8ca5e6fdd13401bc3c392f5 (patch)
tree758926882e6ca30d137327a22ae3f828a6b594a8 /activesupport
parent301dd3d5143077f95bebd434ca2ad1c80c1b5866 (diff)
downloadrails-ada014929b01af5ce8ca5e6fdd13401bc3c392f5.tar.gz
rails-ada014929b01af5ce8ca5e6fdd13401bc3c392f5.tar.bz2
rails-ada014929b01af5ce8ca5e6fdd13401bc3c392f5.zip
Use ActiveSupport::WeakHash for MRI, JRuby prefers Weakling.
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/weak_hash.rb46
1 files changed, 46 insertions, 0 deletions
diff --git a/activesupport/lib/active_support/weak_hash.rb b/activesupport/lib/active_support/weak_hash.rb
new file mode 100644
index 0000000000..c6bf098074
--- /dev/null
+++ b/activesupport/lib/active_support/weak_hash.rb
@@ -0,0 +1,46 @@
+module ActiveSupport
+ class WeakHash
+ def initialize(cache = Hash.new)
+ @cache = cache
+ @key_map = {}
+ @rev_cache = Hash.new{|h,k| h[k] = {}}
+ @reclaim_value = lambda do |value_id|
+ if value = @rev_cache.delete(value_id)
+ value.each_key{|key| @cache.delete key}
+ end
+ end
+ end
+
+ def [](key)
+ value_id = @cache[key]
+ value_id && ObjectSpace._id2ref(value_id)
+ rescue RangeError
+ nil
+ end
+
+ def []=(key, value)
+ key2 = case key
+ when Fixnum, Symbol, true, false, nil
+ key
+ else
+ key.dup
+ end
+
+ @rev_cache[value.object_id][key2] = true
+ @cache[key2] = value.object_id
+ @key_map[key.object_id] = key2
+
+ ObjectSpace.define_finalizer(value, @reclaim_value)
+ end
+
+ def clear
+ @cache.clear
+ end
+
+ def delete(key)
+ @cache.delete(key)
+ end
+ end
+end
+
+ActiveSupport::WeakHash = ::Weakling::WeakHash if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'jruby'