1 | /* |
2 | * Copyright (C) 2018 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. ``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 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 | #pragma once |
27 | |
28 | #include "MetadataTable.h" |
29 | #include "UnlinkedMetadataTable.h" |
30 | #include <wtf/FastMalloc.h> |
31 | |
32 | namespace JSC { |
33 | |
34 | ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable() |
35 | : m_hasMetadata(false) |
36 | , m_isFinalized(false) |
37 | , m_isLinked(false) |
38 | , m_is32Bit(false) |
39 | , m_rawBuffer(static_cast<uint8_t*>(fastZeroedMalloc(sizeof(LinkingData) + s_offset32TableSize))) |
40 | { |
41 | } |
42 | |
43 | ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable(bool is32Bit) |
44 | : m_hasMetadata(false) |
45 | , m_isFinalized(false) |
46 | , m_isLinked(false) |
47 | , m_is32Bit(is32Bit) |
48 | , m_rawBuffer(static_cast<uint8_t*>(fastZeroedMalloc(sizeof(LinkingData) + (is32Bit ? s_offset16TableSize + s_offset32TableSize : s_offset16TableSize)))) |
49 | { |
50 | } |
51 | |
52 | ALWAYS_INLINE UnlinkedMetadataTable::UnlinkedMetadataTable(EmptyTag) |
53 | : m_hasMetadata(false) |
54 | , m_isFinalized(true) |
55 | , m_isLinked(false) |
56 | , m_is32Bit(false) |
57 | , m_rawBuffer(nullptr) |
58 | { |
59 | } |
60 | |
61 | ALWAYS_INLINE UnlinkedMetadataTable::~UnlinkedMetadataTable() |
62 | { |
63 | ASSERT(!m_isLinked); |
64 | if (m_hasMetadata || !m_isFinalized) |
65 | fastFree(m_rawBuffer); |
66 | } |
67 | |
68 | ALWAYS_INLINE unsigned UnlinkedMetadataTable::addEntry(OpcodeID opcodeID) |
69 | { |
70 | ASSERT(!m_isFinalized && opcodeID < s_offsetTableEntries - 1); |
71 | m_hasMetadata = true; |
72 | return preprocessBuffer()[opcodeID]++; |
73 | } |
74 | |
75 | ALWAYS_INLINE size_t UnlinkedMetadataTable::sizeInBytes() |
76 | { |
77 | if (m_isFinalized && !m_hasMetadata) |
78 | return 0; |
79 | |
80 | if (m_is32Bit) |
81 | return s_offset16TableSize + s_offset32TableSize; |
82 | return s_offset16TableSize; |
83 | } |
84 | |
85 | ALWAYS_INLINE size_t UnlinkedMetadataTable::sizeInBytes(MetadataTable& metadataTable) |
86 | { |
87 | ASSERT(m_isFinalized); |
88 | |
89 | // In this case, we return the size of the table minus the offset table, |
90 | // which was already accounted for in the UnlinkedCodeBlock. |
91 | size_t result = totalSize(); |
92 | if (metadataTable.buffer() == buffer()) { |
93 | ASSERT(m_isLinked); |
94 | if (m_is32Bit) |
95 | return result - (s_offset16TableSize + s_offset32TableSize); |
96 | return result - s_offset16TableSize; |
97 | } |
98 | return result; |
99 | } |
100 | |
101 | ALWAYS_INLINE RefPtr<MetadataTable> UnlinkedMetadataTable::link() |
102 | { |
103 | ASSERT(m_isFinalized); |
104 | |
105 | if (!m_hasMetadata) |
106 | return nullptr; |
107 | |
108 | unsigned totalSize = this->totalSize(); |
109 | unsigned offsetTableSize = this->offsetTableSize(); |
110 | uint8_t* buffer; |
111 | if (!m_isLinked) { |
112 | m_isLinked = true; |
113 | m_rawBuffer = buffer = reinterpret_cast<uint8_t*>(fastRealloc(m_rawBuffer, sizeof(LinkingData) + totalSize)); |
114 | } else { |
115 | buffer = reinterpret_cast<uint8_t*>(fastMalloc(sizeof(LinkingData) + totalSize)); |
116 | memcpy(buffer, m_rawBuffer, sizeof(LinkingData) + offsetTableSize); |
117 | } |
118 | memset(buffer + sizeof(LinkingData) + offsetTableSize, 0, totalSize - offsetTableSize); |
119 | return adoptRef(*new (buffer + sizeof(LinkingData)) MetadataTable(*this)); |
120 | } |
121 | |
122 | ALWAYS_INLINE void UnlinkedMetadataTable::unlink(MetadataTable& metadataTable) |
123 | { |
124 | ASSERT(m_isFinalized); |
125 | if (!m_hasMetadata) |
126 | return; |
127 | |
128 | if (metadataTable.buffer() == buffer()) { |
129 | ASSERT(m_isLinked); |
130 | m_isLinked = false; |
131 | m_rawBuffer = static_cast<uint8_t*>(fastRealloc(m_rawBuffer, sizeof(LinkingData) + offsetTableSize())); |
132 | return; |
133 | } |
134 | fastFree(&metadataTable.linkingData()); |
135 | } |
136 | |
137 | } // namespace JSC |
138 | |