1/*
2 * Copyright (C) 2011 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 RefCountedArray_h
27#define RefCountedArray_h
28
29#include <wtf/FastMalloc.h>
30#include <wtf/StdLibExtras.h>
31#include <wtf/Vector.h>
32
33// This implements a reference counted array for POD** values, which is optimized for:
34// - An empty array only uses one word.
35// - A copy of the array only uses one word (i.e. assignment means aliasing).
36// - The vector can't grow beyond 2^32-1 elements.
37// - In all other regards this has similar space usage to a Vector.
38//
39// ** This could be modified to support non-POD values quite easily. It just
40// hasn't been, so far, because there has been no need. Moreover, even now,
41// it's used for things that aren't quite POD according to the official
42// defintion, such as JSC::Instruction.
43
44namespace WTF {
45
46template<typename T>
47class RefCountedArray {
48public:
49 RefCountedArray()
50 : m_data(0)
51 {
52 }
53
54 RefCountedArray(const RefCountedArray& other)
55 : m_data(other.m_data)
56 {
57 if (m_data)
58 Header::fromPayload(m_data)->refCount++;
59 }
60
61 explicit RefCountedArray(size_t size)
62 {
63 if (!size) {
64 m_data = 0;
65 return;
66 }
67
68 m_data = (static_cast<Header*>(fastMalloc(Header::size() + sizeof(T) * size)))->payload();
69 Header::fromPayload(m_data)->refCount = 1;
70 Header::fromPayload(m_data)->length = size;
71 ASSERT(Header::fromPayload(m_data)->length == size);
72 VectorTypeOperations<T>::initialize(begin(), end());
73 }
74
75 template<size_t inlineCapacity, typename OverflowHandler>
76 explicit RefCountedArray(const Vector<T, inlineCapacity, OverflowHandler>& other)
77 {
78 if (other.isEmpty()) {
79 m_data = 0;
80 return;
81 }
82
83 m_data = (static_cast<Header*>(fastMalloc(Header::size() + sizeof(T) * other.size())))->payload();
84 Header::fromPayload(m_data)->refCount = 1;
85 Header::fromPayload(m_data)->length = other.size();
86 ASSERT(Header::fromPayload(m_data)->length == other.size());
87 VectorTypeOperations<T>::uninitializedCopy(other.begin(), other.end(), m_data);
88 }
89
90 RefCountedArray& operator=(const RefCountedArray& other)
91 {
92 T* oldData = m_data;
93 m_data = other.m_data;
94 if (m_data)
95 Header::fromPayload(m_data)->refCount++;
96
97 if (!oldData)
98 return *this;
99 if (--Header::fromPayload(oldData)->refCount)
100 return *this;
101 VectorTypeOperations<T>::destruct(oldData, oldData + Header::fromPayload(oldData)->length);
102 fastFree(Header::fromPayload(oldData));
103 return *this;
104 }
105
106 ~RefCountedArray()
107 {
108 if (!m_data)
109 return;
110 if (--Header::fromPayload(m_data)->refCount)
111 return;
112 VectorTypeOperations<T>::destruct(begin(), end());
113 fastFree(Header::fromPayload(m_data));
114 }
115
116 unsigned refCount() const
117 {
118 if (!m_data)
119 return 0;
120 return Header::fromPayload(m_data)->refCount;
121 }
122
123 size_t size() const
124 {
125 if (!m_data)
126 return 0;
127 return Header::fromPayload(m_data)->length;
128 }
129
130 size_t byteSize() const { return size() * sizeof(T); }
131
132 T* data() { return m_data; }
133 T* begin() { return m_data; }
134 T* end()
135 {
136 if (!m_data)
137 return 0;
138 return m_data + Header::fromPayload(m_data)->length;
139 }
140
141 const T* data() const { return m_data; }
142 const T* begin() const { return m_data; }
143 const T* end() const { return const_cast<RefCountedArray*>(this)->end(); }
144
145 T& at(size_t i)
146 {
147 ASSERT_WITH_SECURITY_IMPLICATION(i < size());
148 return begin()[i];
149 }
150
151 const T& at(size_t i) const
152 {
153 ASSERT_WITH_SECURITY_IMPLICATION(i < size());
154 return begin()[i];
155 }
156
157 T& operator[](size_t i) { return at(i); }
158 const T& operator[](size_t i) const { return at(i); }
159
160 bool operator==(const RefCountedArray& other) const
161 {
162 if (m_data == other.m_data)
163 return true;
164 if (!m_data || !other.m_data)
165 return false;
166 unsigned length = Header::fromPayload(m_data)->length;
167 if (length != Header::fromPayload(other.m_data)->length)
168 return false;
169 for (unsigned i = 0; i < length; ++i) {
170 if (m_data[i] != other.m_data[i])
171 return false;
172 }
173 return true;
174 }
175
176private:
177 struct Header {
178 unsigned refCount;
179 unsigned length;
180
181 static size_t size()
182 {
183 return (sizeof(Header) + 7) & ~7;
184 }
185
186 T* payload()
187 {
188 char* result = reinterpret_cast<char*>(this) + size();
189 ASSERT(!(bitwise_cast<uintptr_t>(result) & 7));
190 return reinterpret_cast_ptr<T*>(result);
191 }
192
193 static Header* fromPayload(T* payload)
194 {
195 return reinterpret_cast_ptr<Header*>(reinterpret_cast<char*>(payload) - size());
196 }
197 };
198
199 T* m_data;
200};
201
202} // namespace WTF
203
204using WTF::RefCountedArray;
205
206#endif // RefCountedArray_h
207
208