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

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

Issue 1186863005: Add a ref-counted data holder to ArrayBufferContents (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 6 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 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 XMLHttpRequest::responseArrayBuffer and Internals::serial izeObject 49 // Only for use by XMLHttpRequest::responseArrayBuffer and Internals::serial izeObject
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 numElemen ts, unsigned elementByteSize);
52 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
53 inline void* data(); 56 inline void* data();
54 inline const void* data() const; 57 inline const void* data() const;
55 inline unsigned byteLength() const; 58 inline unsigned byteLength() const;
56 59
57 // 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
58 // ranging from |begin| upto but not including |end|. 61 // ranging from |begin| upto but not including |end|.
59 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const; 62 inline PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
60 inline PassRefPtr<ArrayBuffer> slice(int begin) const; 63 inline PassRefPtr<ArrayBuffer> slice(int begin) const;
61 64
62 void addView(ArrayBufferView*); 65 void addView(ArrayBufferView*);
63 void removeView(ArrayBufferView*); 66 void removeView(ArrayBufferView*);
64 67
65 bool transfer(ArrayBufferContents&); 68 bool transfer(ArrayBufferContents&);
66 bool isNeutered() { return m_isNeutered; } 69 bool isNeutered() const { return m_isNeutered; }
70 bool isShared() const { return m_contents.isShared(); }
67 71
68 ~ArrayBuffer() { } 72 ~ArrayBuffer() { }
69 73
70 protected: 74 protected:
71 inline explicit ArrayBuffer(ArrayBufferContents&); 75 inline explicit ArrayBuffer(ArrayBufferContents&);
72 76
73 private: 77 private:
74 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy); 78 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
75 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); 79 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy);
80 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy);
76 81
77 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ; 82 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ;
78 inline unsigned clampIndex(int index) const; 83 inline unsigned clampIndex(int index) const;
79 static inline int clampValue(int x, int left, int right); 84 static inline int clampValue(int x, int left, int right);
80 85
81 ArrayBufferContents m_contents; 86 ArrayBufferContents m_contents;
82 ArrayBufferView* m_firstView; 87 ArrayBufferView* m_firstView;
83 bool m_isNeutered; 88 bool m_isNeutered;
84 }; 89 };
85 90
86 int ArrayBuffer::clampValue(int x, int left, int right) 91 int ArrayBuffer::clampValue(int x, int left, int right)
87 { 92 {
88 ASSERT(left <= right); 93 ASSERT(left <= right);
89 if (x < left) 94 if (x < left)
90 x = left; 95 x = left;
91 if (right < x) 96 if (right < x)
92 x = right; 97 x = right;
93 return x; 98 return x;
94 } 99 }
95 100
96 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize) 101 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize)
97 { 102 {
98 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize); 103 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize);
99 } 104 }
100 105
101 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) 106 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other)
102 { 107 {
108 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra yBuffer?
109 ASSERT(!other->isShared());
103 return ArrayBuffer::create(other->data(), other->byteLength()); 110 return ArrayBuffer::create(other->data(), other->byteLength());
104 } 111 }
105 112
106 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth) 113 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth)
107 { 114 {
108 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::ZeroInitial ize); 115 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, ArrayBufferContents::ZeroInitialize);
109 RELEASE_ASSERT(contents.data()); 116 RELEASE_ASSERT(contents.data());
110 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); 117 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
111 memcpy(buffer->data(), source, byteLength); 118 memcpy(buffer->data(), source, byteLength);
112 return buffer.release(); 119 return buffer.release();
113 } 120 }
114 121
115 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) 122 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents)
116 { 123 {
117 return adoptRef(new ArrayBuffer(contents)); 124 return adoptRef(new ArrayBuffer(contents));
118 } 125 }
119 126
120 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize) 127 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize)
121 { 128 {
122 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); 129 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize);
123 } 130 }
124 131
125 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize) 132 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize)
126 { 133 {
127 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize); 134 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize);
128 } 135 }
129 136
130 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy) 137 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy)
131 { 138 {
132 ArrayBufferContents contents(numElements, elementByteSize, policy); 139 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy);
133 RELEASE_ASSERT(contents.data()); 140 RELEASE_ASSERT(contents.data());
134 return adoptRef(new ArrayBuffer(contents)); 141 return adoptRef(new ArrayBuffer(contents));
135 } 142 }
136 143
137 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) 144 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
138 { 145 {
139 ArrayBufferContents contents(numElements, elementByteSize, policy); 146 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy);
140 if (!contents.data()) 147 if (!contents.data())
141 return nullptr; 148 return nullptr;
142 return adoptRef(new ArrayBuffer(contents)); 149 return adoptRef(new ArrayBuffer(contents));
143 } 150 }
144 151
152 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize)
153 {
154 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize);
155 }
156
157 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b yteLength)
158 {
159 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr ayBufferContents::ZeroInitialize);
160 RELEASE_ASSERT(contents.data());
161 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
162 memcpy(buffer->data(), source, byteLength);
163 return buffer.release();
164 }
165
166 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
167 {
168 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::Shared, policy);
169 RELEASE_ASSERT(contents.data());
170 return adoptRef(new ArrayBuffer(contents));
171 }
172
145 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) 173 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
146 : m_firstView(0), m_isNeutered(false) 174 : m_firstView(0), m_isNeutered(false)
147 { 175 {
148 contents.transfer(m_contents); 176 contents.transfer(m_contents);
149 } 177 }
150 178
151 void* ArrayBuffer::data() 179 void* ArrayBuffer::data()
152 { 180 {
153 return m_contents.data(); 181 return m_contents.data();
154 } 182 }
(...skipping 30 matching lines...) Expand all
185 if (index < 0) 213 if (index < 0)
186 index = currentLength + index; 214 index = currentLength + index;
187 return clampValue(index, 0, currentLength); 215 return clampValue(index, 0, currentLength);
188 } 216 }
189 217
190 } // namespace WTF 218 } // namespace WTF
191 219
192 using WTF::ArrayBuffer; 220 using WTF::ArrayBuffer;
193 221
194 #endif // ArrayBuffer_h 222 #endif // ArrayBuffer_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698