blob: 6f99fc0cf859380e5e2286795b49fb72534107e6 [file] [log] [blame]
ggaren@apple.com6a429812009-12-14 08:13:24 +00001/*
fpizlo@apple.com96352992016-09-20 18:12:18 +00002 * Copyright (C) 2009, 2015-2016 Apple Inc. All rights reserved.
ggaren@apple.com6a429812009-12-14 08:13:24 +00003 *
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.com22104f52016-09-28 17:08:17 +000026#pragma once
ggaren@apple.com6a429812009-12-14 08:13:24 +000027
ap@apple.com94163232013-10-20 00:01:17 +000028#include "Weak.h"
ggaren@apple.com6a429812009-12-14 08:13:24 +000029#include <wtf/HashMap.h>
30
31namespace JSC {
32
ggaren@apple.com7467f9b2013-01-18 20:38:10 +000033// A HashMap with Weak<JSCell> values, which automatically removes values once they're garbage collected.
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000034
andersca@apple.comc21b1342013-09-26 19:23:11 +000035template<typename KeyArg, typename ValueArg, typename HashArg = typename DefaultHash<KeyArg>::Hash,
andersca@apple.come4b2dd92013-09-26 18:00:06 +000036 typename KeyTraitsArg = HashTraits<KeyArg>>
37class WeakGCMap {
akling@apple.com36ae30d2014-04-25 18:15:48 +000038 WTF_MAKE_FAST_ALLOCATED;
andersca@apple.comc21b1342013-09-26 19:23:11 +000039 typedef Weak<ValueArg> ValueType;
40 typedef HashMap<KeyArg, ValueType, HashArg, KeyTraitsArg> HashMapType;
ggaren@apple.com6a429812009-12-14 08:13:24 +000041
42public:
andersca@apple.come4b2dd92013-09-26 18:00:06 +000043 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.com7467f9b2013-01-18 20:38:10 +000047
akling@apple.com926b1102015-03-10 00:09:39 +000048 explicit WeakGCMap(VM&);
49 ~WeakGCMap();
oliver@apple.com530e24c2011-02-24 21:59:57 +000050
andersca@apple.comc21b1342013-09-26 19:23:11 +000051 ValueArg* get(const KeyType& key) const
andersca@apple.come4b2dd92013-09-26 18:00:06 +000052 {
53 return m_map.get(key);
54 }
55
andersca@apple.comc21b1342013-09-26 19:23:11 +000056 AddResult set(const KeyType& key, ValueType value)
oliver@apple.com530e24c2011-02-24 21:59:57 +000057 {
aestes@apple.com13aae082016-01-02 08:03:08 +000058 return m_map.set(key, WTFMove(value));
oliver@apple.com530e24c2011-02-24 21:59:57 +000059 }
60
andersca@apple.come4b2dd92013-09-26 18:00:06 +000061 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.com2a514ce2015-03-25 00:19:05 +000071 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.com96352992016-09-20 18:12:18 +000082 inline iterator find(const KeyType& key);
oliver@apple.com530e24c2011-02-24 21:59:57 +000083
fpizlo@apple.com96352992016-09-20 18:12:18 +000084 inline const_iterator find(const KeyType& key) const;
oliver@apple.com530e24c2011-02-24 21:59:57 +000085
ggaren@apple.com2a514ce2015-03-25 00:19:05 +000086 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.com96352992016-09-20 18:12:18 +000095 inline bool contains(const KeyType& key) const;
ggaren@apple.com7467f9b2013-01-18 20:38:10 +000096
akling@apple.com926b1102015-03-10 00:09:39 +000097 void pruneStaleEntries();
98
ggaren@apple.com6a429812009-12-14 08:13:24 +000099private:
andersca@apple.come4b2dd92013-09-26 18:00:06 +0000100 HashMapType m_map;
akling@apple.com926b1102015-03-10 00:09:39 +0000101 VM& m_vm;
oliver@apple.com530e24c2011-02-24 21:59:57 +0000102};
ggaren@apple.com6a429812009-12-14 08:13:24 +0000103
ggaren@apple.com6a429812009-12-14 08:13:24 +0000104} // namespace JSC