| 1 | /* |
| 2 | * Copyright (C) 2012-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 "IndexingHeader.h" |
| 29 | #include "IndexingType.h" |
| 30 | #include "PropertyStorage.h" |
| 31 | #include <wtf/Gigacage.h> |
| 32 | #include <wtf/Noncopyable.h> |
| 33 | |
| 34 | namespace JSC { |
| 35 | |
| 36 | class VM; |
| 37 | class CopyVisitor; |
| 38 | class GCDeferralContext; |
| 39 | struct ArrayStorage; |
| 40 | |
| 41 | template <typename T> |
| 42 | struct ContiguousData { |
| 43 | ContiguousData() = default; |
| 44 | ContiguousData(T* data, size_t length) |
| 45 | : m_data(data) |
| 46 | #if !ASSERT_DISABLED |
| 47 | , m_length(length) |
| 48 | #endif |
| 49 | { |
| 50 | UNUSED_PARAM(length); |
| 51 | } |
| 52 | |
| 53 | struct Data { |
| 54 | Data(T& location, IndexingType indexingMode) |
| 55 | : m_data(location) |
| 56 | #if !ASSERT_DISABLED |
| 57 | , m_isWritable(!isCopyOnWrite(indexingMode)) |
| 58 | #endif |
| 59 | { |
| 60 | UNUSED_PARAM(indexingMode); |
| 61 | } |
| 62 | |
| 63 | explicit operator bool() const { return !!m_data.get(); } |
| 64 | |
| 65 | const T& operator=(const T& value) |
| 66 | { |
| 67 | ASSERT(m_isWritable); |
| 68 | m_data = value; |
| 69 | return value; |
| 70 | } |
| 71 | |
| 72 | operator const T&() const { return m_data; } |
| 73 | |
| 74 | // WriteBarrier forwarded methods. |
| 75 | |
| 76 | void set(VM& vm, const JSCell* owner, const JSValue& value) |
| 77 | { |
| 78 | ASSERT(m_isWritable); |
| 79 | m_data.set(vm, owner, value); |
| 80 | } |
| 81 | |
| 82 | void setWithoutWriteBarrier(const JSValue& value) |
| 83 | { |
| 84 | ASSERT(m_isWritable); |
| 85 | m_data.setWithoutWriteBarrier(value); |
| 86 | } |
| 87 | |
| 88 | void setStartingValue(JSValue value) |
| 89 | { |
| 90 | m_data.setStartingValue(value); |
| 91 | } |
| 92 | |
| 93 | void clear() |
| 94 | { |
| 95 | ASSERT(m_isWritable); |
| 96 | m_data.clear(); |
| 97 | } |
| 98 | |
| 99 | JSValue get() const |
| 100 | { |
| 101 | return m_data.get(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | T& m_data; |
| 106 | #if !ASSERT_DISABLED |
| 107 | bool m_isWritable; |
| 108 | #endif |
| 109 | }; |
| 110 | |
| 111 | const Data at(const JSCell* owner, size_t index) const; |
| 112 | Data at(const JSCell* owner, size_t index); |
| 113 | |
| 114 | T& atUnsafe(size_t index) { ASSERT(index < m_length); return m_data[index]; } |
| 115 | |
| 116 | T* data() const { return m_data; } |
| 117 | #if !ASSERT_DISABLED |
| 118 | size_t length() const { return m_length; } |
| 119 | #endif |
| 120 | |
| 121 | private: |
| 122 | T* m_data { nullptr }; |
| 123 | #if !ASSERT_DISABLED |
| 124 | size_t m_length { 0 }; |
| 125 | #endif |
| 126 | }; |
| 127 | |
| 128 | typedef ContiguousData<double> ContiguousDoubles; |
| 129 | typedef ContiguousData<WriteBarrier<Unknown>> ContiguousJSValues; |
| 130 | |
| 131 | class Butterfly { |
| 132 | WTF_MAKE_NONCOPYABLE(Butterfly); |
| 133 | private: |
| 134 | Butterfly() { } // Not instantiable. |
| 135 | public: |
| 136 | |
| 137 | static size_t totalSize(size_t preCapacity, size_t propertyCapacity, bool , size_t indexingPayloadSizeInBytes) |
| 138 | { |
| 139 | ASSERT(indexingPayloadSizeInBytes ? hasIndexingHeader : true); |
| 140 | ASSERT(sizeof(EncodedJSValue) == sizeof(IndexingHeader)); |
| 141 | return (preCapacity + propertyCapacity) * sizeof(EncodedJSValue) + (hasIndexingHeader ? sizeof(IndexingHeader) : 0) + indexingPayloadSizeInBytes; |
| 142 | } |
| 143 | |
| 144 | static Butterfly* fromBase(void* base, size_t preCapacity, size_t propertyCapacity) |
| 145 | { |
| 146 | return reinterpret_cast<Butterfly*>(static_cast<EncodedJSValue*>(base) + preCapacity + propertyCapacity + 1); |
| 147 | } |
| 148 | |
| 149 | ALWAYS_INLINE static unsigned availableContiguousVectorLength(size_t propertyCapacity, unsigned vectorLength); |
| 150 | static unsigned availableContiguousVectorLength(Structure*, unsigned vectorLength); |
| 151 | |
| 152 | ALWAYS_INLINE static unsigned optimalContiguousVectorLength(size_t propertyCapacity, unsigned vectorLength); |
| 153 | static unsigned optimalContiguousVectorLength(Structure*, unsigned vectorLength); |
| 154 | |
| 155 | // This method is here not just because it's handy, but to remind you that |
| 156 | // the whole point of butterflies is to do evil pointer arithmetic. |
| 157 | static Butterfly* fromPointer(char* ptr) |
| 158 | { |
| 159 | return reinterpret_cast<Butterfly*>(ptr); |
| 160 | } |
| 161 | |
| 162 | char* pointer() { return reinterpret_cast<char*>(this); } |
| 163 | |
| 164 | static ptrdiff_t () { return IndexingHeader::offsetOfIndexingHeader(); } |
| 165 | static ptrdiff_t offsetOfArrayBuffer() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfArrayBuffer(); } |
| 166 | static ptrdiff_t offsetOfPublicLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfPublicLength(); } |
| 167 | static ptrdiff_t offsetOfVectorLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfVectorLength(); } |
| 168 | |
| 169 | static Butterfly* tryCreateUninitialized(VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool , size_t indexingPayloadSizeInBytes, GCDeferralContext* = nullptr); |
| 170 | static Butterfly* createUninitialized(VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool , size_t indexingPayloadSizeInBytes); |
| 171 | |
| 172 | static Butterfly* (VM& vm, JSObject*, size_t preCapacity, size_t propertyCapacity, bool , const IndexingHeader& , size_t indexingPayloadSizeInBytes); |
| 173 | static Butterfly* (VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool , const IndexingHeader&, size_t indexingPayloadSizeInBytes); |
| 174 | static Butterfly* create(VM&, JSObject* intendedOwner, Structure*); |
| 175 | |
| 176 | IndexingHeader* () { return IndexingHeader::from(this); } |
| 177 | const IndexingHeader* () const { return IndexingHeader::from(this); } |
| 178 | PropertyStorage propertyStorage() { return indexingHeader()->propertyStorage(); } |
| 179 | ConstPropertyStorage propertyStorage() const { return indexingHeader()->propertyStorage(); } |
| 180 | |
| 181 | uint32_t publicLength() const { return indexingHeader()->publicLength(); } |
| 182 | uint32_t vectorLength() const { return indexingHeader()->vectorLength(); } |
| 183 | void setPublicLength(uint32_t value) { indexingHeader()->setPublicLength(value); } |
| 184 | void setVectorLength(uint32_t value) { indexingHeader()->setVectorLength(value); } |
| 185 | |
| 186 | template<typename T> |
| 187 | T* indexingPayload() { return reinterpret_cast_ptr<T*>(this); } |
| 188 | ArrayStorage* arrayStorage() { return indexingPayload<ArrayStorage>(); } |
| 189 | ContiguousJSValues contiguousInt32() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); } |
| 190 | ContiguousDoubles contiguousDouble() { return ContiguousDoubles(indexingPayload<double>(), vectorLength()); } |
| 191 | ContiguousJSValues contiguous() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); } |
| 192 | |
| 193 | static Butterfly* fromContiguous(WriteBarrier<Unknown>* contiguous) |
| 194 | { |
| 195 | return reinterpret_cast<Butterfly*>(contiguous); |
| 196 | } |
| 197 | static Butterfly* fromContiguous(double* contiguous) |
| 198 | { |
| 199 | return reinterpret_cast<Butterfly*>(contiguous); |
| 200 | } |
| 201 | |
| 202 | static ptrdiff_t offsetOfPropertyStorage() { return -static_cast<ptrdiff_t>(sizeof(IndexingHeader)); } |
| 203 | static int indexOfPropertyStorage() |
| 204 | { |
| 205 | ASSERT(sizeof(IndexingHeader) == sizeof(EncodedJSValue)); |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | void* base(size_t preCapacity, size_t propertyCapacity) { return propertyStorage() - propertyCapacity - preCapacity; } |
| 210 | void* base(Structure*); |
| 211 | |
| 212 | static Butterfly* createOrGrowArrayRight( |
| 213 | Butterfly*, VM&, JSObject* intendedOwner, Structure* oldStructure, |
| 214 | size_t propertyCapacity, bool , |
| 215 | size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); |
| 216 | |
| 217 | // The butterfly reallocation methods perform the reallocation itself but do not change any |
| 218 | // of the meta-data to reflect that the reallocation occurred. Note that this set of |
| 219 | // methods is not exhaustive and is not intended to encapsulate all possible allocation |
| 220 | // modes of butterflies - there are code paths that allocate butterflies by calling |
| 221 | // directly into Heap::tryAllocateStorage. |
| 222 | static Butterfly* createOrGrowPropertyStorage(Butterfly*, VM&, JSObject* intendedOwner, Structure*, size_t oldPropertyCapacity, size_t newPropertyCapacity); |
| 223 | Butterfly* growArrayRight(VM&, JSObject* intendedOwner, Structure* oldStructure, size_t propertyCapacity, bool , size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); // Assumes that preCapacity is zero, and asserts as much. |
| 224 | Butterfly* growArrayRight(VM&, JSObject* intendedOwner, Structure*, size_t newIndexingPayloadSizeInBytes); |
| 225 | |
| 226 | Butterfly* reallocArrayRightIfPossible(VM&, GCDeferralContext&, JSObject* intendedOwner, Structure* oldStructure, size_t propertyCapacity, bool , size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); // Assumes that preCapacity is zero, and asserts as much. |
| 227 | |
| 228 | Butterfly* resizeArray(VM&, JSObject* intendedOwner, size_t propertyCapacity, bool , size_t oldIndexingPayloadSizeInBytes, size_t newPreCapacity, bool , size_t newIndexingPayloadSizeInBytes); |
| 229 | Butterfly* resizeArray(VM&, JSObject* intendedOwner, Structure*, size_t newPreCapacity, size_t newIndexingPayloadSizeInBytes); // Assumes that you're not changing whether or not the object has an indexing header. |
| 230 | Butterfly* unshift(Structure*, size_t numberOfSlots); |
| 231 | Butterfly* shift(Structure*, size_t numberOfSlots); |
| 232 | }; |
| 233 | |
| 234 | } // namespace JSC |
| 235 | |