Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / ArrayBuffer.h
blob38e2a00d1a160e104fc39be3bbc1dffbc7aab01f
1 /*
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
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.
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.
26 #ifndef ArrayBuffer_h
27 #define ArrayBuffer_h
29 #include "wtf/ArrayBufferContents.h"
30 #include "wtf/HashSet.h"
31 #include "wtf/PassRefPtr.h"
32 #include "wtf/RefCounted.h"
33 #include "wtf/WTFExport.h"
35 namespace WTF {
37 class ArrayBuffer;
38 class ArrayBufferView;
40 class WTF_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> {
41 public:
42 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize);
43 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*);
44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned byteLength);
45 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&);
47 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, unsigned elementByteSize);
49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serializeObject
50 // (through DOMArrayBuffer::createUninitialized).
51 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElements, unsigned elementByteSize);
53 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, unsigned elementByteSize);
54 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsigned byteLength);
56 inline void* data();
57 inline const void* data() const;
58 inline unsigned byteLength() const;
60 // Creates a new ArrayBuffer object with copy of bytes in this object
61 // ranging from |begin| upto but not including |end|.
62 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
63 inline PassRefPtr<ArrayBuffer> slice(int begin) const;
65 void addView(ArrayBufferView*);
66 void removeView(ArrayBufferView*);
68 bool transfer(ArrayBufferContents&);
69 bool shareContentsWith(ArrayBufferContents&);
70 bool isNeutered() const { return m_isNeutered; }
71 bool isShared() const { return m_contents.isShared(); }
73 ~ArrayBuffer() { }
75 protected:
76 inline explicit ArrayBuffer(ArrayBufferContents&);
78 private:
79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
83 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const;
84 inline unsigned clampIndex(int index) const;
85 static inline int clampValue(int x, int left, int right);
87 ArrayBufferContents m_contents;
88 ArrayBufferView* m_firstView;
89 bool m_isNeutered;
92 int ArrayBuffer::clampValue(int x, int left, int right)
94 ASSERT(left <= right);
95 if (x < left)
96 x = left;
97 if (right < x)
98 x = right;
99 return x;
102 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
104 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitialize);
107 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other)
109 // TODO(binji): support creating a SharedArrayBuffer by copying another ArrayBuffer?
110 ASSERT(!other->isShared());
111 return ArrayBuffer::create(other->data(), other->byteLength());
114 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLength)
116 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, ArrayBufferContents::ZeroInitialize);
117 RELEASE_ASSERT(contents.data());
118 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
119 memcpy(buffer->data(), source, byteLength);
120 return buffer.release();
123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents)
125 RELEASE_ASSERT(contents.data());
126 return adoptRef(new ArrayBuffer(contents));
129 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize)
131 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroInitialize);
134 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, unsigned elementByteSize)
136 return create(numElements, elementByteSize, ArrayBufferContents::DontInitialize);
139 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
141 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents::NotShared, policy);
142 RELEASE_ASSERT(contents.data());
143 return adoptRef(new ArrayBuffer(contents));
146 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
148 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents::NotShared, policy);
149 if (!contents.data())
150 return nullptr;
151 return adoptRef(new ArrayBuffer(contents));
154 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize)
156 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroInitialize);
159 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned byteLength)
161 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, ArrayBufferContents::ZeroInitialize);
162 RELEASE_ASSERT(contents.data());
163 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
164 memcpy(buffer->data(), source, byteLength);
165 return buffer.release();
168 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
170 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents::Shared, policy);
171 RELEASE_ASSERT(contents.data());
172 return adoptRef(new ArrayBuffer(contents));
175 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
176 : m_firstView(0), m_isNeutered(false)
178 if (contents.isShared())
179 contents.shareWith(m_contents);
180 else
181 contents.transfer(m_contents);
184 void* ArrayBuffer::data()
186 return m_contents.data();
189 const void* ArrayBuffer::data() const
191 return m_contents.data();
194 unsigned ArrayBuffer::byteLength() const
196 return m_contents.sizeInBytes();
199 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
201 return sliceImpl(clampIndex(begin), clampIndex(end));
204 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
206 return sliceImpl(clampIndex(begin), byteLength());
209 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) const
211 unsigned size = begin <= end ? end - begin : 0;
212 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
215 unsigned ArrayBuffer::clampIndex(int index) const
217 unsigned currentLength = byteLength();
218 if (index < 0)
219 index = currentLength + index;
220 return clampValue(index, 0, currentLength);
223 } // namespace WTF
225 using WTF::ArrayBuffer;
227 #endif // ArrayBuffer_h