source: webkit/trunk/JavaScriptCore/runtime/StructureTransitionTable.h@ 48331

Last change on this file since 48331 was 48265, checked in by [email protected], 16 years ago

Windows build fix.

Make StructureTransitionTable use an enum for the PtrAndFlags member
used for the single transition slot optimisation.

File size: 6.8 KB
Line 
1/*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
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
26#ifndef StructureTransitionTable_h
27#define StructureTransitionTable_h
28
29#include "UString.h"
30#include <wtf/HashFunctions.h>
31#include <wtf/HashMap.h>
32#include <wtf/HashTraits.h>
33#include <wtf/PtrAndFlags.h>
34#include <wtf/RefPtr.h>
35
36namespace JSC {
37
38 class Structure;
39
40 struct StructureTransitionTableHash {
41 typedef std::pair<RefPtr<UString::Rep>, unsigned> Key;
42 static unsigned hash(const Key& p)
43 {
44 return p.first->computedHash();
45 }
46
47 static bool equal(const Key& a, const Key& b)
48 {
49 return a == b;
50 }
51
52 static const bool safeToCompareToEmptyOrDeleted = true;
53 };
54
55 struct StructureTransitionTableHashTraits {
56 typedef WTF::HashTraits<RefPtr<UString::Rep> > FirstTraits;
57 typedef WTF::GenericHashTraits<unsigned> SecondTraits;
58 typedef std::pair<FirstTraits::TraitType, SecondTraits::TraitType > TraitType;
59
60 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
61 static TraitType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
62
63 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
64
65 static void constructDeletedValue(TraitType& slot) { FirstTraits::constructDeletedValue(slot.first); }
66 static bool isDeletedValue(const TraitType& value) { return FirstTraits::isDeletedValue(value.first); }
67 };
68
69 class StructureTransitionTable {
70 typedef std::pair<Structure*, Structure*> Transition;
71 typedef HashMap<StructureTransitionTableHash::Key, Transition, StructureTransitionTableHash, StructureTransitionTableHashTraits> TransitionTable;
72 public:
73 StructureTransitionTable() {
74 m_transitions.m_singleTransition.set(0);
75 m_transitions.m_singleTransition.setFlag(usingSingleSlot);
76 }
77
78 ~StructureTransitionTable() {
79 if (!usingSingleTransitionSlot())
80 delete table();
81 }
82
83 // The contains and get methods accept imprecise matches, so if an unspecialised transition exists
84 // for the given key they will consider that transition to be a match. If a specialised transition
85 // exists and it matches the provided specificValue, get will return the specific transition.
86 inline bool contains(const StructureTransitionTableHash::Key&, JSCell* specificValue);
87 inline Structure* get(const StructureTransitionTableHash::Key&, JSCell* specificValue) const;
88 inline bool hasTransition(const StructureTransitionTableHash::Key& key) const;
89 void remove(const StructureTransitionTableHash::Key& key, JSCell* specificValue)
90 {
91 if (usingSingleTransitionSlot()) {
92 ASSERT(contains(key, specificValue));
93 setSingleTransition(0);
94 return;
95 }
96 TransitionTable::iterator find = table()->find(key);
97 if (!specificValue)
98 find->second.first = 0;
99 else
100 find->second.second = 0;
101 if (!find->second.first && !find->second.second)
102 table()->remove(find);
103 }
104 void add(const StructureTransitionTableHash::Key& key, Structure* structure, JSCell* specificValue)
105 {
106 if (usingSingleTransitionSlot()) {
107 if (!singleTransition()) {
108 setSingleTransition(structure);
109 return;
110 }
111 reifySingleTransition();
112 }
113 if (!specificValue) {
114 TransitionTable::iterator find = table()->find(key);
115 if (find == table()->end())
116 table()->add(key, Transition(structure, 0));
117 else
118 find->second.first = structure;
119 } else {
120 // If we're adding a transition to a specific value, then there cannot be
121 // an existing transition
122 ASSERT(!table()->contains(key));
123 table()->add(key, Transition(0, structure));
124 }
125 }
126 private:
127 TransitionTable* table() const { ASSERT(!usingSingleTransitionSlot()); return m_transitions.m_table; }
128 Structure* singleTransition() const {
129 ASSERT(usingSingleTransitionSlot());
130 return m_transitions.m_singleTransition.get();
131 }
132 bool usingSingleTransitionSlot() const { return m_transitions.m_singleTransition.isFlagSet(usingSingleSlot); }
133 void setSingleTransition(Structure* structure)
134 {
135 ASSERT(usingSingleTransitionSlot());
136 m_transitions.m_singleTransition.set(structure);
137 }
138
139 void setTransitionTable(TransitionTable* table)
140 {
141 ASSERT(usingSingleTransitionSlot());
142#ifndef NDEBUG
143 setSingleTransition(0);
144#endif
145 m_transitions.m_table = table;
146 // This implicitly clears the flag that indicates we're using a single transition
147 ASSERT(!usingSingleTransitionSlot());
148 }
149 inline void reifySingleTransition();
150
151 enum UsingSingleSlot {
152 usingSingleSlot
153 };
154 // Last bit indicates whether we are using the single transition optimisation
155 union {
156 TransitionTable* m_table;
157 PtrAndFlagsBase<Structure, UsingSingleSlot> m_singleTransition;
158 } m_transitions;
159 };
160
161} // namespace JSC
162
163#endif // StructureTransitionTable_h
Note: See TracBrowser for help on using the repository browser.