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

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: type in ArrayBuffer::shareContentsWith 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 shareContentsWith(ArrayBufferContents&);
70 bool isNeutered() const { return m_isNeutered; }
71 bool isShared() const { return m_contents.isShared(); }
67 72
68 ~ArrayBuffer() { } 73 ~ArrayBuffer() { }
69 74
70 protected: 75 protected:
71 inline explicit ArrayBuffer(ArrayBufferContents&); 76 inline explicit ArrayBuffer(ArrayBufferContents&);
72 77
73 private: 78 private:
74 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy); 79 static inline PassRefPtr<ArrayBuffer> create(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy);
75 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy); 80 static inline PassRefPtr<ArrayBuffer> createOrNull(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy);
81 static inline PassRefPtr<ArrayBuffer> createShared(unsigned numElements, uns igned elementByteSize, ArrayBufferContents::InitializationPolicy);
76 82
77 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ; 83 inline PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const ;
78 inline unsigned clampIndex(int index) const; 84 inline unsigned clampIndex(int index) const;
79 static inline int clampValue(int x, int left, int right); 85 static inline int clampValue(int x, int left, int right);
80 86
81 ArrayBufferContents m_contents; 87 ArrayBufferContents m_contents;
82 ArrayBufferView* m_firstView; 88 ArrayBufferView* m_firstView;
83 bool m_isNeutered; 89 bool m_isNeutered;
84 }; 90 };
85 91
86 int ArrayBuffer::clampValue(int x, int left, int right) 92 int ArrayBuffer::clampValue(int x, int left, int right)
87 { 93 {
88 ASSERT(left <= right); 94 ASSERT(left <= right);
89 if (x < left) 95 if (x < left)
90 x = left; 96 x = left;
91 if (right < x) 97 if (right < x)
92 x = right; 98 x = right;
93 return x; 99 return x;
94 } 100 }
95 101
96 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize) 102 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize)
97 { 103 {
98 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize); 104 return create(numElements, elementByteSize, ArrayBufferContents::ZeroInitial ize);
99 } 105 }
100 106
101 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other) 107 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBuffer* other)
102 { 108 {
109 // TODO(binji): support creating a SharedArrayBuffer by copying another Arra yBuffer?
110 ASSERT(!other->isShared());
103 return ArrayBuffer::create(other->data(), other->byteLength()); 111 return ArrayBuffer::create(other->data(), other->byteLength());
104 } 112 }
105 113
106 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth) 114 PassRefPtr<ArrayBuffer> ArrayBuffer::create(const void* source, unsigned byteLen gth)
107 { 115 {
108 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::ZeroInitial ize); 116 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::NotShared, ArrayBufferContents::ZeroInitialize);
109 RELEASE_ASSERT(contents.data()); 117 RELEASE_ASSERT(contents.data());
110 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents)); 118 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
111 memcpy(buffer->data(), source, byteLength); 119 memcpy(buffer->data(), source, byteLength);
112 return buffer.release(); 120 return buffer.release();
113 } 121 }
114 122
115 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents) 123 PassRefPtr<ArrayBuffer> ArrayBuffer::create(ArrayBufferContents& contents)
116 { 124 {
117 return adoptRef(new ArrayBuffer(contents)); 125 return adoptRef(new ArrayBuffer(contents));
118 } 126 }
119 127
120 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize) 128 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize)
121 { 129 {
122 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize); 130 return createOrNull(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize);
123 } 131 }
124 132
125 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize) 133 PassRefPtr<ArrayBuffer> ArrayBuffer::createUninitialized(unsigned numElements, u nsigned elementByteSize)
126 { 134 {
127 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize); 135 return create(numElements, elementByteSize, ArrayBufferContents::DontInitial ize);
128 } 136 }
129 137
130 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy) 138 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned eleme ntByteSize, ArrayBufferContents::InitializationPolicy policy)
131 { 139 {
132 ArrayBufferContents contents(numElements, elementByteSize, policy); 140 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy);
133 RELEASE_ASSERT(contents.data()); 141 RELEASE_ASSERT(contents.data());
134 return adoptRef(new ArrayBuffer(contents)); 142 return adoptRef(new ArrayBuffer(contents));
135 } 143 }
136 144
137 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy) 145 PassRefPtr<ArrayBuffer> ArrayBuffer::createOrNull(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
138 { 146 {
139 ArrayBufferContents contents(numElements, elementByteSize, policy); 147 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::NotShared, policy);
140 if (!contents.data()) 148 if (!contents.data())
141 return nullptr; 149 return nullptr;
142 return adoptRef(new ArrayBuffer(contents)); 150 return adoptRef(new ArrayBuffer(contents));
143 } 151 }
144 152
153 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize)
154 {
155 return createShared(numElements, elementByteSize, ArrayBufferContents::ZeroI nitialize);
156 }
157
158 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(const void* source, unsigned b yteLength)
159 {
160 ArrayBufferContents contents(byteLength, 1, ArrayBufferContents::Shared, Arr ayBufferContents::ZeroInitialize);
161 RELEASE_ASSERT(contents.data());
162 RefPtr<ArrayBuffer> buffer = adoptRef(new ArrayBuffer(contents));
163 memcpy(buffer->data(), source, byteLength);
164 return buffer.release();
165 }
166
167 PassRefPtr<ArrayBuffer> ArrayBuffer::createShared(unsigned numElements, unsigned elementByteSize, ArrayBufferContents::InitializationPolicy policy)
168 {
169 ArrayBufferContents contents(numElements, elementByteSize, ArrayBufferConten ts::Shared, policy);
170 RELEASE_ASSERT(contents.data());
171 return adoptRef(new ArrayBuffer(contents));
172 }
173
145 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents) 174 ArrayBuffer::ArrayBuffer(ArrayBufferContents& contents)
146 : m_firstView(0), m_isNeutered(false) 175 : m_firstView(0), m_isNeutered(false)
147 { 176 {
148 contents.transfer(m_contents); 177 contents.transfer(m_contents);
149 } 178 }
150 179
151 void* ArrayBuffer::data() 180 void* ArrayBuffer::data()
152 { 181 {
153 return m_contents.data(); 182 return m_contents.data();
154 } 183 }
(...skipping 30 matching lines...) Expand all
185 if (index < 0) 214 if (index < 0)
186 index = currentLength + index; 215 index = currentLength + index;
187 return clampValue(index, 0, currentLength); 216 return clampValue(index, 0, currentLength);
188 } 217 }
189 218
190 } // namespace WTF 219 } // namespace WTF
191 220
192 using WTF::ArrayBuffer; 221 using WTF::ArrayBuffer;
193 222
194 #endif // ArrayBuffer_h 223 #endif // ArrayBuffer_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698