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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned by teLength); | 44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned by teLength); |
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, uns igned elementByteSize); |
48 | 48 |
49 // Only for use by Uint8ClampedArray::createUninitialized, | 49 // Only for use by Uint8ClampedArray::createUninitialized, |
50 // XMLHttpRequest::responseArrayBuffer and Internals::serializeObject | 50 // XMLHttpRequest::responseArrayBuffer and Internals::serializeObject |
51 // (through DOMArrayBuffer::createUninitialized). | 51 // (through DOMArrayBuffer::createUninitialized). |
52 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen ts, unsigned elementByteSize); | 52 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen ts, unsigned elementByteSize); |
53 | 53 |
54 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize); | |
55 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsig ned byteLength); | |
56 | |
54 inline void* data(); | 57 inline void* data(); |
55 inline const void* data() const; | 58 inline const void* data() const; |
56 inline unsigned byteLength() const; | 59 inline unsigned byteLength() const; |
57 | 60 |
58 // Creates a new ArrayBuffer object with copy of bytes in this object | 61 // Creates a new ArrayBuffer object with copy of bytes in this object |
59 // ranging from |begin| upto but not including |end|. | 62 // ranging from |begin| upto but not including |end|. |
60 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; | 63 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; |
61 inline PassRefPtr<ArrayBuffer> slice(int begin) const; | 64 inline PassRefPtr<ArrayBuffer> slice(int begin) const; |
62 | 65 |
63 void addView(ArrayBufferView*); | 66 void addView(ArrayBufferView*); |
64 void removeView(ArrayBufferView*); | 67 void removeView(ArrayBufferView*); |
65 | 68 |
66 bool transfer(ArrayBufferContents&); | 69 bool transfer(ArrayBufferContents&); |
67 bool isNeutered() { return m_isNeutered; } | 70 bool isNeutered() const { return m_isNeutered; } |
71 bool isShared() const { return m_contents.isShared(); } | |
68 | 72 |
69 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer) | 73 void setDeallocationObserver(ArrayBufferDeallocationObserver& observer) |
70 { | 74 { |
71 m_contents.setDeallocationObserver(observer); | 75 m_contents.setDeallocationObserver(observer); |
72 } | 76 } |
73 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer) | 77 void setDeallocationObserverWithoutAllocationNotification(ArrayBufferDealloc ationObserver& observer) |
74 { | 78 { |
75 m_contents.setDeallocationObserverWithoutAllocationNotification(observer ); | 79 m_contents.setDeallocationObserverWithoutAllocationNotification(observer ); |
76 } | 80 } |
77 | 81 |
78 ~ArrayBuffer() { } | 82 ~ArrayBuffer() { } |
79 | 83 |
80 protected: | 84 protected: |
81 inline explicit ArrayBuffer(ArrayBufferContents&); | 85 inline explicit ArrayBuffer(ArrayBufferContents&); |
82 | 86 |
83 private: | 87 private: |
84 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy); | 88 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy); |
85 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); | 89 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); |
90 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); | |
86 | 91 |
87 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ; | 92 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ; |
88 inline unsigned clampIndex(int index) const; | 93 inline unsigned clampIndex(int index) const; |
89 static inline int clampValue(int x, int left, int right); | 94 static inline int clampValue(int x, int left, int right); |
90 | 95 |
91 ArrayBufferContents m_contents; | 96 ArrayBufferContents m_contents; |
92 ArrayBufferView* m_firstView; | 97 ArrayBufferView* m_firstView; |
93 bool m_isNeutered; | 98 bool m_isNeutered; |
94 }; | 99 }; |
95 | 100 |
96 int ArrayBuffer::clampValue(int x, int left, int right) | 101 int ArrayBuffer::clampValue(int x, int left, int right) |
97 { | 102 { |
98 ASSERT(left <= right); | 103 ASSERT(left <= right); |
99 if (x < left) | 104 if (x < left) |
100 x = left; | 105 x = left; |
101 if (right < x) | 106 if (right < x) |
102 x = right; | 107 x = right; |
103 return x; | 108 return x; |
104 } | 109 } |
105 | 110 |
106 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize) | 111 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize) |
107 { | 112 { |
108 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize); | 113 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize); |
109 } | 114 } |
110 | 115 |
111 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) | 116 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) |
112 { | 117 { |
118 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra yBuffer? | |
119 ASSERT(!other->isShared()); | |
113 return ArrayBuffer::create(other->data(), other->byteLength()); | 120 return ArrayBuffer::create(other->data(), other->byteLength()); |
114 } | 121 } |
115 | 122 |
116 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth) | 123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth) |
117 { | 124 { |
118 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::ZeroInitial ize); | 125 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, ArrayBufferContents::ZeroInitialize); |
119 RELEASE_ASSERT(contents.data()); | 126 RELEASE_ASSERT(contents.data()); |
120 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); | 127 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); |
121 memcpy(buffer->data(), source, byteLength); | 128 memcpy(buffer->data(), source, byteLength); |
122 return buffer.release(); | 129 return buffer.release(); |
123 } | 130 } |
124 | 131 |
125 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) | 132 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) |
126 { | 133 { |
127 return adoptRef(new ArrayBuffer(contents)); | 134 return adoptRef(new ArrayBuffer(contents)); |
128 } | 135 } |
129 | 136 |
130 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize) | 137 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize) |
131 { | 138 { |
132 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); | 139 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); |
133 } | 140 } |
134 | 141 |
135 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize) | 142 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize) |
136 { | 143 { |
137 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize); | 144 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize); |
138 } | 145 } |
139 | 146 |
140 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy) | 147 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy) |
141 { | 148 { |
142 ArrayBufferContents contents(numElements, elementByteSize, policy); | 149 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy); |
143 RELEASE_ASSERT(contents.data()); | 150 RELEASE_ASSERT(contents.data()); |
144 return adoptRef(new ArrayBuffer(contents)); | 151 return adoptRef(new ArrayBuffer(contents)); |
145 } | 152 } |
146 | 153 |
147 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) | 154 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) |
148 { | 155 { |
149 ArrayBufferContents contents(numElements, elementByteSize, policy); | 156 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy); |
150 if (!contents.data()) | 157 if (!contents.data()) |
151 return nullptr; | 158 return nullptr; |
152 return adoptRef(new ArrayBuffer(contents)); | 159 return adoptRef(new ArrayBuffer(contents)); |
153 } | 160 } |
154 | 161 |
162 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize) | |
163 { | |
164 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); | |
165 } | |
166 | |
167 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b yteLength) | |
168 { | |
169 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr ayBufferContents::ZeroInitialize); | |
170 RELEASE_ASSERT(contents.data()); | |
haraken
2015/06/11 05:59:29
Any reason to make this a RELEASE assert?
haraken
2015/06/11 05:59:29
Shall we add ASSERT(contents.isShared())?
| |
171 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); | |
172 memcpy(buffer->data(), source, byteLength); | |
173 return buffer.release(); | |
174 } | |
175 | |
176 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) | |
177 { | |
178 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::Shared, policy); | |
179 RELEASE_ASSERT(contents.data()); | |
haraken
2015/06/11 05:59:29
Ditto.
| |
180 return adoptRef(new ArrayBuffer(contents)); | |
181 } | |
182 | |
155 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) | 183 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) |
156 : m_firstView(0), m_isNeutered(false) | 184 : m_firstView(0), m_isNeutered(false) |
157 { | 185 { |
158 contents.transfer(m_contents); | 186 contents.transfer(m_contents); |
159 } | 187 } |
160 | 188 |
161 void* ArrayBuffer::data() | 189 void* ArrayBuffer::data() |
162 { | 190 { |
163 return m_contents.data(); | 191 return m_contents.data(); |
164 } | 192 } |
(...skipping 13 matching lines...) Expand all Loading... | |
178 return sliceImpl(clampIndex(begin), clampIndex(end)); | 206 return sliceImpl(clampIndex(begin), clampIndex(end)); |
179 } | 207 } |
180 | 208 |
181 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const | 209 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const |
182 { | 210 { |
183 return sliceImpl(clampIndex(begin), byteLength()); | 211 return sliceImpl(clampIndex(begin), byteLength()); |
184 } | 212 } |
185 | 213 |
186 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con st | 214 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con st |
187 { | 215 { |
216 // TODO(binji): Isn't supported by SharedArrayBuffer, but maybe we don't nee d to assert? | |
217 // If we do support this, make sure to call createShared instead of create b elow. | |
haraken
2015/06/11 05:59:29
I'm just curious but do we need to make the sliced
| |
218 ASSERT(!isShared()); | |
188 unsigned size = begin <= end ? end - begin : 0; | 219 unsigned size = begin <= end ? end - begin : 0; |
189 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); | 220 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); |
190 } | 221 } |
191 | 222 |
192 unsigned ArrayBuffer::clampIndex(int index) const | 223 unsigned ArrayBuffer::clampIndex(int index) const |
193 { | 224 { |
194 unsigned currentLength = byteLength(); | 225 unsigned currentLength = byteLength(); |
195 if (index < 0) | 226 if (index < 0) |
196 index = currentLength + index; | 227 index = currentLength + index; |
197 return clampValue(index, 0, currentLength); | 228 return clampValue(index, 0, currentLength); |
198 } | 229 } |
199 | 230 |
200 } // namespace WTF | 231 } // namespace WTF |
201 | 232 |
202 using WTF::ArrayBuffer; | 233 using WTF::ArrayBuffer; |
203 | 234 |
204 #endif // ArrayBuffer_h | 235 #endif // ArrayBuffer_h |
OLD | NEW |