]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StructureTransitionTable.h
JavaScriptCore-1097.3.3.tar.gz
[apple/javascriptcore.git] / runtime / StructureTransitionTable.h
CommitLineData
9dae56ea 1/*
ba379fdc 2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
9dae56ea
A
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"
14957cd0 30#include "WeakGCMap.h"
9dae56ea 31#include <wtf/HashFunctions.h>
f9bf01c6 32#include <wtf/OwnPtr.h>
9dae56ea
A
33#include <wtf/RefPtr.h>
34
35namespace JSC {
36
6fe7ccc8 37class JSCell;
14957cd0 38class Structure;
9dae56ea 39
14957cd0
A
40class StructureTransitionTable {
41 static const intptr_t UsingSingleSlotFlag = 1;
42
43 struct Hash {
44 typedef std::pair<RefPtr<StringImpl>, unsigned> Key;
9dae56ea
A
45 static unsigned hash(const Key& p)
46 {
f9bf01c6 47 return p.first->existingHash();
9dae56ea
A
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
14957cd0
A
58 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
6fe7ccc8 70 typedef WeakGCMap<Hash::Key, Structure, WeakGCMapFinalizerCallback, Hash> TransitionMap;
14957cd0
A
71
72 static Hash::Key keyForWeakGCMapFinalizer(void* context, Structure*);
73
74public:
75 StructureTransitionTable()
76 : m_data(UsingSingleSlotFlag)
77 {
78 }
79
80 ~StructureTransitionTable()
81 {
6fe7ccc8 82 if (!isUsingSingleSlot()) {
14957cd0 83 delete map();
6fe7ccc8
A
84 return;
85 }
86
87 WeakImpl* impl = this->weakImpl();
88 if (!impl)
89 return;
90 WeakSet::deallocate(impl);
14957cd0
A
91 }
92
93 inline void add(JSGlobalData&, Structure*);
14957cd0
A
94 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
6fe7ccc8 109 WeakImpl* weakImpl() const
14957cd0
A
110 {
111 ASSERT(isUsingSingleSlot());
6fe7ccc8 112 return reinterpret_cast<WeakImpl*>(m_data & ~UsingSingleSlotFlag);
14957cd0
A
113 }
114
115 void setMap(TransitionMap* map)
116 {
117 ASSERT(isUsingSingleSlot());
118
6fe7ccc8
A
119 if (WeakImpl* impl = this->weakImpl())
120 WeakSet::deallocate(impl);
14957cd0
A
121
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());
6fe7ccc8
A
131 if (WeakImpl* impl = this->weakImpl()) {
132 if (impl->state() == WeakImpl::Live)
133 return reinterpret_cast<Structure*>(impl->jsValue().asCell());
14957cd0
A
134 }
135 return 0;
136 }
137
6fe7ccc8 138 void setSingleTransition(JSGlobalData&, Structure* structure)
14957cd0
A
139 {
140 ASSERT(isUsingSingleSlot());
6fe7ccc8
A
141 if (WeakImpl* impl = this->weakImpl())
142 WeakSet::deallocate(impl);
143 WeakImpl* impl = WeakSet::allocate(reinterpret_cast<JSCell*>(structure));
144 m_data = reinterpret_cast<intptr_t>(impl) | UsingSingleSlotFlag;
14957cd0
A
145 }
146
147 intptr_t m_data;
148};
149
9dae56ea
A
150} // namespace JSC
151
152#endif // StructureTransitionTable_h