blob: c1041283e5f1402004dcc2e4254c22fda4323e91 [file] [log] [blame]
weinig@apple.com8059db02008-10-29 23:52:52 +00001/*
oliver@apple.com9bf9e0f2009-08-20 22:36:36 +00002 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
weinig@apple.com8059db02008-10-29 23:52:52 +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 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.coma9778f92008-11-16 04:40:06 +000026#ifndef StructureTransitionTable_h
27#define StructureTransitionTable_h
weinig@apple.com8059db02008-10-29 23:52:52 +000028
cwzwarich@webkit.org0b51a732008-11-05 23:21:32 +000029#include "UString.h"
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000030#include "WeakGCMap.h"
weinig@apple.com8059db02008-10-29 23:52:52 +000031#include <wtf/HashFunctions.h>
weinig@apple.com8059db02008-10-29 23:52:52 +000032#include <wtf/HashTraits.h>
oliver@apple.com4d4086d2009-09-15 23:17:19 +000033#include <wtf/OwnPtr.h>
weinig@apple.com8059db02008-10-29 23:52:52 +000034#include <wtf/RefPtr.h>
35
36namespace JSC {
37
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000038class Structure;
weinig@apple.com8059db02008-10-29 23:52:52 +000039
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000040class StructureTransitionTable {
41 static const intptr_t UsingSingleSlotFlag = 1;
42
43 struct Hash {
barraclough@apple.comee2085b2010-08-11 00:16:38 +000044 typedef std::pair<RefPtr<StringImpl>, unsigned> Key;
weinig@apple.com8059db02008-10-29 23:52:52 +000045 static unsigned hash(const Key& p)
46 {
barraclough@apple.comb150b882010-01-15 06:43:21 +000047 return p.first->existingHash();
weinig@apple.com8059db02008-10-29 23:52:52 +000048 }
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.com07be2aa2011-02-22 21:49:59 +000058 struct HashTraits {
barraclough@apple.comee2085b2010-08-11 00:16:38 +000059 typedef WTF::HashTraits<RefPtr<StringImpl> > FirstTraits;
oliver@apple.com9bf9e0f2009-08-20 22:36:36 +000060 typedef WTF::GenericHashTraits<unsigned> SecondTraits;
61 typedef std::pair<FirstTraits::TraitType, SecondTraits::TraitType > TraitType;
weinig@apple.com8059db02008-10-29 23:52:52 +000062
oliver@apple.com9bf9e0f2009-08-20 22:36:36 +000063 static const bool emptyValueIsZero = FirstTraits::emptyValueIsZero && SecondTraits::emptyValueIsZero;
64 static TraitType emptyValue() { return std::make_pair(FirstTraits::emptyValue(), SecondTraits::emptyValue()); }
weinig@apple.com8059db02008-10-29 23:52:52 +000065
oliver@apple.com9bf9e0f2009-08-20 22:36:36 +000066 static const bool needsDestruction = FirstTraits::needsDestruction || SecondTraits::needsDestruction;
weinig@apple.com8059db02008-10-29 23:52:52 +000067
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.comb2fa0dc2011-04-15 23:55:42 +000072 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.org4102fb02011-04-14 09:27:47 +000087
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000088public:
89 StructureTransitionTable()
90 : m_data(UsingSingleSlotFlag)
91 {
92 }
93
94 ~StructureTransitionTable()
95 {
ggaren@apple.com15ba84d2011-08-11 01:08:42 +000096 if (!isUsingSingleSlot()) {
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000097 delete map();
ggaren@apple.com15ba84d2011-08-11 01:08:42 +000098 return;
99 }
100
101 HandleSlot slot = this->slot();
102 if (!slot)
103 return;
104 HandleHeap::heapFor(slot)->deallocate(slot);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000105 }
106
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000107 inline void add(JSGlobalData&, Structure*);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000108 inline bool contains(StringImpl* rep, unsigned attributes) const;
109 inline Structure* get(StringImpl* rep, unsigned attributes) const;
110
111private:
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.comb2fa0dc2011-04-15 23:55:42 +0000123 HandleSlot slot() const
124 {
125 ASSERT(isUsingSingleSlot());
126 return reinterpret_cast<HandleSlot>(m_data & ~UsingSingleSlotFlag);
127 }
128
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000129 void setMap(TransitionMap* map)
130 {
131 ASSERT(isUsingSingleSlot());
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000132
133 if (HandleSlot slot = this->slot())
134 HandleHeap::heapFor(slot)->deallocate(slot);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000135
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.comb2fa0dc2011-04-15 23:55:42 +0000145 if (HandleSlot slot = this->slot()) {
commit-queue@webkit.org2dba4a42011-04-22 19:26:04 +0000146 if (*slot)
147 return reinterpret_cast<Structure*>(slot->asCell());
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000148 }
149 return 0;
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000150 }
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000151
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000152 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.org2dba4a42011-04-22 19:26:04 +0000162 *slot = reinterpret_cast<JSCell*>(structure);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000163 }
164
165 intptr_t m_data;
166};
167
weinig@apple.com8059db02008-10-29 23:52:52 +0000168} // namespace JSC
169
darin@apple.coma9778f92008-11-16 04:40:06 +0000170#endif // StructureTransitionTable_h