Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(467)

Side by Side Diff: Source/WTF/wtf/ArrayBuffer.h

Issue 14238015: Move Source/WTF/wtf to Source/wtf (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/HashSet.h>
30 #include <wtf/PassRefPtr.h>
31 #include <wtf/RefCounted.h>
32 #include <wtf/Vector.h>
33
34 namespace WTF {
35
36 class ArrayBuffer;
37 class ArrayBufferView;
38
39 // The current implementation assumes that the instance of this class is a
40 // singleton living for the entire process's lifetime.
41 class ArrayBufferDeallocationObserver {
42 public:
43 virtual void ArrayBufferDeallocated(unsigned sizeInBytes) = 0;
44 };
45
46
47 class ArrayBufferContents {
48 WTF_MAKE_NONCOPYABLE(ArrayBufferContents);
49 public:
50 ArrayBufferContents()
51 : m_data(0)
52 , m_sizeInBytes(0)
53 , m_deallocationObserver(0)
54 { }
55
56 inline ~ArrayBufferContents();
57
58 void* data() { return m_data; }
59 unsigned sizeInBytes() { return m_sizeInBytes; }
60
61 private:
62 ArrayBufferContents(void* data, unsigned sizeInBytes)
63 : m_data(data)
64 , m_sizeInBytes(sizeInBytes)
65 , m_deallocationObserver(0)
66 { }
67
68 friend class ArrayBuffer;
69
70 enum InitializationPolicy {
71 ZeroInitialize,
72 DontInitialize
73 };
74
75 static inline void tryAllocate(unsigned numElements, unsigned elementByteSiz e, InitializationPolicy, ArrayBufferContents&);
76 void transfer(ArrayBufferContents& other)
77 {
78 ASSERT(!other.m_data);
79 other.m_data = m_data;
80 other.m_sizeInBytes = m_sizeInBytes;
81 m_data = 0;
82 m_sizeInBytes = 0;
83 // Notify the current V8 isolate that the buffer is gone.
84 if (m_deallocationObserver)
85 m_deallocationObserver->ArrayBufferDeallocated(other.m_sizeInBytes);
86 ASSERT(!other.m_deallocationObserver);
87 m_deallocationObserver = 0;
88 }
89
90 void* m_data;
91 unsigned m_sizeInBytes;
92
93 ArrayBufferDeallocationObserver* m_deallocationObserver;
94 };
95
96 class ArrayBuffer : public RefCounted<ArrayBuffer> {
97 public:
98 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize);
99 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*);
100 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned by teLength);
101 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&);
102
103 // Only for use by Uint8ClampedArray::createUninitialized.
104 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen ts, unsigned elementByteSize);
105
106 inline void* data();
107 inline const void* data() const;
108 inline unsigned byteLength() const;
109
110 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
111 inline PassRefPtr<ArrayBuffer> slice(int begin) const;
112
113 void addView(ArrayBufferView*);
114 void removeView(ArrayBufferView*);
115
116 WTF_EXPORT_PRIVATE bool transfer(ArrayBufferContents&, Vector<RefPtr<ArrayBu fferView> >& neuteredViews);
117 bool isNeutered() { return !m_contents.m_data; }
118
119 void setDeallocationObserver(ArrayBufferDeallocationObserver* deallocationOb server)
120 {
121 m_contents.m_deallocationObserver = deallocationObserver;
122 }
123
124 ~ArrayBuffer() { }
125
126 private:
127 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
128
129 inline ArrayBuffer(ArrayBufferContents&);
130 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ;
131 inline unsigned clampIndex(int index) const;
132 static inline int clampValue(int x, int left, int right);
133
134 ArrayBufferContents m_contents;
135 ArrayBufferView* m_firstView;
136 };
137
138 int ArrayBuffer::clampValue(int x, int left, int right)
139 {
140 ASSERT(left <= right);
141 if (x < left)
142 x = left;
143 if (right < x)
144 x = right;
145 return x;
146 }
147
148 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize)
149 {
150 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize);
151 }
152
153 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other)
154 {
155 return ArrayBuffer::create(other->data(), other->byteLength());
156 }
157
158 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth)
159 {
160 ArrayBufferContents contents;
161 ArrayBufferContents::tryAllocate(byteLength, 1, ArrayBufferContents::ZeroIni tialize, contents);
162 if (!contents.m_data)
163 return 0;
164 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
165 memcpy(buffer->data(), source, byteLength);
166 return buffer.release();
167 }
168
169 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents)
170 {
171 return adoptRef(new ArrayBuffer(contents));
172 }
173
174 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize)
175 {
176 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize);
177 }
178
179 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy)
180 {
181 ArrayBufferContents contents;
182 ArrayBufferContents::tryAllocate(numElements, elementByteSize, policy, conte nts);
183 if (!contents.m_data)
184 return 0;
185 return adoptRef(new ArrayBuffer(contents));
186 }
187
188 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
189 : m_firstView(0)
190 {
191 contents.transfer(m_contents);
192 }
193
194 void* ArrayBuffer::data()
195 {
196 return m_contents.m_data;
197 }
198
199 const void* ArrayBuffer::data() const
200 {
201 return m_contents.m_data;
202 }
203
204 unsigned ArrayBuffer::byteLength() const
205 {
206 return m_contents.m_sizeInBytes;
207 }
208
209 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
210 {
211 return sliceImpl(clampIndex(begin), clampIndex(end));
212 }
213
214 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
215 {
216 return sliceImpl(clampIndex(begin), byteLength());
217 }
218
219 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con st
220 {
221 unsigned size = begin <= end ? end - begin : 0;
222 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
223 }
224
225 unsigned ArrayBuffer::clampIndex(int index) const
226 {
227 unsigned currentLength = byteLength();
228 if (index < 0)
229 index = currentLength + index;
230 return clampValue(index, 0, currentLength);
231 }
232
233 void ArrayBufferContents::tryAllocate(unsigned numElements, unsigned elementByte Size, ArrayBufferContents::InitializationPolicy policy, ArrayBufferContents& res ult)
234 {
235 // Do not allow 32-bit overflow of the total size.
236 // FIXME: Why not? The tryFastCalloc function already checks its arguments,
237 // and will fail if there is any overflow, so why should we include a
238 // redudant unnecessarily restrictive check here?
239 if (numElements) {
240 unsigned totalSize = numElements * elementByteSize;
241 if (totalSize / numElements != elementByteSize) {
242 result.m_data = 0;
243 return;
244 }
245 }
246 bool allocationSucceeded = false;
247 if (policy == ZeroInitialize)
248 allocationSucceeded = WTF::tryFastCalloc(numElements, elementByteSize).g etValue(result.m_data);
249 else {
250 ASSERT(policy == DontInitialize);
251 allocationSucceeded = WTF::tryFastMalloc(numElements * elementByteSize). getValue(result.m_data);
252 }
253
254 if (allocationSucceeded) {
255 result.m_sizeInBytes = numElements * elementByteSize;
256 return;
257 }
258 result.m_data = 0;
259 }
260
261 ArrayBufferContents::~ArrayBufferContents()
262 {
263 if (m_deallocationObserver)
264 m_deallocationObserver->ArrayBufferDeallocated(m_sizeInBytes);
265 WTF::fastFree(m_data);
266 }
267
268 } // namespace WTF
269
270 using WTF::ArrayBuffer;
271
272 #endif // ArrayBuffer_h
OLDNEW
« no previous file with comments | « Source/WTF/wtf/Alignment.h ('k') | Source/WTF/wtf/ArrayBuffer.cpp » ('j') | Source/config.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698