2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "JSGlobalData.h"
31 #include <wtf/HashMap.h>
35 // A HashMap for GC'd values that removes entries when the associated value
37 template <typename KeyType, typename MappedType> struct DefaultWeakGCMapFinalizerCallback {
38 static void* finalizerContextFor(KeyType key)
40 return reinterpret_cast<void*>(key);
43 static KeyType keyForFinalizer(void* context, typename HandleTypes<MappedType>::ExternalType)
45 return reinterpret_cast<KeyType>(context);
49 template<typename KeyType, typename MappedType, typename FinalizerCallback = DefaultWeakGCMapFinalizerCallback<KeyType, MappedType>, typename HashArg = typename DefaultHash<KeyType>::Hash, typename KeyTraitsArg = HashTraits<KeyType> >
50 class WeakGCMap : private WeakHandleOwner {
51 WTF_MAKE_FAST_ALLOCATED;
52 WTF_MAKE_NONCOPYABLE(WeakGCMap);
54 typedef HashMap<KeyType, WeakImpl*, HashArg, KeyTraitsArg> MapType;
55 typedef typename HandleTypes<MappedType>::ExternalType ExternalType;
64 typename MapType::iterator end = m_map.end();
65 for (typename MapType::iterator ptr = m_map.begin(); ptr != end; ++ptr)
66 WeakSet::deallocate(ptr->second);
70 ExternalType get(const KeyType& key) const
72 WeakImpl* impl = m_map.get(key);
73 if (!impl || impl->state() != WeakImpl::Live)
74 return ExternalType();
75 return HandleTypes<MappedType>::getFromSlot(const_cast<JSValue*>(&impl->jsValue()));
78 void set(JSGlobalData&, const KeyType& key, ExternalType value)
80 typename MapType::AddResult result = m_map.add(key, 0);
81 if (!result.isNewEntry)
82 WeakSet::deallocate(result.iterator->second);
83 result.iterator->second = WeakSet::allocate(value, this, FinalizerCallback::finalizerContextFor(key));
86 void remove(const KeyType& key)
88 WeakImpl* impl = m_map.take(key);
91 WeakSet::deallocate(impl);
100 virtual void finalize(Handle<Unknown> handle, void* context)
102 WeakImpl* impl = m_map.take(FinalizerCallback::keyForFinalizer(context, HandleTypes<MappedType>::getFromSlot(handle.slot())));
104 WeakSet::deallocate(impl);
112 #endif // WeakGCMap_h