ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 1 | /* |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 2 | * Copyright (C) 2009, 2015-2016 Apple Inc. All rights reserved. |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
ryanhaddad@apple.com | 22104f5 | 2016-09-28 17:08:17 +0000 | [diff] [blame] | 26 | #pragma once |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 27 | |
ap@apple.com | 9416323 | 2013-10-20 00:01:17 +0000 | [diff] [blame] | 28 | #include "Weak.h" |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 29 | #include <wtf/HashMap.h> |
| 30 | |
| 31 | namespace JSC { |
| 32 | |
ggaren@apple.com | 7467f9b | 2013-01-18 20:38:10 +0000 | [diff] [blame] | 33 | // A HashMap with Weak<JSCell> values, which automatically removes values once they're garbage collected. |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 34 | |
andersca@apple.com | c21b134 | 2013-09-26 19:23:11 +0000 | [diff] [blame] | 35 | template<typename KeyArg, typename ValueArg, typename HashArg = typename DefaultHash<KeyArg>::Hash, |
andersca@apple.com | e4b2dd9 | 2013-09-26 18:00:06 +0000 | [diff] [blame] | 36 | typename KeyTraitsArg = HashTraits<KeyArg>> |
| 37 | class WeakGCMap { |
akling@apple.com | 36ae30d | 2014-04-25 18:15:48 +0000 | [diff] [blame] | 38 | WTF_MAKE_FAST_ALLOCATED; |
andersca@apple.com | c21b134 | 2013-09-26 19:23:11 +0000 | [diff] [blame] | 39 | typedef Weak<ValueArg> ValueType; |
| 40 | typedef HashMap<KeyArg, ValueType, HashArg, KeyTraitsArg> HashMapType; |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 41 | |
| 42 | public: |
andersca@apple.com | e4b2dd9 | 2013-09-26 18:00:06 +0000 | [diff] [blame] | 43 | typedef typename HashMapType::KeyType KeyType; |
| 44 | typedef typename HashMapType::AddResult AddResult; |
| 45 | typedef typename HashMapType::iterator iterator; |
| 46 | typedef typename HashMapType::const_iterator const_iterator; |
ggaren@apple.com | 7467f9b | 2013-01-18 20:38:10 +0000 | [diff] [blame] | 47 | |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 48 | explicit WeakGCMap(VM&); |
| 49 | ~WeakGCMap(); |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 50 | |
andersca@apple.com | c21b134 | 2013-09-26 19:23:11 +0000 | [diff] [blame] | 51 | ValueArg* get(const KeyType& key) const |
andersca@apple.com | e4b2dd9 | 2013-09-26 18:00:06 +0000 | [diff] [blame] | 52 | { |
| 53 | return m_map.get(key); |
| 54 | } |
| 55 | |
andersca@apple.com | c21b134 | 2013-09-26 19:23:11 +0000 | [diff] [blame] | 56 | AddResult set(const KeyType& key, ValueType value) |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 57 | { |
aestes@apple.com | 13aae08 | 2016-01-02 08:03:08 +0000 | [diff] [blame] | 58 | return m_map.set(key, WTFMove(value)); |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 59 | } |
| 60 | |
andersca@apple.com | e4b2dd9 | 2013-09-26 18:00:06 +0000 | [diff] [blame] | 61 | bool remove(const KeyType& key) |
| 62 | { |
| 63 | return m_map.remove(key); |
| 64 | } |
| 65 | |
| 66 | void clear() |
| 67 | { |
| 68 | m_map.clear(); |
| 69 | } |
| 70 | |
ggaren@apple.com | 2a514ce | 2015-03-25 00:19:05 +0000 | [diff] [blame] | 71 | bool isEmpty() const |
| 72 | { |
| 73 | const_iterator it = m_map.begin(); |
| 74 | const_iterator end = m_map.end(); |
| 75 | while (it != end) { |
| 76 | if (it->value) |
| 77 | return true; |
| 78 | } |
| 79 | return false; |
| 80 | } |
| 81 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 82 | inline iterator find(const KeyType& key); |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 83 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 84 | inline const_iterator find(const KeyType& key) const; |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 85 | |
ggaren@apple.com | 2a514ce | 2015-03-25 00:19:05 +0000 | [diff] [blame] | 86 | template<typename Functor> |
| 87 | void forEach(Functor functor) |
| 88 | { |
| 89 | for (auto& pair : m_map) { |
| 90 | if (pair.value) |
| 91 | functor(pair.key, pair.value.get()); |
| 92 | } |
| 93 | } |
| 94 | |
fpizlo@apple.com | 9635299 | 2016-09-20 18:12:18 +0000 | [diff] [blame] | 95 | inline bool contains(const KeyType& key) const; |
ggaren@apple.com | 7467f9b | 2013-01-18 20:38:10 +0000 | [diff] [blame] | 96 | |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 97 | void pruneStaleEntries(); |
| 98 | |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 99 | private: |
andersca@apple.com | e4b2dd9 | 2013-09-26 18:00:06 +0000 | [diff] [blame] | 100 | HashMapType m_map; |
akling@apple.com | 926b110 | 2015-03-10 00:09:39 +0000 | [diff] [blame] | 101 | VM& m_vm; |
oliver@apple.com | 530e24c | 2011-02-24 21:59:57 +0000 | [diff] [blame] | 102 | }; |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 103 | |
ggaren@apple.com | 6a42981 | 2009-12-14 08:13:24 +0000 | [diff] [blame] | 104 | } // namespace JSC |