| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2009 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 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. | |
| 24 */ | |
| 25 | |
| 26 #ifndef ArrayBuffer_h | |
| 27 #define ArrayBuffer_h | |
| 28 | |
| 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" | |
| 34 | |
| 35 namespace WTF { | |
| 36 | |
| 37 class ArrayBuffer; | |
| 38 class ArrayBufferView; | |
| 39 | |
| 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 by
teLength); | |
| 45 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&); | |
| 46 | |
| 47 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns
igned elementByteSize); | |
| 48 | |
| 49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serial
izeObject | |
| 50 // (through DOMArrayBuffer::createUninitialized). | |
| 51 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen
ts, unsigned elementByteSize); | |
| 52 | |
| 53 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns
igned elementByteSize); | |
| 54 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsig
ned byteLength); | |
| 55 | |
| 56 inline void* data(); | |
| 57 inline const void* data() const; | |
| 58 inline unsigned byteLength() const; | |
| 59 | |
| 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; | |
| 64 | |
| 65 void addView(ArrayBufferView*); | |
| 66 void removeView(ArrayBufferView*); | |
| 67 | |
| 68 bool transfer(ArrayBufferContents&); | |
| 69 bool shareContentsWith(ArrayBufferContents&); | |
| 70 bool isNeutered() const { return m_isNeutered; } | |
| 71 bool isShared() const { return m_contents.isShared(); } | |
| 72 | |
| 73 ~ArrayBuffer() { } | |
| 74 | |
| 75 protected: | |
| 76 inline explicit ArrayBuffer(ArrayBufferContents&); | |
| 77 | |
| 78 private: | |
| 79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy); | |
| 80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns
igned elementByteSize, ArrayBufferContents::InitializationPolicy); | |
| 81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns
igned elementByteSize, ArrayBufferContents::InitializationPolicy); | |
| 82 | |
| 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); | |
| 86 | |
| 87 ArrayBufferContents m_contents; | |
| 88 ArrayBufferView* m_firstView; | |
| 89 bool m_isNeutered; | |
| 90 }; | |
| 91 | |
| 92 int ArrayBuffer::clampValue(int x, int left, int right) | |
| 93 { | |
| 94 ASSERT(left <= right); | |
| 95 if (x < left) | |
| 96 x = left; | |
| 97 if (right < x) | |
| 98 x = right; | |
| 99 return x; | |
| 100 } | |
| 101 | |
| 102 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize) | |
| 103 { | |
| 104 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial
ize); | |
| 105 } | |
| 106 | |
| 107 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) | |
| 108 { | |
| 109 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra
yBuffer? | |
| 110 ASSERT(!other->isShared()); | |
| 111 return ArrayBuffer::create(other->data(), other->byteLength()); | |
| 112 } | |
| 113 | |
| 114 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen
gth) | |
| 115 { | |
| 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(); | |
| 121 } | |
| 122 | |
| 123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) | |
| 124 { | |
| 125 RELEASE_ASSERT(contents.data()); | |
| 126 return adoptRef(new ArrayBuffer(contents)); | |
| 127 } | |
| 128 | |
| 129 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize) | |
| 130 { | |
| 131 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI
nitialize); | |
| 132 } | |
| 133 | |
| 134 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u
nsigned elementByteSize) | |
| 135 { | |
| 136 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial
ize); | |
| 137 } | |
| 138 | |
| 139 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize, ArrayBufferContents::InitializationPolicy policy) | |
| 140 { | |
| 141 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::NotShared, policy); | |
| 142 RELEASE_ASSERT(contents.data()); | |
| 143 return adoptRef(new ArrayBuffer(contents)); | |
| 144 } | |
| 145 | |
| 146 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) | |
| 147 { | |
| 148 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::NotShared, policy); | |
| 149 if (!contents.data()) | |
| 150 return nullptr; | |
| 151 return adoptRef(new ArrayBuffer(contents)); | |
| 152 } | |
| 153 | |
| 154 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize) | |
| 155 { | |
| 156 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI
nitialize); | |
| 157 } | |
| 158 | |
| 159 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b
yteLength) | |
| 160 { | |
| 161 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr
ayBufferContents::ZeroInitialize); | |
| 162 RELEASE_ASSERT(contents.data()); | |
| 163 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); | |
| 164 memcpy(buffer->data(), source, byteLength); | |
| 165 return buffer.release(); | |
| 166 } | |
| 167 | |
| 168 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) | |
| 169 { | |
| 170 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::Shared, policy); | |
| 171 RELEASE_ASSERT(contents.data()); | |
| 172 return adoptRef(new ArrayBuffer(contents)); | |
| 173 } | |
| 174 | |
| 175 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) | |
| 176 : m_firstView(0), m_isNeutered(false) | |
| 177 { | |
| 178 if (contents.isShared()) | |
| 179 contents.shareWith(m_contents); | |
| 180 else | |
| 181 contents.transfer(m_contents); | |
| 182 } | |
| 183 | |
| 184 void* ArrayBuffer::data() | |
| 185 { | |
| 186 return m_contents.data(); | |
| 187 } | |
| 188 | |
| 189 const void* ArrayBuffer::data() const | |
| 190 { | |
| 191 return m_contents.data(); | |
| 192 } | |
| 193 | |
| 194 unsigned ArrayBuffer::byteLength() const | |
| 195 { | |
| 196 return m_contents.sizeInBytes(); | |
| 197 } | |
| 198 | |
| 199 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const | |
| 200 { | |
| 201 return sliceImpl(clampIndex(begin), clampIndex(end)); | |
| 202 } | |
| 203 | |
| 204 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const | |
| 205 { | |
| 206 return sliceImpl(clampIndex(begin), byteLength()); | |
| 207 } | |
| 208 | |
| 209 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con
st | |
| 210 { | |
| 211 unsigned size = begin <= end ? end - begin : 0; | |
| 212 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); | |
| 213 } | |
| 214 | |
| 215 unsigned ArrayBuffer::clampIndex(int index) const | |
| 216 { | |
| 217 unsigned currentLength = byteLength(); | |
| 218 if (index < 0) | |
| 219 index = currentLength + index; | |
| 220 return clampValue(index, 0, currentLength); | |
| 221 } | |
| 222 | |
| 223 } // namespace WTF | |
| 224 | |
| 225 using WTF::ArrayBuffer; | |
| 226 | |
| 227 #endif // ArrayBuffer_h | |
| OLD | NEW |