fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #ifndef Butterfly_h |
| 27 | #define Butterfly_h |
| 28 | |
| 29 | #include "IndexingHeader.h" |
| 30 | #include "PropertyOffset.h" |
| 31 | #include "PropertyStorage.h" |
| 32 | #include <wtf/Noncopyable.h> |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 33 | |
| 34 | namespace JSC { |
| 35 | |
ggaren@apple.com | 9a9a4b5 | 2013-04-18 19:32:17 +0000 | [diff] [blame] | 36 | class VM; |
mhahnenberg@apple.com | 02e39c7 | 2012-10-12 19:38:35 +0000 | [diff] [blame] | 37 | class CopyVisitor; |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 38 | struct ArrayStorage; |
| 39 | |
oliver@apple.com | a009962 | 2013-01-29 21:11:11 +0000 | [diff] [blame] | 40 | template <typename T> struct ContiguousData { |
| 41 | ContiguousData() |
| 42 | : m_data(0) |
| 43 | #if !ASSERT_DISABLED |
| 44 | , m_length(0) |
| 45 | #endif |
| 46 | { |
| 47 | } |
| 48 | ContiguousData(T* data, size_t length) |
| 49 | : m_data(data) |
| 50 | #if !ASSERT_DISABLED |
| 51 | , m_length(length) |
| 52 | #endif |
| 53 | { |
| 54 | UNUSED_PARAM(length); |
| 55 | } |
| 56 | |
| 57 | const T& operator[](size_t index) const { ASSERT(index < m_length); return m_data[index]; } |
| 58 | T& operator[](size_t index) { ASSERT(index < m_length); return m_data[index]; } |
| 59 | |
| 60 | T* data() const { return m_data; } |
| 61 | #if !ASSERT_DISABLED |
| 62 | size_t length() const { return m_length; } |
| 63 | #endif |
| 64 | |
| 65 | private: |
| 66 | T* m_data; |
| 67 | #if !ASSERT_DISABLED |
| 68 | size_t m_length; |
| 69 | #endif |
| 70 | }; |
| 71 | |
| 72 | typedef ContiguousData<double> ContiguousDoubles; |
andersca@apple.com | c3523f8 | 2013-10-18 23:41:24 +0000 | [diff] [blame] | 73 | typedef ContiguousData<WriteBarrier<Unknown>> ContiguousJSValues; |
oliver@apple.com | a009962 | 2013-01-29 21:11:11 +0000 | [diff] [blame] | 74 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 75 | class Butterfly { |
| 76 | WTF_MAKE_NONCOPYABLE(Butterfly); |
| 77 | private: |
| 78 | Butterfly() { } // Not instantiable. |
| 79 | public: |
| 80 | |
| 81 | static size_t totalSize(size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes) |
| 82 | { |
| 83 | ASSERT(indexingPayloadSizeInBytes ? hasIndexingHeader : true); |
| 84 | ASSERT(sizeof(EncodedJSValue) == sizeof(IndexingHeader)); |
| 85 | return (preCapacity + propertyCapacity) * sizeof(EncodedJSValue) + (hasIndexingHeader ? sizeof(IndexingHeader) : 0) + indexingPayloadSizeInBytes; |
| 86 | } |
| 87 | |
| 88 | static Butterfly* fromBase(void* base, size_t preCapacity, size_t propertyCapacity) |
| 89 | { |
| 90 | return reinterpret_cast<Butterfly*>(static_cast<EncodedJSValue*>(base) + preCapacity + propertyCapacity + 1); |
| 91 | } |
| 92 | |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 93 | // This method is here not just because it's handy, but to remind you that |
| 94 | // the whole point of butterflies is to do evil pointer arithmetic. |
| 95 | static Butterfly* fromPointer(char* ptr) |
| 96 | { |
| 97 | return reinterpret_cast<Butterfly*>(ptr); |
| 98 | } |
| 99 | |
| 100 | char* pointer() { return reinterpret_cast<char*>(this); } |
| 101 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 102 | static ptrdiff_t offsetOfIndexingHeader() { return IndexingHeader::offsetOfIndexingHeader(); } |
fpizlo@apple.com | 537a477 | 2013-08-19 23:16:01 +0000 | [diff] [blame] | 103 | static ptrdiff_t offsetOfArrayBuffer() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfArrayBuffer(); } |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 104 | static ptrdiff_t offsetOfPublicLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfPublicLength(); } |
| 105 | static ptrdiff_t offsetOfVectorLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfVectorLength(); } |
| 106 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 107 | static Butterfly* createUninitialized(VM&, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes); |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 108 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 109 | static Butterfly* create(VM&, JSCell* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader&, size_t indexingPayloadSizeInBytes); |
| 110 | static Butterfly* create(VM&, JSCell* intendedOwner, Structure*); |
mhahnenberg@apple.com | 02e39c7 | 2012-10-12 19:38:35 +0000 | [diff] [blame] | 111 | static Butterfly* createUninitializedDuringCollection(CopyVisitor&, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes); |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 112 | |
| 113 | IndexingHeader* indexingHeader() { return IndexingHeader::from(this); } |
| 114 | const IndexingHeader* indexingHeader() const { return IndexingHeader::from(this); } |
| 115 | PropertyStorage propertyStorage() { return indexingHeader()->propertyStorage(); } |
| 116 | ConstPropertyStorage propertyStorage() const { return indexingHeader()->propertyStorage(); } |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 117 | |
| 118 | uint32_t publicLength() { return indexingHeader()->publicLength(); } |
| 119 | uint32_t vectorLength() { return indexingHeader()->vectorLength(); } |
| 120 | void setPublicLength(uint32_t value) { indexingHeader()->setPublicLength(value); } |
| 121 | void setVectorLength(uint32_t value) { indexingHeader()->setVectorLength(value); } |
| 122 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 123 | template<typename T> |
commit-queue@webkit.org | 94ea812 | 2013-02-25 13:13:43 +0000 | [diff] [blame] | 124 | T* indexingPayload() { return reinterpret_cast_ptr<T*>(this); } |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 125 | ArrayStorage* arrayStorage() { return indexingPayload<ArrayStorage>(); } |
andersca@apple.com | c3523f8 | 2013-10-18 23:41:24 +0000 | [diff] [blame] | 126 | ContiguousJSValues contiguousInt32() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); } |
oliver@apple.com | a009962 | 2013-01-29 21:11:11 +0000 | [diff] [blame] | 127 | |
| 128 | ContiguousDoubles contiguousDouble() { return ContiguousDoubles(indexingPayload<double>(), vectorLength()); } |
andersca@apple.com | c3523f8 | 2013-10-18 23:41:24 +0000 | [diff] [blame] | 129 | ContiguousJSValues contiguous() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); } |
fpizlo@apple.com | 0e9910a | 2012-10-09 23:39:53 +0000 | [diff] [blame] | 130 | |
| 131 | static Butterfly* fromContiguous(WriteBarrier<Unknown>* contiguous) |
| 132 | { |
| 133 | return reinterpret_cast<Butterfly*>(contiguous); |
| 134 | } |
fpizlo@apple.com | 75c91a7 | 2012-11-08 22:28:25 +0000 | [diff] [blame] | 135 | static Butterfly* fromContiguous(double* contiguous) |
| 136 | { |
| 137 | return reinterpret_cast<Butterfly*>(contiguous); |
| 138 | } |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 139 | |
| 140 | static ptrdiff_t offsetOfPropertyStorage() { return -static_cast<ptrdiff_t>(sizeof(IndexingHeader)); } |
| 141 | static int indexOfPropertyStorage() |
| 142 | { |
| 143 | ASSERT(sizeof(IndexingHeader) == sizeof(EncodedJSValue)); |
| 144 | return -1; |
| 145 | } |
| 146 | |
| 147 | void* base(size_t preCapacity, size_t propertyCapacity) { return propertyStorage() - propertyCapacity - preCapacity; } |
| 148 | void* base(Structure*); |
mhahnenberg@apple.com | 30408ac | 2012-12-18 01:03:54 +0000 | [diff] [blame] | 149 | |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 150 | static Butterfly* createOrGrowArrayRight( |
| 151 | Butterfly*, VM&, JSCell* intendedOwner, Structure* oldStructure, |
| 152 | size_t propertyCapacity, bool hadIndexingHeader, |
| 153 | size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); |
mhahnenberg@apple.com | 30408ac | 2012-12-18 01:03:54 +0000 | [diff] [blame] | 154 | |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 155 | // The butterfly reallocation methods perform the reallocation itself but do not change any |
| 156 | // of the meta-data to reflect that the reallocation occurred. Note that this set of |
| 157 | // methods is not exhaustive and is not intended to encapsulate all possible allocation |
| 158 | // modes of butterflies - there are code paths that allocate butterflies by calling |
| 159 | // directly into Heap::tryAllocateStorage. |
ggaren@apple.com | ecd38ec | 2014-03-25 03:27:46 +0000 | [diff] [blame] | 160 | static Butterfly* createOrGrowPropertyStorage(Butterfly*, VM&, JSCell* intendedOwner, Structure*, size_t oldPropertyCapacity, size_t newPropertyCapacity); |
oliver@apple.com | a03796a | 2013-07-25 04:01:20 +0000 | [diff] [blame] | 161 | Butterfly* growArrayRight(VM&, JSCell* intendedOwner, Structure* oldStructure, size_t propertyCapacity, bool hadIndexingHeader, size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); // Assumes that preCapacity is zero, and asserts as much. |
| 162 | Butterfly* growArrayRight(VM&, JSCell* intendedOwner, Structure*, size_t newIndexingPayloadSizeInBytes); |
| 163 | Butterfly* resizeArray(VM&, JSCell* intendedOwner, size_t propertyCapacity, bool oldHasIndexingHeader, size_t oldIndexingPayloadSizeInBytes, size_t newPreCapacity, bool newHasIndexingHeader, size_t newIndexingPayloadSizeInBytes); |
| 164 | Butterfly* resizeArray(VM&, JSCell* intendedOwner, Structure*, size_t newPreCapacity, size_t newIndexingPayloadSizeInBytes); // Assumes that you're not changing whether or not the object has an indexing header. |
fpizlo@apple.com | d8dd053 | 2012-09-13 04:18:52 +0000 | [diff] [blame] | 165 | Butterfly* unshift(Structure*, size_t numberOfSlots); |
| 166 | Butterfly* shift(Structure*, size_t numberOfSlots); |
| 167 | }; |
| 168 | |
| 169 | } // namespace JSC |
| 170 | |
| 171 | #endif // Butterfly_h |
| 172 | |