weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 1 | /* |
oliver@apple.com | 9bf9e0f | 2009-08-20 22:36:36 +0000 | [diff] [blame] | 2 | * Copyright (C) 2008, 2009 Apple Inc. All rights reserved. |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +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 COMPUTER, INC. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
darin@apple.com | a9778f9 | 2008-11-16 04:40:06 +0000 | [diff] [blame] | 26 | #ifndef StructureTransitionTable_h |
| 27 | #define StructureTransitionTable_h |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 28 | |
cwzwarich@webkit.org | 0b51a73 | 2008-11-05 23:21:32 +0000 | [diff] [blame] | 29 | #include "UString.h" |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 30 | #include "WeakGCMap.h" |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 31 | #include <wtf/HashFunctions.h> |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 32 | #include <wtf/HashTraits.h> |
oliver@apple.com | 4d4086d | 2009-09-15 23:17:19 +0000 | [diff] [blame] | 33 | #include <wtf/OwnPtr.h> |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 34 | #include <wtf/RefPtr.h> |
| 35 | |
| 36 | namespace JSC { |
| 37 | |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 38 | class Structure; |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 39 | |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 40 | class StructureTransitionTable { |
| 41 | static const intptr_t UsingSingleSlotFlag = 1; |
| 42 | |
| 43 | struct Hash { |
barraclough@apple.com | ee2085b | 2010-08-11 00:16:38 +0000 | [diff] [blame] | 44 | typedef std::pair<RefPtr<StringImpl>, unsigned> Key; |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 45 | static unsigned hash(const Key& p) |
| 46 | { |
barraclough@apple.com | b150b88 | 2010-01-15 06:43:21 +0000 | [diff] [blame] | 47 | return p.first->existingHash(); |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static bool equal(const Key& a, const Key& b) |
| 51 | { |
| 52 | return a == b; |
| 53 | } |
| 54 | |
| 55 | static const bool safeToCompareToEmptyOrDeleted = true; |
| 56 | }; |
| 57 | |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 58 | struct HashTraits { |
barraclough@apple.com | ee2085b | 2010-08-11 00:16:38 +0000 | [diff] [blame] | 59 | typedef WTF::HashTraits<RefPtr<StringImpl> > FirstTraits; |
oliver@apple.com | 9bf9e0f | 2009-08-20 22:36:36 +0000 | [diff] [blame] | 60 | typedef WTF::GenericHashTraits<unsigned> SecondTraits; |
| 61 | typedef std::pair<FirstTraits::TraitType, SecondTraits::TraitType > TraitType; |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 62 | |
oliver@apple.com | 9bf9e0f | 2009-08-20 22:36:36 +0000 | [diff] [blame] | 63 | static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero; |
| 64 | static TraitType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); } |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 65 | |
oliver@apple.com | 9bf9e0f | 2009-08-20 22:36:36 +0000 | [diff] [blame] | 66 | static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction; |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 67 | |
| 68 | static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); } |
| 69 | static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); } |
| 70 | }; |
| 71 | |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 72 | struct WeakGCMapFinalizerCallback { |
| 73 | static void* finalizerContextFor(Hash::Key) |
| 74 | { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static inline Hash::Key keyForFinalizer(void* context, Structure* structure) |
| 79 | { |
| 80 | return keyForWeakGCMapFinalizer(context, structure); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | typedef WeakGCMap<Hash::Key, Structure, WeakGCMapFinalizerCallback, Hash, HashTraits> TransitionMap; |
| 85 | |
| 86 | static Hash::Key keyForWeakGCMapFinalizer(void* context, Structure*); |
paroga@webkit.org | 4102fb0 | 2011-04-14 09:27:47 +0000 | [diff] [blame] | 87 | |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 88 | public: |
| 89 | StructureTransitionTable() |
| 90 | : m_data(UsingSingleSlotFlag) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | ~StructureTransitionTable() |
| 95 | { |
ggaren@apple.com | 15ba84d | 2011-08-11 01:08:42 +0000 | [diff] [blame^] | 96 | if (!isUsingSingleSlot()) { |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 97 | delete map(); |
ggaren@apple.com | 15ba84d | 2011-08-11 01:08:42 +0000 | [diff] [blame^] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | HandleSlot slot = this->slot(); |
| 102 | if (!slot) |
| 103 | return; |
| 104 | HandleHeap::heapFor(slot)->deallocate(slot); |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 105 | } |
| 106 | |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 107 | inline void add(JSGlobalData&, Structure*); |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 108 | inline bool contains(StringImpl* rep, unsigned attributes) const; |
| 109 | inline Structure* get(StringImpl* rep, unsigned attributes) const; |
| 110 | |
| 111 | private: |
| 112 | bool isUsingSingleSlot() const |
| 113 | { |
| 114 | return m_data & UsingSingleSlotFlag; |
| 115 | } |
| 116 | |
| 117 | TransitionMap* map() const |
| 118 | { |
| 119 | ASSERT(!isUsingSingleSlot()); |
| 120 | return reinterpret_cast<TransitionMap*>(m_data); |
| 121 | } |
| 122 | |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 123 | HandleSlot slot() const |
| 124 | { |
| 125 | ASSERT(isUsingSingleSlot()); |
| 126 | return reinterpret_cast<HandleSlot>(m_data & ~UsingSingleSlotFlag); |
| 127 | } |
| 128 | |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 129 | void setMap(TransitionMap* map) |
| 130 | { |
| 131 | ASSERT(isUsingSingleSlot()); |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 132 | |
| 133 | if (HandleSlot slot = this->slot()) |
| 134 | HandleHeap::heapFor(slot)->deallocate(slot); |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 135 | |
| 136 | // This implicitly clears the flag that indicates we're using a single transition |
| 137 | m_data = reinterpret_cast<intptr_t>(map); |
| 138 | |
| 139 | ASSERT(!isUsingSingleSlot()); |
| 140 | } |
| 141 | |
| 142 | Structure* singleTransition() const |
| 143 | { |
| 144 | ASSERT(isUsingSingleSlot()); |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 145 | if (HandleSlot slot = this->slot()) { |
commit-queue@webkit.org | 2dba4a4 | 2011-04-22 19:26:04 +0000 | [diff] [blame] | 146 | if (*slot) |
| 147 | return reinterpret_cast<Structure*>(slot->asCell()); |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 148 | } |
| 149 | return 0; |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 150 | } |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 151 | |
oliver@apple.com | b2fa0dc | 2011-04-15 23:55:42 +0000 | [diff] [blame] | 152 | void setSingleTransition(JSGlobalData& globalData, Structure* structure) |
| 153 | { |
| 154 | ASSERT(isUsingSingleSlot()); |
| 155 | HandleSlot slot = this->slot(); |
| 156 | if (!slot) { |
| 157 | slot = globalData.allocateGlobalHandle(); |
| 158 | HandleHeap::heapFor(slot)->makeWeak(slot, 0, 0); |
| 159 | m_data = reinterpret_cast<intptr_t>(slot) | UsingSingleSlotFlag; |
| 160 | } |
| 161 | HandleHeap::heapFor(slot)->writeBarrier(slot, reinterpret_cast<JSCell*>(structure)); |
commit-queue@webkit.org | 2dba4a4 | 2011-04-22 19:26:04 +0000 | [diff] [blame] | 162 | *slot = reinterpret_cast<JSCell*>(structure); |
barraclough@apple.com | 07be2aa | 2011-02-22 21:49:59 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | intptr_t m_data; |
| 166 | }; |
| 167 | |
weinig@apple.com | 8059db0 | 2008-10-29 23:52:52 +0000 | [diff] [blame] | 168 | } // namespace JSC |
| 169 | |
darin@apple.com | a9778f9 | 2008-11-16 04:40:06 +0000 | [diff] [blame] | 170 | #endif // StructureTransitionTable_h |