| 1 | /* |
| 2 | * Copyright (C) 2012, 2016 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 "ArrayConventions.h" |
| 29 | #include "Butterfly.h" |
| 30 | #include "IndexingHeader.h" |
| 31 | #include "MarkedSpace.h" |
| 32 | #include "SparseArrayValueMap.h" |
| 33 | #include "Structure.h" |
| 34 | #include "WriteBarrier.h" |
| 35 | #include <wtf/Noncopyable.h> |
| 36 | |
| 37 | namespace JSC { |
| 38 | |
| 39 | // This struct holds the actual data values of an array. A JSArray object points to its contained ArrayStorage |
| 40 | // struct by pointing to m_vector. To access the contained ArrayStorage struct, use the getStorage() and |
| 41 | // setStorage() methods. It is important to note that there may be space before the ArrayStorage that |
| 42 | // is used to quick unshift / shift operation. The actual allocated pointer is available by using: |
| 43 | // getStorage() - m_indexBias * sizeof(JSValue) |
| 44 | // All slots in ArrayStorage (slots from 0 to vectorLength) are expected to be initialized to a JSValue or, |
| 45 | // for hole slots, JSValue(). |
| 46 | struct ArrayStorage { |
| 47 | WTF_MAKE_NONCOPYABLE(ArrayStorage); |
| 48 | private: |
| 49 | ArrayStorage() { } // Not directly instantiable. Can only be created as part of a Butterfly. |
| 50 | public: |
| 51 | |
| 52 | static ArrayStorage* from(Butterfly* butterfly) { return reinterpret_cast_ptr<ArrayStorage*>(butterfly); } |
| 53 | static ArrayStorage* (IndexingHeader* ) { return indexingHeader->arrayStorage(); } |
| 54 | |
| 55 | Butterfly* butterfly() { return reinterpret_cast<Butterfly*>(this); } |
| 56 | IndexingHeader* () { return IndexingHeader::from(this); } |
| 57 | const IndexingHeader* () const { return IndexingHeader::from(this); } |
| 58 | |
| 59 | // We steal two fields from the indexing header: vectorLength and length. |
| 60 | unsigned length() const { return indexingHeader()->publicLength(); } |
| 61 | void setLength(unsigned length) { indexingHeader()->setPublicLength(length); } |
| 62 | unsigned vectorLength() const { return indexingHeader()->vectorLength(); } |
| 63 | void setVectorLength(unsigned length) { indexingHeader()->setVectorLength(length); } |
| 64 | |
| 65 | ALWAYS_INLINE void (const ArrayStorage& other) |
| 66 | { |
| 67 | m_sparseMap.copyFrom(other.m_sparseMap); |
| 68 | m_indexBias = other.m_indexBias; |
| 69 | m_numValuesInVector = other.m_numValuesInVector; |
| 70 | } |
| 71 | |
| 72 | bool hasHoles() const |
| 73 | { |
| 74 | return m_numValuesInVector != length(); |
| 75 | } |
| 76 | |
| 77 | bool inSparseMode() |
| 78 | { |
| 79 | return m_sparseMap && m_sparseMap->sparseMode(); |
| 80 | } |
| 81 | |
| 82 | ContiguousJSValues vector() { return ContiguousJSValues(m_vector, vectorLength()); } |
| 83 | |
| 84 | static ptrdiff_t lengthOffset() { return Butterfly::offsetOfPublicLength(); } |
| 85 | static ptrdiff_t vectorLengthOffset() { return Butterfly::offsetOfVectorLength(); } |
| 86 | static ptrdiff_t numValuesInVectorOffset() { return OBJECT_OFFSETOF(ArrayStorage, m_numValuesInVector); } |
| 87 | static ptrdiff_t vectorOffset() { return OBJECT_OFFSETOF(ArrayStorage, m_vector); } |
| 88 | static ptrdiff_t indexBiasOffset() { return OBJECT_OFFSETOF(ArrayStorage, m_indexBias); } |
| 89 | static ptrdiff_t sparseMapOffset() { return OBJECT_OFFSETOF(ArrayStorage, m_sparseMap); } |
| 90 | |
| 91 | static size_t sizeFor(unsigned vectorLength) |
| 92 | { |
| 93 | return ArrayStorage::vectorOffset() + vectorLength * sizeof(WriteBarrier<Unknown>); |
| 94 | } |
| 95 | |
| 96 | static size_t totalSizeFor(unsigned indexBias, size_t propertyCapacity, unsigned vectorLength) |
| 97 | { |
| 98 | return Butterfly::totalSize(indexBias, propertyCapacity, true, sizeFor(vectorLength)); |
| 99 | } |
| 100 | |
| 101 | size_t totalSize(size_t propertyCapacity) const |
| 102 | { |
| 103 | return totalSizeFor(m_indexBias, propertyCapacity, vectorLength()); |
| 104 | } |
| 105 | |
| 106 | size_t totalSize(Structure* structure) const |
| 107 | { |
| 108 | return totalSize(structure->outOfLineCapacity()); |
| 109 | } |
| 110 | |
| 111 | static unsigned availableVectorLength(unsigned indexBias, size_t propertyCapacity, unsigned vectorLength) |
| 112 | { |
| 113 | size_t cellSize = MarkedSpace::optimalSizeFor(totalSizeFor(indexBias, propertyCapacity, vectorLength)); |
| 114 | |
| 115 | vectorLength = (cellSize - totalSizeFor(indexBias, propertyCapacity, 0)) / sizeof(WriteBarrier<Unknown>); |
| 116 | |
| 117 | return vectorLength; |
| 118 | } |
| 119 | |
| 120 | static unsigned availableVectorLength(unsigned indexBias, Structure* structure, unsigned vectorLength) |
| 121 | { |
| 122 | return availableVectorLength(indexBias, structure->outOfLineCapacity(), vectorLength); |
| 123 | } |
| 124 | |
| 125 | unsigned availableVectorLength(size_t propertyCapacity, unsigned vectorLength) |
| 126 | { |
| 127 | return availableVectorLength(m_indexBias, propertyCapacity, vectorLength); |
| 128 | } |
| 129 | |
| 130 | unsigned availableVectorLength(Structure* structure, unsigned vectorLength) |
| 131 | { |
| 132 | return availableVectorLength(structure->outOfLineCapacity(), vectorLength); |
| 133 | } |
| 134 | |
| 135 | static unsigned optimalVectorLength(unsigned indexBias, size_t propertyCapacity, unsigned vectorLength) |
| 136 | { |
| 137 | vectorLength = std::max(BASE_ARRAY_STORAGE_VECTOR_LEN, vectorLength); |
| 138 | return availableVectorLength(indexBias, propertyCapacity, vectorLength); |
| 139 | } |
| 140 | |
| 141 | static unsigned optimalVectorLength(unsigned indexBias, Structure* structure, unsigned vectorLength) |
| 142 | { |
| 143 | return optimalVectorLength(indexBias, structure->outOfLineCapacity(), vectorLength); |
| 144 | } |
| 145 | |
| 146 | unsigned optimalVectorLength(size_t propertyCapacity, unsigned vectorLength) |
| 147 | { |
| 148 | return optimalVectorLength(m_indexBias, propertyCapacity, vectorLength); |
| 149 | } |
| 150 | |
| 151 | unsigned optimalVectorLength(Structure* structure, unsigned vectorLength) |
| 152 | { |
| 153 | return optimalVectorLength(structure->outOfLineCapacity(), vectorLength); |
| 154 | } |
| 155 | |
| 156 | WriteBarrier<SparseArrayValueMap> m_sparseMap; |
| 157 | unsigned m_indexBias; |
| 158 | unsigned m_numValuesInVector; |
| 159 | #if USE(JSVALUE32_64) |
| 160 | uintptr_t m_padding; |
| 161 | #endif |
| 162 | WriteBarrier<Unknown> m_vector[1]; |
| 163 | }; |
| 164 | |
| 165 | } // namespace JSC |
| 166 | |