blob: 7c9d50894e3ec9f2ea726648baac32be30c3a6aa [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
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000029#include "WeakGCMap.h"
weinig@apple.com8059db02008-10-29 23:52:52 +000030#include <wtf/HashFunctions.h>
oliver@apple.com4d4086d2009-09-15 23:17:19 +000031#include <wtf/OwnPtr.h>
weinig@apple.com8059db02008-10-29 23:52:52 +000032#include <wtf/RefPtr.h>
benjamin@webkit.orgcff06e42012-08-30 21:23:51 +000033#include <wtf/text/StringImpl.h>
weinig@apple.com8059db02008-10-29 23:52:52 +000034
35namespace JSC {
36
ggaren@apple.com76215812012-04-04 05:28:13 +000037class JSCell;
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
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000058 struct WeakGCMapFinalizerCallback {
59 static void* finalizerContextFor(Hash::Key)
60 {
61 return 0;
62 }
63
64 static inline Hash::Key keyForFinalizer(void* context, Structure* structure)
65 {
66 return keyForWeakGCMapFinalizer(context, structure);
67 }
68 };
69
enne@google.com991c9232012-02-10 19:36:38 +000070 typedef WeakGCMap<Hash::Key, Structure, WeakGCMapFinalizerCallback, Hash> TransitionMap;
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000071
72 static Hash::Key keyForWeakGCMapFinalizer(void* context, Structure*);
paroga@webkit.org4102fb02011-04-14 09:27:47 +000073
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000074public:
75 StructureTransitionTable()
76 : m_data(UsingSingleSlotFlag)
77 {
78 }
79
80 ~StructureTransitionTable()
81 {
ggaren@apple.com15ba84d2011-08-11 01:08:42 +000082 if (!isUsingSingleSlot()) {
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000083 delete map();
ggaren@apple.com15ba84d2011-08-11 01:08:42 +000084 return;
85 }
86
ggaren@apple.com76215812012-04-04 05:28:13 +000087 WeakImpl* impl = this->weakImpl();
88 if (!impl)
ggaren@apple.com15ba84d2011-08-11 01:08:42 +000089 return;
ggaren@apple.com61b97002012-04-06 22:35:55 +000090 WeakSet::deallocate(impl);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000091 }
92
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +000093 inline void add(JSGlobalData&, Structure*);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +000094 inline bool contains(StringImpl* rep, unsigned attributes) const;
95 inline Structure* get(StringImpl* rep, unsigned attributes) const;
96
97private:
98 bool isUsingSingleSlot() const
99 {
100 return m_data & UsingSingleSlotFlag;
101 }
102
103 TransitionMap* map() const
104 {
105 ASSERT(!isUsingSingleSlot());
106 return reinterpret_cast<TransitionMap*>(m_data);
107 }
108
ggaren@apple.com76215812012-04-04 05:28:13 +0000109 WeakImpl* weakImpl() const
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000110 {
111 ASSERT(isUsingSingleSlot());
ggaren@apple.com76215812012-04-04 05:28:13 +0000112 return reinterpret_cast<WeakImpl*>(m_data & ~UsingSingleSlotFlag);
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000113 }
114
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000115 void setMap(TransitionMap* map)
116 {
117 ASSERT(isUsingSingleSlot());
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000118
ggaren@apple.com76215812012-04-04 05:28:13 +0000119 if (WeakImpl* impl = this->weakImpl())
ggaren@apple.com61b97002012-04-06 22:35:55 +0000120 WeakSet::deallocate(impl);
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000121
122 // This implicitly clears the flag that indicates we're using a single transition
123 m_data = reinterpret_cast<intptr_t>(map);
124
125 ASSERT(!isUsingSingleSlot());
126 }
127
128 Structure* singleTransition() const
129 {
130 ASSERT(isUsingSingleSlot());
ggaren@apple.com76215812012-04-04 05:28:13 +0000131 if (WeakImpl* impl = this->weakImpl()) {
132 if (impl->state() == WeakImpl::Live)
133 return reinterpret_cast<Structure*>(impl->jsValue().asCell());
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000134 }
135 return 0;
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000136 }
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000137
ggaren@apple.comab71e972012-04-28 05:57:46 +0000138 void setSingleTransition(JSGlobalData&, Structure* structure)
oliver@apple.comb2fa0dc2011-04-15 23:55:42 +0000139 {
140 ASSERT(isUsingSingleSlot());
ggaren@apple.com76215812012-04-04 05:28:13 +0000141 if (WeakImpl* impl = this->weakImpl())
ggaren@apple.com61b97002012-04-06 22:35:55 +0000142 WeakSet::deallocate(impl);
ggaren@apple.comab71e972012-04-28 05:57:46 +0000143 WeakImpl* impl = WeakSet::allocate(reinterpret_cast<JSCell*>(structure));
ggaren@apple.com76215812012-04-04 05:28:13 +0000144 m_data = reinterpret_cast<intptr_t>(impl) | UsingSingleSlotFlag;
barraclough@apple.com07be2aa2011-02-22 21:49:59 +0000145 }
146
147 intptr_t m_data;
148};
149
weinig@apple.com8059db02008-10-29 23:52:52 +0000150} // namespace JSC
151
darin@apple.coma9778f92008-11-16 04:40:06 +0000152#endif // StructureTransitionTable_h