diff options
author | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
---|---|---|
committer | Lorry Tar Creator <lorry-tar-importer@lorry> | 2017-06-27 06:07:23 +0000 |
commit | 1bf1084f2b10c3b47fd1a588d85d21ed0eb41d0c (patch) | |
tree | 46dcd36c86e7fbc6e5df36deb463b33e9967a6f7 /Source/JavaScriptCore/runtime/WeakGCMap.h | |
parent | 32761a6cee1d0dee366b885b7b9c777e67885688 (diff) | |
download | WebKitGtk-tarball-master.tar.gz |
webkitgtk-2.16.5HEADwebkitgtk-2.16.5master
Diffstat (limited to 'Source/JavaScriptCore/runtime/WeakGCMap.h')
-rw-r--r-- | Source/JavaScriptCore/runtime/WeakGCMap.h | 85 |
1 files changed, 25 insertions, 60 deletions
diff --git a/Source/JavaScriptCore/runtime/WeakGCMap.h b/Source/JavaScriptCore/runtime/WeakGCMap.h index d9c1fb63e..6f99fc0cf 100644 --- a/Source/JavaScriptCore/runtime/WeakGCMap.h +++ b/Source/JavaScriptCore/runtime/WeakGCMap.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2009 Apple Inc. All rights reserved. + * Copyright (C) 2009, 2015-2016 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -23,11 +23,9 @@ * THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef WeakGCMap_h -#define WeakGCMap_h +#pragma once #include "Weak.h" -#include "WeakInlines.h" #include <wtf/HashMap.h> namespace JSC { @@ -37,6 +35,7 @@ namespace JSC { template<typename KeyArg, typename ValueArg, typename HashArg = typename DefaultHash<KeyArg>::Hash, typename KeyTraitsArg = HashTraits<KeyArg>> class WeakGCMap { + WTF_MAKE_FAST_ALLOCATED; typedef Weak<ValueArg> ValueType; typedef HashMap<KeyArg, ValueType, HashArg, KeyTraitsArg> HashMapType; @@ -46,10 +45,8 @@ public: typedef typename HashMapType::iterator iterator; typedef typename HashMapType::const_iterator const_iterator; - WeakGCMap() - : m_gcThreshold(minGCThreshold) - { - } + explicit WeakGCMap(VM&); + ~WeakGCMap(); ValueArg* get(const KeyType& key) const { @@ -58,19 +55,7 @@ public: AddResult set(const KeyType& key, ValueType value) { - gcMapIfNeeded(); - return m_map.set(key, std::move(value)); - } - - AddResult add(const KeyType& key, ValueType value) - { - gcMapIfNeeded(); - AddResult addResult = m_map.add(key, nullptr); - if (!addResult.iterator->value) { // New value or found a zombie value. - addResult.isNewEntry = true; - addResult.iterator->value = std::move(value); - } - return addResult; + return m_map.set(key, WTFMove(value)); } bool remove(const KeyType& key) @@ -83,57 +68,37 @@ public: m_map.clear(); } - iterator find(const KeyType& key) + bool isEmpty() const { - iterator it = m_map.find(key); - iterator end = m_map.end(); - if (it != end && !it->value) // Found a zombie value. - return end; - return it; - } - - const_iterator find(const KeyType& key) const - { - return const_cast<WeakGCMap*>(this)->find(key); + const_iterator it = m_map.begin(); + const_iterator end = m_map.end(); + while (it != end) { + if (it->value) + return true; + } + return false; } - bool contains(const KeyType& key) const - { - return find(key) != m_map.end(); - } + inline iterator find(const KeyType& key); -private: - static const int minGCThreshold = 3; + inline const_iterator find(const KeyType& key) const; - void gcMap() + template<typename Functor> + void forEach(Functor functor) { - Vector<KeyType, 4> zombies; - - for (iterator it = m_map.begin(), end = m_map.end(); it != end; ++it) { - if (!it->value) - zombies.append(it->key); + for (auto& pair : m_map) { + if (pair.value) + functor(pair.key, pair.value.get()); } - - for (size_t i = 0; i < zombies.size(); ++i) - m_map.remove(zombies[i]); } - void gcMapIfNeeded() - { - if (m_map.size() < m_gcThreshold) - return; + inline bool contains(const KeyType& key) const; - gcMap(); - m_gcThreshold = std::max(minGCThreshold, m_map.size() * 2 - 1); - } + void pruneStaleEntries(); +private: HashMapType m_map; - int m_gcThreshold; + VM& m_vm; }; -template<typename KeyArg, typename RawMappedArg, typename HashArg, typename KeyTraitsArg> -const int WeakGCMap<KeyArg, RawMappedArg, HashArg, KeyTraitsArg>::minGCThreshold; - } // namespace JSC - -#endif // WeakGCMap_h |