1/*
2 * Copyright (C) 2012-2019 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
37namespace 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().
46struct ArrayStorage {
47 WTF_MAKE_NONCOPYABLE(ArrayStorage);
48private:
49 ArrayStorage() { } // Not directly instantiable. Can only be created as part of a Butterfly.
50public:
51
52 static ArrayStorage* from(Butterfly* butterfly) { return reinterpret_cast_ptr<ArrayStorage*>(butterfly); }
53 static ArrayStorage* from(IndexingHeader* indexingHeader) { return indexingHeader->arrayStorage(); }
54
55 Butterfly* butterfly() { return reinterpret_cast<Butterfly*>(this); }
56 IndexingHeader* indexingHeader() { return IndexingHeader::from(this); }
57 const IndexingHeader* 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 copyHeaderFromDuringGC(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 inline size_t totalSize(Structure*) const;
107
108 static unsigned availableVectorLength(unsigned indexBias, size_t propertyCapacity, unsigned vectorLength)
109 {
110 size_t cellSize = MarkedSpace::optimalSizeFor(totalSizeFor(indexBias, propertyCapacity, vectorLength));
111
112 vectorLength = (cellSize - totalSizeFor(indexBias, propertyCapacity, 0)) / sizeof(WriteBarrier<Unknown>);
113
114 return vectorLength;
115 }
116
117 inline static unsigned availableVectorLength(unsigned indexBias, Structure*, unsigned vectorLength);
118
119 inline unsigned availableVectorLength(size_t propertyCapacity, unsigned vectorLength);
120
121 inline unsigned availableVectorLength(Structure*, unsigned vectorLength);
122
123 inline static unsigned optimalVectorLength(unsigned indexBias, size_t propertyCapacity, unsigned vectorLength);
124
125 inline static unsigned optimalVectorLength(unsigned indexBias, Structure*, unsigned vectorLength);
126
127 inline unsigned optimalVectorLength(size_t propertyCapacity, unsigned vectorLength);
128
129 inline unsigned optimalVectorLength(Structure*, unsigned vectorLength);
130
131 WriteBarrier<SparseArrayValueMap> m_sparseMap;
132 unsigned m_indexBias;
133 unsigned m_numValuesInVector;
134#if USE(JSVALUE32_64)
135 uintptr_t m_padding;
136#endif
137 WriteBarrier<Unknown> m_vector[1];
138};
139
140} // namespace JSC
141