| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Apple Inc. All rights reserved. | 2 * Copyright (C) 2009 Apple Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
| 6 * are met: | 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 #include "wtf/PassRefPtr.h" | 31 #include "wtf/PassRefPtr.h" |
| 32 #include "wtf/RefCounted.h" | 32 #include "wtf/RefCounted.h" |
| 33 #include "wtf/WTFExport.h" | 33 #include "wtf/WTFExport.h" |
| 34 | 34 |
| 35 namespace WTF { | 35 namespace WTF { |
| 36 | 36 |
| 37 class ArrayBuffer; | 37 class ArrayBuffer; |
| 38 class ArrayBufferView; | 38 class ArrayBufferView; |
| 39 | 39 |
| 40 class WTF_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> { | 40 class WTF_EXPORT ArrayBuffer : public RefCounted<ArrayBuffer> { |
| 41 public: | 41 public: |
| 42 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned
elementByteSize); | 42 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned el
ementByteSize); |
| 43 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*); | 43 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*); |
| 44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned by
teLength); | 44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned byte
Length); |
| 45 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&); | 45 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&); |
| 46 | 46 |
| 47 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns
igned elementByteSize); | 47 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, unsig
ned elementByteSize); |
| 48 | 48 |
| 49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serial
izeObject | 49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serializ
eObject |
| 50 // (through DOMArrayBuffer::createUninitialized). | 50 // (through DOMArrayBuffer::createUninitialized). |
| 51 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen
ts, unsigned elementByteSize); | 51 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElements
, unsigned elementByteSize); |
| 52 | 52 |
| 53 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns
igned elementByteSize); | 53 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, unsig
ned elementByteSize); |
| 54 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsig
ned byteLength); | 54 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsigne
d byteLength); |
| 55 | 55 |
| 56 inline void* data(); | 56 inline void* data(); |
| 57 inline const void* data() const; | 57 inline const void* data() const; |
| 58 inline unsigned byteLength() const; | 58 inline unsigned byteLength() const; |
| 59 | 59 |
| 60 // Creates a new ArrayBuffer object with copy of bytes in this object | 60 // Creates a new ArrayBuffer object with copy of bytes in this object |
| 61 // ranging from |begin| upto but not including |end|. | 61 // ranging from |begin| upto but not including |end|. |
| 62 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; | 62 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; |
| 63 inline PassRefPtr<ArrayBuffer> slice(int begin) const; | 63 inline PassRefPtr<ArrayBuffer> slice(int begin) const; |
| 64 | 64 |
| 65 void addView(ArrayBufferView*); | 65 void addView(ArrayBufferView*); |
| 66 void removeView(ArrayBufferView*); | 66 void removeView(ArrayBufferView*); |
| 67 | 67 |
| 68 bool transfer(ArrayBufferContents&); | 68 bool transfer(ArrayBufferContents&); |
| 69 bool shareContentsWith(ArrayBufferContents&); | 69 bool shareContentsWith(ArrayBufferContents&); |
| 70 bool isNeutered() const { return m_isNeutered; } | 70 bool isNeutered() const { return m_isNeutered; } |
| 71 bool isShared() const { return m_contents.isShared(); } | 71 bool isShared() const { return m_contents.isShared(); } |
| 72 | 72 |
| 73 ~ArrayBuffer() { } | 73 ~ArrayBuffer() {} |
| 74 | 74 |
| 75 protected: | 75 protected: |
| 76 inline explicit ArrayBuffer(ArrayBufferContents&); | 76 inline explicit ArrayBuffer(ArrayBufferContents&); |
| 77 | 77 |
| 78 private: | 78 private: |
| 79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy); | 79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned el
ementByteSize, ArrayBufferContents::InitializationPolicy); |
| 80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns
igned elementByteSize, ArrayBufferContents::InitializationPolicy); | 80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, unsig
ned elementByteSize, ArrayBufferContents::InitializationPolicy); |
| 81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns
igned elementByteSize, ArrayBufferContents::InitializationPolicy); | 81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, unsig
ned elementByteSize, ArrayBufferContents::InitializationPolicy); |
| 82 | 82 |
| 83 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const
; | 83 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const; |
| 84 inline unsigned clampIndex(int index) const; | 84 inline unsigned clampIndex(int index) const; |
| 85 static inline int clampValue(int x, int left, int right); | 85 static inline int clampValue(int x, int left, int right); |
| 86 | 86 |
| 87 ArrayBufferContents m_contents; | 87 ArrayBufferContents m_contents; |
| 88 ArrayBufferView* m_firstView; | 88 ArrayBufferView* m_firstView; |
| 89 bool m_isNeutered; | 89 bool m_isNeutered; |
| 90 }; | 90 }; |
| 91 | 91 |
| 92 int ArrayBuffer::clampValue(int x, int left, int right) | 92 int ArrayBuffer::clampValue(int x, int left, int right) { |
| 93 { | 93 ASSERT(left <= right); |
| 94 ASSERT(left <= right); | 94 if (x < left) |
| 95 if (x < left) | 95 x = left; |
| 96 x = left; | 96 if (right < x) |
| 97 if (right < x) | 97 x = right; |
| 98 x = right; | 98 return x; |
| 99 return x; | |
| 100 } | 99 } |
| 101 | 100 |
| 102 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize) | 101 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize) { |
| 103 { | 102 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitializ
e); |
| 104 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial
ize); | |
| 105 } | 103 } |
| 106 | 104 |
| 107 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) | 105 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) { |
| 108 { | 106 // TODO(binji): support creating a SharedArrayBuffer by copying another ArrayB
uffer? |
| 109 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra
yBuffer? | 107 ASSERT(!other->isShared()); |
| 110 ASSERT(!other->isShared()); | 108 return ArrayBuffer::create(other->data(), other->byteLength()); |
| 111 return ArrayBuffer::create(other->data(), other->byteLength()); | |
| 112 } | 109 } |
| 113 | 110 |
| 114 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen
gth) | 111 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen
gth) { |
| 115 { | 112 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, Ar
rayBufferContents::ZeroInitialize); |
| 116 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared,
ArrayBufferContents::ZeroInitialize); | 113 RELEASE_ASSERT(contents.data()); |
| 117 RELEASE_ASSERT(contents.data()); | 114 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); |
| 118 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); | 115 memcpy(buffer->data(), source, byteLength); |
| 119 memcpy(buffer->data(), source, byteLength); | 116 return buffer.release(); |
| 120 return buffer.release(); | |
| 121 } | 117 } |
| 122 | 118 |
| 123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) | 119 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) { |
| 124 { | 120 RELEASE_ASSERT(contents.data()); |
| 125 RELEASE_ASSERT(contents.data()); | 121 return adoptRef(new ArrayBuffer(contents)); |
| 126 return adoptRef(new ArrayBuffer(contents)); | |
| 127 } | 122 } |
| 128 | 123 |
| 129 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize) | 124 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize) { |
| 130 { | 125 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroIni
tialize); |
| 131 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI
nitialize); | |
| 132 } | 126 } |
| 133 | 127 |
| 134 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u
nsigned elementByteSize) | 128 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u
nsigned elementByteSize) { |
| 135 { | 129 return create(numElements, elementByteSize, ArrayBufferContents::DontInitializ
e); |
| 136 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial
ize); | |
| 137 } | 130 } |
| 138 | 131 |
| 139 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize, ArrayBufferContents::InitializationPolicy policy) | 132 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme
ntByteSize, ArrayBufferContents::InitializationPolicy policy) { |
| 140 { | 133 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents
::NotShared, policy); |
| 141 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::NotShared, policy); | 134 RELEASE_ASSERT(contents.data()); |
| 142 RELEASE_ASSERT(contents.data()); | 135 return adoptRef(new ArrayBuffer(contents)); |
| 143 return adoptRef(new ArrayBuffer(contents)); | |
| 144 } | 136 } |
| 145 | 137 |
| 146 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) | 138 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) { |
| 147 { | 139 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents
::NotShared, policy); |
| 148 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::NotShared, policy); | 140 if (!contents.data()) |
| 149 if (!contents.data()) | 141 return nullptr; |
| 150 return nullptr; | 142 return adoptRef(new ArrayBuffer(contents)); |
| 151 return adoptRef(new ArrayBuffer(contents)); | |
| 152 } | 143 } |
| 153 | 144 |
| 154 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize) | 145 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize) { |
| 155 { | 146 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroIni
tialize); |
| 156 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI
nitialize); | |
| 157 } | 147 } |
| 158 | 148 |
| 159 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b
yteLength) | 149 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b
yteLength) { |
| 160 { | 150 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Array
BufferContents::ZeroInitialize); |
| 161 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr
ayBufferContents::ZeroInitialize); | 151 RELEASE_ASSERT(contents.data()); |
| 162 RELEASE_ASSERT(contents.data()); | 152 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); |
| 163 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); | 153 memcpy(buffer->data(), source, byteLength); |
| 164 memcpy(buffer->data(), source, byteLength); | 154 return buffer.release(); |
| 165 return buffer.release(); | |
| 166 } | 155 } |
| 167 | 156 |
| 168 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) | 157 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned
elementByteSize, ArrayBufferContents::InitializationPolicy policy) { |
| 169 { | 158 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferContents
::Shared, policy); |
| 170 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten
ts::Shared, policy); | 159 RELEASE_ASSERT(contents.data()); |
| 171 RELEASE_ASSERT(contents.data()); | 160 return adoptRef(new ArrayBuffer(contents)); |
| 172 return adoptRef(new ArrayBuffer(contents)); | |
| 173 } | 161 } |
| 174 | 162 |
| 175 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) | 163 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) |
| 176 : m_firstView(0), m_isNeutered(false) | 164 : m_firstView(0), m_isNeutered(false) { |
| 177 { | 165 if (contents.isShared()) |
| 178 if (contents.isShared()) | 166 contents.shareWith(m_contents); |
| 179 contents.shareWith(m_contents); | 167 else |
| 180 else | 168 contents.transfer(m_contents); |
| 181 contents.transfer(m_contents); | |
| 182 } | 169 } |
| 183 | 170 |
| 184 void* ArrayBuffer::data() | 171 void* ArrayBuffer::data() { |
| 185 { | 172 return m_contents.data(); |
| 186 return m_contents.data(); | |
| 187 } | 173 } |
| 188 | 174 |
| 189 const void* ArrayBuffer::data() const | 175 const void* ArrayBuffer::data() const { |
| 190 { | 176 return m_contents.data(); |
| 191 return m_contents.data(); | |
| 192 } | 177 } |
| 193 | 178 |
| 194 unsigned ArrayBuffer::byteLength() const | 179 unsigned ArrayBuffer::byteLength() const { |
| 195 { | 180 return m_contents.sizeInBytes(); |
| 196 return m_contents.sizeInBytes(); | |
| 197 } | 181 } |
| 198 | 182 |
| 199 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const | 183 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const { |
| 200 { | 184 return sliceImpl(clampIndex(begin), clampIndex(end)); |
| 201 return sliceImpl(clampIndex(begin), clampIndex(end)); | |
| 202 } | 185 } |
| 203 | 186 |
| 204 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const | 187 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const { |
| 205 { | 188 return sliceImpl(clampIndex(begin), byteLength()); |
| 206 return sliceImpl(clampIndex(begin), byteLength()); | |
| 207 } | 189 } |
| 208 | 190 |
| 209 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con
st | 191 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con
st { |
| 210 { | 192 unsigned size = begin <= end ? end - begin : 0; |
| 211 unsigned size = begin <= end ? end - begin : 0; | 193 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); |
| 212 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); | |
| 213 } | 194 } |
| 214 | 195 |
| 215 unsigned ArrayBuffer::clampIndex(int index) const | 196 unsigned ArrayBuffer::clampIndex(int index) const { |
| 216 { | 197 unsigned currentLength = byteLength(); |
| 217 unsigned currentLength = byteLength(); | 198 if (index < 0) |
| 218 if (index < 0) | 199 index = currentLength + index; |
| 219 index = currentLength + index; | 200 return clampValue(index, 0, currentLength); |
| 220 return clampValue(index, 0, currentLength); | |
| 221 } | 201 } |
| 222 | 202 |
| 223 } // namespace WTF | 203 } // namespace WTF |
| 224 | 204 |
| 225 using WTF::ArrayBuffer; | 205 using WTF::ArrayBuffer; |
| 226 | 206 |
| 227 #endif // ArrayBuffer_h | 207 #endif // ArrayBuffer_h |
| OLD | NEW |