]> git.saurik.com Git - apple/javascriptcore.git/blob - runtime/StructureIDTable.cpp
JavaScriptCore-7600.1.4.16.1.tar.gz
[apple/javascriptcore.git] / runtime / StructureIDTable.cpp
1 /*
2 * Copyright (C) 2013 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 INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "StructureIDTable.h"
28
29 #include <limits.h>
30 #include <wtf/Atomics.h>
31 #include <wtf/DataLog.h>
32 #include <wtf/PassOwnPtr.h>
33
34 namespace JSC {
35
36 StructureIDTable::StructureIDTable()
37 : m_firstFreeOffset(0)
38 , m_table(adoptPtr(new StructureOrOffset[s_initialSize]))
39 , m_size(0)
40 , m_capacity(s_initialSize)
41 {
42 // We pre-allocate the first offset so that the null Structure
43 // can still be represented as the StructureID '0'.
44 allocateID(0);
45 }
46
47 void StructureIDTable::resize(size_t newCapacity)
48 {
49 // Create the new table.
50 OwnPtr<StructureOrOffset> newTable = adoptPtr(new StructureOrOffset[newCapacity]);
51
52 // Copy the contents of the old table to the new table.
53 memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset));
54
55 // Store fence to make sure we've copied everything before doing the swap.
56 WTF::storeStoreFence();
57
58 // Swap the old and new tables.
59 swap(m_table, newTable);
60
61 // Put the old table (now labeled as new) into the list of old tables.
62 m_oldTables.append(newTable.release());
63
64 // Update the capacity.
65 m_capacity = newCapacity;
66 }
67
68 void StructureIDTable::flushOldTables()
69 {
70 m_oldTables.clear();
71 }
72
73 StructureID StructureIDTable::allocateID(Structure* structure)
74 {
75 #if USE(JSVALUE64)
76 if (!m_firstFreeOffset) {
77 RELEASE_ASSERT(m_capacity <= UINT_MAX);
78 if (m_size == m_capacity)
79 resize(m_capacity * 2);
80 ASSERT(m_size < m_capacity);
81
82 StructureOrOffset newEntry;
83 newEntry.structure = structure;
84
85 if (m_size == s_unusedID) {
86 m_size++;
87 return allocateID(structure);
88 }
89
90 StructureID result = m_size;
91 table()[result] = newEntry;
92 m_size++;
93 return result;
94 }
95
96 ASSERT(m_firstFreeOffset != s_unusedID);
97
98 StructureID result = m_firstFreeOffset;
99 m_firstFreeOffset = table()[m_firstFreeOffset].offset;
100 table()[result].structure = structure;
101 return result;
102 #else
103 return structure;
104 #endif
105 }
106
107 void StructureIDTable::deallocateID(Structure* structure, StructureID structureID)
108 {
109 #if USE(JSVALUE64)
110 ASSERT(structureID != s_unusedID);
111 RELEASE_ASSERT(table()[structureID].structure == structure);
112 table()[structureID].offset = m_firstFreeOffset;
113 m_firstFreeOffset = structureID;
114 #else
115 UNUSED_PARAM(structure);
116 UNUSED_PARAM(structureID);
117 #endif
118 }
119
120 } // namespace JSC