]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/StructureIDTable.cpp
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / StructureIDTable.cpp
CommitLineData
81345200
A
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>
81345200
A
32
33namespace JSC {
34
35StructureIDTable::StructureIDTable()
36 : m_firstFreeOffset(0)
ed1e77d3 37 , m_table(std::make_unique<StructureOrOffset[]>(s_initialSize))
81345200
A
38 , m_size(0)
39 , m_capacity(s_initialSize)
40{
41 // We pre-allocate the first offset so that the null Structure
42 // can still be represented as the StructureID '0'.
43 allocateID(0);
44}
45
46void StructureIDTable::resize(size_t newCapacity)
47{
48 // Create the new table.
ed1e77d3 49 auto newTable = std::make_unique<StructureOrOffset[]>(newCapacity);
81345200
A
50
51 // Copy the contents of the old table to the new table.
52 memcpy(newTable.get(), table(), m_capacity * sizeof(StructureOrOffset));
53
54 // Store fence to make sure we've copied everything before doing the swap.
55 WTF::storeStoreFence();
56
57 // Swap the old and new tables.
58 swap(m_table, newTable);
59
60 // Put the old table (now labeled as new) into the list of old tables.
ed1e77d3 61 m_oldTables.append(WTF::move(newTable));
81345200
A
62
63 // Update the capacity.
64 m_capacity = newCapacity;
65}
66
67void StructureIDTable::flushOldTables()
68{
69 m_oldTables.clear();
70}
71
72StructureID StructureIDTable::allocateID(Structure* structure)
73{
74#if USE(JSVALUE64)
75 if (!m_firstFreeOffset) {
76 RELEASE_ASSERT(m_capacity <= UINT_MAX);
77 if (m_size == m_capacity)
78 resize(m_capacity * 2);
79 ASSERT(m_size < m_capacity);
80
81 StructureOrOffset newEntry;
82 newEntry.structure = structure;
83
84 if (m_size == s_unusedID) {
85 m_size++;
86 return allocateID(structure);
87 }
88
89 StructureID result = m_size;
90 table()[result] = newEntry;
91 m_size++;
92 return result;
93 }
94
95 ASSERT(m_firstFreeOffset != s_unusedID);
96
97 StructureID result = m_firstFreeOffset;
98 m_firstFreeOffset = table()[m_firstFreeOffset].offset;
99 table()[result].structure = structure;
100 return result;
101#else
102 return structure;
103#endif
104}
105
106void StructureIDTable::deallocateID(Structure* structure, StructureID structureID)
107{
108#if USE(JSVALUE64)
109 ASSERT(structureID != s_unusedID);
110 RELEASE_ASSERT(table()[structureID].structure == structure);
111 table()[structureID].offset = m_firstFreeOffset;
112 m_firstFreeOffset = structureID;
113#else
114 UNUSED_PARAM(structure);
115 UNUSED_PARAM(structureID);
116#endif
117}
118
119} // namespace JSC