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

Side by Side Diff: src/core/SkRWBuffer.cpp

Issue 1872853002: Fix race condition in SkROBuffer. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: use constructors to make fCapacity const Created 4 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
« no previous file with comments | « include/core/SkRWBuffer.h ('k') | tests/DataRefTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2015 Google Inc. 2 * Copyright 2015 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkRWBuffer.h" 8 #include "SkRWBuffer.h"
9 #include "SkStream.h" 9 #include "SkStream.h"
10 10
11 // Force small chunks to be a page's worth 11 // Force small chunks to be a page's worth
12 static const size_t kMinAllocSize = 4096; 12 static const size_t kMinAllocSize = 4096;
13 13
14 struct SkBufferBlock { 14 struct SkBufferBlock {
15 SkBufferBlock* fNext; 15 SkBufferBlock* fNext; // updated by the writer
16 size_t fUsed; 16 size_t fUsed; // updated by the writer
17 size_t fCapacity; 17 const size_t fCapacity;
18
19 SkBufferBlock(size_t capacity) : fNext(nullptr), fUsed(0), fCapacity(capacit y) {}
18 20
19 const void* startData() const { return this + 1; }; 21 const void* startData() const { return this + 1; };
20 22
21 size_t avail() const { return fCapacity - fUsed; } 23 size_t avail() const { return fCapacity - fUsed; }
22 void* availData() { return (char*)this->startData() + fUsed; } 24 void* availData() { return (char*)this->startData() + fUsed; }
23 25
24 static SkBufferBlock* Alloc(size_t length) { 26 static SkBufferBlock* Alloc(size_t length) {
25 size_t capacity = LengthToCapacity(length); 27 size_t capacity = LengthToCapacity(length);
26 SkBufferBlock* block = (SkBufferBlock*)sk_malloc_throw(sizeof(SkBufferBl ock) + capacity); 28 void* buffer = sk_malloc_throw(sizeof(SkBufferBlock) + capacity);
27 block->fNext = nullptr; 29 return new (buffer) SkBufferBlock(capacity);
28 block->fUsed = 0;
29 block->fCapacity = capacity;
30 return block;
31 } 30 }
32 31
33 // Return number of bytes actually appended 32 // Return number of bytes actually appended. Important that we always comple tely this block
33 // before spilling into the next, since the reader uses fCapacity to know ho w many it can read.
34 //
34 size_t append(const void* src, size_t length) { 35 size_t append(const void* src, size_t length) {
35 this->validate(); 36 this->validate();
36 size_t amount = SkTMin(this->avail(), length); 37 size_t amount = SkTMin(this->avail(), length);
37 memcpy(this->availData(), src, amount); 38 memcpy(this->availData(), src, amount);
38 fUsed += amount; 39 fUsed += amount;
39 this->validate(); 40 this->validate();
40 return amount; 41 return amount;
41 } 42 }
42 43
43 void validate() const { 44 void validate() const {
44 #ifdef SK_DEBUG 45 #ifdef SK_DEBUG
45 SkASSERT(fCapacity > 0); 46 SkASSERT(fCapacity > 0);
46 SkASSERT(fUsed <= fCapacity); 47 SkASSERT(fUsed <= fCapacity);
47 #endif 48 #endif
48 } 49 }
49 50
50 private: 51 private:
51 static size_t LengthToCapacity(size_t length) { 52 static size_t LengthToCapacity(size_t length) {
52 const size_t minSize = kMinAllocSize - sizeof(SkBufferBlock); 53 const size_t minSize = kMinAllocSize - sizeof(SkBufferBlock);
53 return SkTMax(length, minSize); 54 return SkTMax(length, minSize);
54 } 55 }
55 }; 56 };
56 57
57 struct SkBufferHead { 58 struct SkBufferHead {
58 mutable int32_t fRefCnt; 59 mutable int32_t fRefCnt;
59 SkBufferBlock fBlock; 60 SkBufferBlock fBlock;
60 61
62 SkBufferHead(size_t capacity) : fRefCnt(1), fBlock(capacity) {}
63
61 static size_t LengthToCapacity(size_t length) { 64 static size_t LengthToCapacity(size_t length) {
62 const size_t minSize = kMinAllocSize - sizeof(SkBufferHead); 65 const size_t minSize = kMinAllocSize - sizeof(SkBufferHead);
63 return SkTMax(length, minSize); 66 return SkTMax(length, minSize);
64 } 67 }
65 68
66 static SkBufferHead* Alloc(size_t length) { 69 static SkBufferHead* Alloc(size_t length) {
67 size_t capacity = LengthToCapacity(length); 70 size_t capacity = LengthToCapacity(length);
68 size_t size = sizeof(SkBufferHead) + capacity; 71 size_t size = sizeof(SkBufferHead) + capacity;
69 SkBufferHead* head = (SkBufferHead*)sk_malloc_throw(size); 72 void* buffer = sk_malloc_throw(size);
70 head->fRefCnt = 1; 73 return new (buffer) SkBufferHead(capacity);
71 head->fBlock.fNext = nullptr;
72 head->fBlock.fUsed = 0;
73 head->fBlock.fCapacity = capacity;
74 return head;
75 } 74 }
76 75
77 void ref() const { 76 void ref() const {
78 SkASSERT(fRefCnt > 0); 77 SkASSERT(fRefCnt > 0);
79 sk_atomic_inc(&fRefCnt); 78 sk_atomic_inc(&fRefCnt);
80 } 79 }
81 80
82 void unref() const { 81 void unref() const {
83 SkASSERT(fRefCnt > 0); 82 SkASSERT(fRefCnt > 0);
84 // A release here acts in place of all releases we "should" have been do ing in ref(). 83 // A release here acts in place of all releases we "should" have been do ing in ref().
(...skipping 22 matching lines...) Expand all
107 block = block->fNext; 106 block = block->fNext;
108 } 107 }
109 SkASSERT(minUsed <= totalUsed); 108 SkASSERT(minUsed <= totalUsed);
110 if (tail) { 109 if (tail) {
111 SkASSERT(tail == lastBlock); 110 SkASSERT(tail == lastBlock);
112 } 111 }
113 #endif 112 #endif
114 } 113 }
115 }; 114 };
116 115
117 SkROBuffer::SkROBuffer(const SkBufferHead* head, size_t used) : fHead(head), fUs ed(used) { 116 //////////////////////////////////////////////////////////////////////////////// ///////////////////
117 // The reader can only access block.fCapacity (which never changes), and cannot access
118 // block.fUsed, which may be updated by the writer.
119 //
120 SkROBuffer::SkROBuffer(const SkBufferHead* head, size_t available)
121 : fHead(head), fAvailable(available)
122 {
118 if (head) { 123 if (head) {
119 fHead->ref(); 124 fHead->ref();
120 SkASSERT(used > 0); 125 SkASSERT(available > 0);
121 head->validate(used); 126 head->validate(available);
122 } else { 127 } else {
123 SkASSERT(0 == used); 128 SkASSERT(0 == available);
124 } 129 }
125 } 130 }
126 131
127 SkROBuffer::~SkROBuffer() { 132 SkROBuffer::~SkROBuffer() {
128 if (fHead) { 133 if (fHead) {
129 fHead->validate(fUsed); 134 fHead->validate(fAvailable);
130 fHead->unref(); 135 fHead->unref();
131 } 136 }
132 } 137 }
133 138
134 SkROBuffer::Iter::Iter(const SkROBuffer* buffer) { 139 SkROBuffer::Iter::Iter(const SkROBuffer* buffer) {
135 this->reset(buffer); 140 this->reset(buffer);
136 } 141 }
137 142
138 void SkROBuffer::Iter::reset(const SkROBuffer* buffer) { 143 void SkROBuffer::Iter::reset(const SkROBuffer* buffer) {
139 if (buffer) { 144 if (buffer) {
140 fBlock = &buffer->fHead->fBlock; 145 fBlock = &buffer->fHead->fBlock;
141 fRemaining = buffer->fUsed; 146 fRemaining = buffer->fAvailable;
142 } else { 147 } else {
143 fBlock = nullptr; 148 fBlock = nullptr;
144 fRemaining = 0; 149 fRemaining = 0;
145 } 150 }
146 } 151 }
147 152
148 const void* SkROBuffer::Iter::data() const { 153 const void* SkROBuffer::Iter::data() const {
149 return fRemaining ? fBlock->startData() : nullptr; 154 return fRemaining ? fBlock->startData() : nullptr;
150 } 155 }
151 156
152 size_t SkROBuffer::Iter::size() const { 157 size_t SkROBuffer::Iter::size() const {
153 if (!fBlock) { 158 if (!fBlock) {
154 return 0; 159 return 0;
155 } 160 }
156 return SkTMin(fBlock->fUsed, fRemaining); 161 return SkTMin(fBlock->fCapacity, fRemaining);
157 } 162 }
158 163
159 bool SkROBuffer::Iter::next() { 164 bool SkROBuffer::Iter::next() {
160 if (fRemaining) { 165 if (fRemaining) {
161 fRemaining -= this->size(); 166 fRemaining -= this->size();
162 fBlock = fBlock->fNext; 167 fBlock = fBlock->fNext;
163 } 168 }
164 return fRemaining != 0; 169 return fRemaining != 0;
165 } 170 }
166 171
172 //////////////////////////////////////////////////////////////////////////////// ///////////////////
173
167 SkRWBuffer::SkRWBuffer(size_t initialCapacity) : fHead(nullptr), fTail(nullptr), fTotalUsed(0) {} 174 SkRWBuffer::SkRWBuffer(size_t initialCapacity) : fHead(nullptr), fTail(nullptr), fTotalUsed(0) {}
168 175
169 SkRWBuffer::~SkRWBuffer() { 176 SkRWBuffer::~SkRWBuffer() {
170 this->validate(); 177 this->validate();
171 if (fHead) { 178 if (fHead) {
172 fHead->unref(); 179 fHead->unref();
173 } 180 }
174 } 181 }
175 182
183 // It is important that we always completely fill the current block before spill ing over to the
184 // next, since our reader will be using fCapacity (min'd against its total avail able) to know how
185 // many bytes to read from a given block.
186 //
176 void SkRWBuffer::append(const void* src, size_t length) { 187 void SkRWBuffer::append(const void* src, size_t length) {
177 this->validate(); 188 this->validate();
178 if (0 == length) { 189 if (0 == length) {
179 return; 190 return;
180 } 191 }
181 192
182 fTotalUsed += length; 193 fTotalUsed += length;
183 194
184 if (nullptr == fHead) { 195 if (nullptr == fHead) {
185 fHead = SkBufferHead::Alloc(length); 196 fHead = SkBufferHead::Alloc(length);
186 fTail = &fHead->fBlock; 197 fTail = &fHead->fBlock;
187 } 198 }
188 199
189 size_t written = fTail->append(src, length); 200 size_t written = fTail->append(src, length);
190 SkASSERT(written <= length); 201 SkASSERT(written <= length);
191 src = (const char*)src + written; 202 src = (const char*)src + written;
192 length -= written; 203 length -= written;
193 204
194 if (length) { 205 if (length) {
195 SkBufferBlock* block = SkBufferBlock::Alloc(length); 206 SkBufferBlock* block = SkBufferBlock::Alloc(length);
196 fTail->fNext = block; 207 fTail->fNext = block;
197 fTail = block; 208 fTail = block;
198 written = fTail->append(src, length); 209 written = fTail->append(src, length);
199 SkASSERT(written == length); 210 SkASSERT(written == length);
200 } 211 }
201 this->validate(); 212 this->validate();
202 } 213 }
203 214
204 void* SkRWBuffer::append(size_t length) {
205 this->validate();
206 if (0 == length) {
207 return nullptr;
208 }
209
210 fTotalUsed += length;
211
212 if (nullptr == fHead) {
213 fHead = SkBufferHead::Alloc(length);
214 fTail = &fHead->fBlock;
215 } else if (fTail->avail() < length) {
216 SkBufferBlock* block = SkBufferBlock::Alloc(length);
217 fTail->fNext = block;
218 fTail = block;
219 }
220
221 fTail->fUsed += length;
222 this->validate();
223 return (char*)fTail->availData() - length;
224 }
225
226 #ifdef SK_DEBUG 215 #ifdef SK_DEBUG
227 void SkRWBuffer::validate() const { 216 void SkRWBuffer::validate() const {
228 if (fHead) { 217 if (fHead) {
229 fHead->validate(fTotalUsed, fTail); 218 fHead->validate(fTotalUsed, fTail);
230 } else { 219 } else {
231 SkASSERT(nullptr == fTail); 220 SkASSERT(nullptr == fTail);
232 SkASSERT(0 == fTotalUsed); 221 SkASSERT(0 == fTotalUsed);
233 } 222 }
234 } 223 }
235 #endif 224 #endif
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 const SkROBuffer* fBuffer; 334 const SkROBuffer* fBuffer;
346 SkROBuffer::Iter fIter; 335 SkROBuffer::Iter fIter;
347 size_t fLocalOffset; 336 size_t fLocalOffset;
348 size_t fGlobalOffset; 337 size_t fGlobalOffset;
349 }; 338 };
350 339
351 SkStreamAsset* SkRWBuffer::newStreamSnapshot() const { 340 SkStreamAsset* SkRWBuffer::newStreamSnapshot() const {
352 SkAutoTUnref<SkROBuffer> buffer(this->newRBufferSnapshot()); 341 SkAutoTUnref<SkROBuffer> buffer(this->newRBufferSnapshot());
353 return new SkROBufferStreamAsset(buffer); 342 return new SkROBufferStreamAsset(buffer);
354 } 343 }
OLDNEW
« no previous file with comments | « include/core/SkRWBuffer.h ('k') | tests/DataRefTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698