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

Side by Side Diff: third_party/WebKit/Source/wtf/ArrayBuffer.h

Issue 1611343002: wtf reformat test Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: pydent Created 4 years, 11 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
OLDNEW
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
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,
43 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*); 43 unsigned elementByteSize);
44 static inline PassRefPtr<ArrayBuffer> create(const void* source, unsigned by teLength); 44 static inline PassRefPtr<ArrayBuffer> create(ArrayBuffer*);
45 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&); 45 static inline PassRefPtr<ArrayBuffer> create(const void* source,
46 46 unsigned byteLength);
47 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize); 47 static inline PassRefPtr<ArrayBuffer> create(ArrayBufferContents&);
48 48
49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serial izeObject 49 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements,
50 // (through DOMArrayBuffer::createUninitialized). 50 unsigned elementByteSize);
51 static inline PassRefPtr<ArrayBuffer> createUninitialized(unsigned numElemen ts, unsigned elementByteSize); 51
52 52 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serializ eObject
53 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize); 53 // (through DOMArrayBuffer::createUninitialized).
54 static inline PassRefPtr<ArrayBuffer> createShared(const void* source, unsig ned byteLength); 54 static inline PassRefPtr<ArrayBuffer> createUninitialized(
55 55 unsigned numElements,
56 inline void* data(); 56 unsigned elementByteSize);
57 inline const void* data() const; 57
58 inline unsigned byteLength() const; 58 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements,
59 59 unsigned elementByteSize);
60 // Creates a new ArrayBuffer object with copy of bytes in this object 60 static inline PassRefPtr<ArrayBuffer> createShared(const void* source,
61 // ranging from |begin| upto but not including |end|. 61 unsigned byteLength);
62 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; 62
63 inline PassRefPtr<ArrayBuffer> slice(int begin) const; 63 inline void* data();
64 64 inline const void* data() const;
65 void addView(ArrayBufferView*); 65 inline unsigned byteLength() const;
66 void removeView(ArrayBufferView*); 66
67 67 // Creates a new ArrayBuffer object with copy of bytes in this object
68 bool transfer(ArrayBufferContents&); 68 // ranging from |begin| upto but not including |end|.
69 bool shareContentsWith(ArrayBufferContents&); 69 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
70 bool isNeutered() const { return m_isNeutered; } 70 inline PassRefPtr<ArrayBuffer> slice(int begin) const;
71 bool isShared() const { return m_contents.isShared(); } 71
72 72 void addView(ArrayBufferView*);
73 ~ArrayBuffer() { } 73 void removeView(ArrayBufferView*);
74 74
75 protected: 75 bool transfer(ArrayBufferContents&);
76 inline explicit ArrayBuffer(ArrayBufferContents&); 76 bool shareContentsWith(ArrayBufferContents&);
77 77 bool isNeutered() const { return m_isNeutered; }
78 private: 78 bool isShared() const { return m_contents.isShared(); }
79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy); 79
80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); 80 ~ArrayBuffer() {}
81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); 81
82 82 protected:
83 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ; 83 inline explicit ArrayBuffer(ArrayBufferContents&);
84 inline unsigned clampIndex(int index) const; 84
85 static inline int clampValue(int x, int left, int right); 85 private:
86 86 static inline PassRefPtr<ArrayBuffer> create(
87 ArrayBufferContents m_contents; 87 unsigned numElements,
88 ArrayBufferView* m_firstView; 88 unsigned elementByteSize,
89 bool m_isNeutered; 89 ArrayBufferContents::InitializationPolicy);
90 static inline PassRefPtr<ArrayBuffer> createOrNull(
91 unsigned numElements,
92 unsigned elementByteSize,
93 ArrayBufferContents::InitializationPolicy);
94 static inline PassRefPtr<ArrayBuffer> createShared(
95 unsigned numElements,
96 unsigned elementByteSize,
97 ArrayBufferContents::InitializationPolicy);
98
99 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const;
100 inline unsigned clampIndex(int index) const;
101 static inline int clampValue(int x, int left, int right);
102
103 ArrayBufferContents m_contents;
104 ArrayBufferView* m_firstView;
105 bool m_isNeutered;
90 }; 106 };
91 107
92 int ArrayBuffer::clampValue(int x, int left, int right) 108 int ArrayBuffer::clampValue(int x, int left, int right) {
93 { 109 ASSERT(left <= right);
94 ASSERT(left <= right); 110 if (x < left)
95 if (x < left) 111 x = left;
96 x = left; 112 if (right < x)
97 if (right < x) 113 x = right;
98 x = right; 114 return x;
99 return x; 115 }
100 } 116
101 117 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements,
102 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize) 118 unsigned elementByteSize) {
103 { 119 return create(numElements, elementByteSize,
104 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize); 120 ArrayBufferContents::ZeroInitialize);
105 } 121 }
106 122
107 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) 123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) {
108 { 124 // TODO(binji): support creating a SharedArrayBuffer by copying another ArrayB uffer?
109 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra yBuffer? 125 ASSERT(!other->isShared());
110 ASSERT(!other->isShared()); 126 return ArrayBuffer::create(other->data(), other->byteLength());
111 return ArrayBuffer::create(other->data(), other->byteLength()); 127 }
112 } 128
113 129 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source,
114 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth) 130 unsigned byteLength) {
115 { 131 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared,
116 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, ArrayBufferContents::ZeroInitialize); 132 ArrayBufferContents::ZeroInitialize);
117 RELEASE_ASSERT(contents.data()); 133 RELEASE_ASSERT(contents.data());
118 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); 134 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
119 memcpy(buffer->data(), source, byteLength); 135 memcpy(buffer->data(), source, byteLength);
120 return buffer.release(); 136 return buffer.release();
121 } 137 }
122 138
123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) 139 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) {
124 { 140 RELEASE_ASSERT(contents.data());
125 RELEASE_ASSERT(contents.data()); 141 return adoptRef(new ArrayBuffer(contents));
126 return adoptRef(new ArrayBuffer(contents)); 142 }
127 } 143
128 144 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements,
129 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize) 145 unsigned elementByteSize) {
130 { 146 return createOrNull(numElements, elementByteSize,
131 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); 147 ArrayBufferContents::ZeroInitialize);
132 } 148 }
133 149
134 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize) 150 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(
135 { 151 unsigned numElements,
136 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize); 152 unsigned elementByteSize) {
137 } 153 return create(numElements, elementByteSize,
138 154 ArrayBufferContents::DontInitialize);
139 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy) 155 }
140 { 156
141 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy); 157 PassRefPtr<ArrayBuffer> ArrayBuffer::create(
142 RELEASE_ASSERT(contents.data()); 158 unsigned numElements,
143 return adoptRef(new ArrayBuffer(contents)); 159 unsigned elementByteSize,
144 } 160 ArrayBufferContents::InitializationPolicy policy) {
145 161 ArrayBufferContents contents(numElements, elementByteSize,
146 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) 162 ArrayBufferContents::NotShared, policy);
147 { 163 RELEASE_ASSERT(contents.data());
148 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy); 164 return adoptRef(new ArrayBuffer(contents));
149 if (!contents.data()) 165 }
150 return nullptr; 166
151 return adoptRef(new ArrayBuffer(contents)); 167 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(
152 } 168 unsigned numElements,
153 169 unsigned elementByteSize,
154 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize) 170 ArrayBufferContents::InitializationPolicy policy) {
155 { 171 ArrayBufferContents contents(numElements, elementByteSize,
156 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); 172 ArrayBufferContents::NotShared, policy);
157 } 173 if (!contents.data())
158 174 return nullptr;
159 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b yteLength) 175 return adoptRef(new ArrayBuffer(contents));
160 { 176 }
161 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr ayBufferContents::ZeroInitialize); 177
162 RELEASE_ASSERT(contents.data()); 178 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements,
163 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); 179 unsigned elementByteSize) {
164 memcpy(buffer->data(), source, byteLength); 180 return createShared(numElements, elementByteSize,
165 return buffer.release(); 181 ArrayBufferContents::ZeroInitialize);
166 } 182 }
167 183
168 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) 184 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source,
169 { 185 unsigned byteLength) {
170 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::Shared, policy); 186 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared,
171 RELEASE_ASSERT(contents.data()); 187 ArrayBufferContents::ZeroInitialize);
172 return adoptRef(new ArrayBuffer(contents)); 188 RELEASE_ASSERT(contents.data());
189 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
190 memcpy(buffer->data(), source, byteLength);
191 return buffer.release();
192 }
193
194 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(
195 unsigned numElements,
196 unsigned elementByteSize,
197 ArrayBufferContents::InitializationPolicy policy) {
198 ArrayBufferContents contents(numElements, elementByteSize,
199 ArrayBufferContents::Shared, policy);
200 RELEASE_ASSERT(contents.data());
201 return adoptRef(new ArrayBuffer(contents));
173 } 202 }
174 203
175 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) 204 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
176 : m_firstView(0), m_isNeutered(false) 205 : m_firstView(0), m_isNeutered(false) {
177 { 206 if (contents.isShared())
178 if (contents.isShared()) 207 contents.shareWith(m_contents);
179 contents.shareWith(m_contents); 208 else
180 else 209 contents.transfer(m_contents);
181 contents.transfer(m_contents); 210 }
182 } 211
183 212 void* ArrayBuffer::data() {
184 void* ArrayBuffer::data() 213 return m_contents.data();
185 { 214 }
186 return m_contents.data(); 215
187 } 216 const void* ArrayBuffer::data() const {
188 217 return m_contents.data();
189 const void* ArrayBuffer::data() const 218 }
190 { 219
191 return m_contents.data(); 220 unsigned ArrayBuffer::byteLength() const {
192 } 221 return m_contents.sizeInBytes();
193 222 }
194 unsigned ArrayBuffer::byteLength() const 223
195 { 224 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const {
196 return m_contents.sizeInBytes(); 225 return sliceImpl(clampIndex(begin), clampIndex(end));
197 } 226 }
198 227
199 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const 228 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const {
200 { 229 return sliceImpl(clampIndex(begin), byteLength());
201 return sliceImpl(clampIndex(begin), clampIndex(end)); 230 }
202 } 231
203 232 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin,
204 PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const 233 unsigned end) const {
205 { 234 unsigned size = begin <= end ? end - begin : 0;
206 return sliceImpl(clampIndex(begin), byteLength()); 235 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
207 } 236 }
208 237
209 PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) con st 238 unsigned ArrayBuffer::clampIndex(int index) const {
210 { 239 unsigned currentLength = byteLength();
211 unsigned size = begin <= end ? end - begin : 0; 240 if (index < 0)
212 return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size); 241 index = currentLength + index;
213 } 242 return clampValue(index, 0, currentLength);
214 243 }
215 unsigned ArrayBuffer::clampIndex(int index) const 244
216 { 245 } // namespace WTF
217 unsigned currentLength = byteLength();
218 if (index < 0)
219 index = currentLength + index;
220 return clampValue(index, 0, currentLength);
221 }
222
223 } // namespace WTF
224 246
225 using WTF::ArrayBuffer; 247 using WTF::ArrayBuffer;
226 248
227 #endif // ArrayBuffer_h 249 #endif // ArrayBuffer_h
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Allocator.h ('k') | third_party/WebKit/Source/wtf/ArrayBuffer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698