2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, 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 COMPUTER, 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.
29 #include <wtf/HashSet.h>
30 #include <wtf/PassRefPtr.h>
31 #include <wtf/RefCounted.h>
32 #include <wtf/Vector.h>
37 class ArrayBufferView;
39 class ArrayBufferContents {
40 WTF_MAKE_NONCOPYABLE(ArrayBufferContents);
47 inline ~ArrayBufferContents();
49 void* data() { return m_data; }
50 unsigned sizeInBytes() { return m_sizeInBytes; }
53 ArrayBufferContents(void* data, unsigned sizeInBytes)
55 , m_sizeInBytes(sizeInBytes)
58 friend class ArrayBuffer;
60 enum InitializationPolicy {
65 static inline void tryAllocate(unsigned numElements, unsigned elementByteSize, InitializationPolicy, ArrayBufferContents&);
66 void transfer(ArrayBufferContents& other)
68 ASSERT(!other.m_data);
69 other.m_data = m_data;
70 other.m_sizeInBytes = m_sizeInBytes;
76 unsigned m_sizeInBytes;
79 class ArrayBuffer : public RefCounted<ArrayBuffer> {
81 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize);
82 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*);
83 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned byteLength);
84 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&);
86 // Only for use by Uint8ClampedArray::createUninitialized.
87 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElements, unsigned elementByteSize);
90 inline const void* data() const;
91 inline unsigned byteLength() const;
93 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
94 inline PassRefPtr<ArrayBuffer> slice(int begin) const;
96 void addView(ArrayBufferView*);
97 void removeView(ArrayBufferView*);
99 WTF_EXPORT_PRIVATE bool transfer(ArrayBufferContents&, Vector<RefPtr<ArrayBufferView> >& neuteredViews);
100 bool isNeutered() { return !m_contents.m_data; }
105 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
107 inline ArrayBuffer(ArrayBufferContents&);
108 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const;
109 inline unsigned clampIndex(int index) const;
110 static inline int clampValue(int x, int left, int right);
112 ArrayBufferContents m_contents;
113 ArrayBufferView* m_firstView;
116 int ArrayBuffer::clampValue(int x, int left, int right)
118 ASSERT(left <= right);
126 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
128 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitialize);
131 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other)
133 return ArrayBuffer::create(other->data(), other->byteLength());
136 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLength)
138 ArrayBufferContents contents;
139 ArrayBufferContents::tryAllocate(byteLength, 1, ArrayBufferContents::ZeroInitialize, contents);
140 if (!contents.m_data)
142 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
143 memcpy(buffer->data(), source, byteLength);
144 return buffer.release();
147 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents)
149 return adoptRef(new ArrayBuffer(contents));
152 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, unsigned elementByteSize)
154 return create(numElements, elementByteSize, ArrayBufferContents::DontInitialize);
157 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
159 ArrayBufferContents contents;
160 ArrayBufferContents::tryAllocate(numElements, elementByteSize, policy, contents);
161 if (!contents.m_data)
163 return adoptRef(new ArrayBuffer(contents));
166 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
169 contents.transfer(m_contents);
172 void* ArrayBuffer::data()
174 return m_contents.m_data;
177 const void* ArrayBuffer::data() const
179 return m_contents.m_data;
182 unsigned ArrayBuffer::byteLength() const
184 return m_contents.m_sizeInBytes;
187 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
189 return sliceImpl(clampIndex(begin), clampIndex(end));
192 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
194 return sliceImpl(clampIndex(begin), byteLength());
197 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) const
199 unsigned size = begin <= end ? end - begin : 0;
200 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
203 unsigned ArrayBuffer::clampIndex(int index) const
205 unsigned currentLength = byteLength();
207 index = currentLength + index;
208 return clampValue(index, 0, currentLength);
211 void ArrayBufferContents::tryAllocate(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy, ArrayBufferContents& result)
213 // Do not allow 32-bit overflow of the total size.
214 // FIXME: Why not? The tryFastCalloc function already checks its arguments,
215 // and will fail if there is any overflow, so why should we include a
216 // redudant unnecessarily restrictive check here?
218 unsigned totalSize = numElements * elementByteSize;
219 if (totalSize / numElements != elementByteSize) {
224 bool allocationSucceeded = false;
225 if (policy == ZeroInitialize)
226 allocationSucceeded = WTF::tryFastCalloc(numElements, elementByteSize).getValue(result.m_data);
228 ASSERT(policy == DontInitialize);
229 allocationSucceeded = WTF::tryFastMalloc(numElements * elementByteSize).getValue(result.m_data);
232 if (allocationSucceeded) {
233 result.m_sizeInBytes = numElements * elementByteSize;
239 ArrayBufferContents::~ArrayBufferContents()
241 WTF::fastFree(m_data);
246 using WTF::ArrayBuffer;
248 #endif // ArrayBuffer_h