OLD | NEW |
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; |
16 size_t fUsed; | 16 size_t fUsed; |
17 size_t fCapacity; | 17 size_t fCapacity; |
18 | 18 |
19 const void* startData() const { return this + 1; }; | 19 const void* startData() const { return this + 1; }; |
20 | 20 |
21 size_t avail() const { return fCapacity - fUsed; } | 21 size_t avail() const { return fCapacity - fUsed; } |
22 void* availData() { return (char*)this->startData() + fUsed; } | 22 void* availData() { return (char*)this->startData() + fUsed; } |
23 | 23 |
24 static SkBufferBlock* Alloc(size_t length) { | 24 static SkBufferBlock* Alloc(size_t length) { |
25 size_t capacity = LengthToCapacity(length); | 25 size_t capacity = LengthToCapacity(length); |
26 SkBufferBlock* block = (SkBufferBlock*)sk_malloc_throw(sizeof(SkBufferBl
ock) + capacity); | 26 SkBufferBlock* block = (SkBufferBlock*)sk_malloc_throw(sizeof(SkBufferBl
ock) + capacity); |
27 block->fNext = NULL; | 27 block->fNext = nullptr; |
28 block->fUsed = 0; | 28 block->fUsed = 0; |
29 block->fCapacity = capacity; | 29 block->fCapacity = capacity; |
30 return block; | 30 return block; |
31 } | 31 } |
32 | 32 |
33 // Return number of bytes actually appended | 33 // Return number of bytes actually appended |
34 size_t append(const void* src, size_t length) { | 34 size_t append(const void* src, size_t length) { |
35 this->validate(); | 35 this->validate(); |
36 size_t amount = SkTMin(this->avail(), length); | 36 size_t amount = SkTMin(this->avail(), length); |
37 memcpy(this->availData(), src, amount); | 37 memcpy(this->availData(), src, amount); |
(...skipping 23 matching lines...) Expand all Loading... |
61 static size_t LengthToCapacity(size_t length) { | 61 static size_t LengthToCapacity(size_t length) { |
62 const size_t minSize = kMinAllocSize - sizeof(SkBufferHead); | 62 const size_t minSize = kMinAllocSize - sizeof(SkBufferHead); |
63 return SkTMax(length, minSize); | 63 return SkTMax(length, minSize); |
64 } | 64 } |
65 | 65 |
66 static SkBufferHead* Alloc(size_t length) { | 66 static SkBufferHead* Alloc(size_t length) { |
67 size_t capacity = LengthToCapacity(length); | 67 size_t capacity = LengthToCapacity(length); |
68 size_t size = sizeof(SkBufferHead) + capacity; | 68 size_t size = sizeof(SkBufferHead) + capacity; |
69 SkBufferHead* head = (SkBufferHead*)sk_malloc_throw(size); | 69 SkBufferHead* head = (SkBufferHead*)sk_malloc_throw(size); |
70 head->fRefCnt = 1; | 70 head->fRefCnt = 1; |
71 head->fBlock.fNext = NULL; | 71 head->fBlock.fNext = nullptr; |
72 head->fBlock.fUsed = 0; | 72 head->fBlock.fUsed = 0; |
73 head->fBlock.fCapacity = capacity; | 73 head->fBlock.fCapacity = capacity; |
74 return head; | 74 return head; |
75 } | 75 } |
76 | 76 |
77 void ref() const { | 77 void ref() const { |
78 SkASSERT(fRefCnt > 0); | 78 SkASSERT(fRefCnt > 0); |
79 sk_atomic_inc(&fRefCnt); | 79 sk_atomic_inc(&fRefCnt); |
80 } | 80 } |
81 | 81 |
82 void unref() const { | 82 void unref() const { |
83 SkASSERT(fRefCnt > 0); | 83 SkASSERT(fRefCnt > 0); |
84 // A release here acts in place of all releases we "should" have been do
ing in ref(). | 84 // A release here acts in place of all releases we "should" have been do
ing in ref(). |
85 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { | 85 if (1 == sk_atomic_fetch_add(&fRefCnt, -1, sk_memory_order_acq_rel)) { |
86 // Like unique(), the acquire is only needed on success. | 86 // Like unique(), the acquire is only needed on success. |
87 SkBufferBlock* block = fBlock.fNext; | 87 SkBufferBlock* block = fBlock.fNext; |
88 sk_free((void*)this); | 88 sk_free((void*)this); |
89 while (block) { | 89 while (block) { |
90 SkBufferBlock* next = block->fNext; | 90 SkBufferBlock* next = block->fNext; |
91 sk_free(block); | 91 sk_free(block); |
92 block = next; | 92 block = next; |
93 } | 93 } |
94 } | 94 } |
95 } | 95 } |
96 | 96 |
97 void validate(size_t minUsed, SkBufferBlock* tail = NULL) const { | 97 void validate(size_t minUsed, SkBufferBlock* tail = nullptr) const { |
98 #ifdef SK_DEBUG | 98 #ifdef SK_DEBUG |
99 SkASSERT(fRefCnt > 0); | 99 SkASSERT(fRefCnt > 0); |
100 size_t totalUsed = 0; | 100 size_t totalUsed = 0; |
101 const SkBufferBlock* block = &fBlock; | 101 const SkBufferBlock* block = &fBlock; |
102 const SkBufferBlock* lastBlock = block; | 102 const SkBufferBlock* lastBlock = block; |
103 while (block) { | 103 while (block) { |
104 block->validate(); | 104 block->validate(); |
105 totalUsed += block->fUsed; | 105 totalUsed += block->fUsed; |
106 lastBlock = block; | 106 lastBlock = block; |
107 block = block->fNext; | 107 block = block->fNext; |
(...skipping 25 matching lines...) Expand all Loading... |
133 | 133 |
134 SkROBuffer::Iter::Iter(const SkROBuffer* buffer) { | 134 SkROBuffer::Iter::Iter(const SkROBuffer* buffer) { |
135 this->reset(buffer); | 135 this->reset(buffer); |
136 } | 136 } |
137 | 137 |
138 void SkROBuffer::Iter::reset(const SkROBuffer* buffer) { | 138 void SkROBuffer::Iter::reset(const SkROBuffer* buffer) { |
139 if (buffer) { | 139 if (buffer) { |
140 fBlock = &buffer->fHead->fBlock; | 140 fBlock = &buffer->fHead->fBlock; |
141 fRemaining = buffer->fUsed; | 141 fRemaining = buffer->fUsed; |
142 } else { | 142 } else { |
143 fBlock = NULL; | 143 fBlock = nullptr; |
144 fRemaining = 0; | 144 fRemaining = 0; |
145 } | 145 } |
146 } | 146 } |
147 | 147 |
148 const void* SkROBuffer::Iter::data() const { | 148 const void* SkROBuffer::Iter::data() const { |
149 return fRemaining ? fBlock->startData() : NULL; | 149 return fRemaining ? fBlock->startData() : nullptr; |
150 } | 150 } |
151 | 151 |
152 size_t SkROBuffer::Iter::size() const { | 152 size_t SkROBuffer::Iter::size() const { |
153 return SkTMin(fBlock->fUsed, fRemaining); | 153 return SkTMin(fBlock->fUsed, fRemaining); |
154 } | 154 } |
155 | 155 |
156 bool SkROBuffer::Iter::next() { | 156 bool SkROBuffer::Iter::next() { |
157 if (fRemaining) { | 157 if (fRemaining) { |
158 fRemaining -= this->size(); | 158 fRemaining -= this->size(); |
159 fBlock = fBlock->fNext; | 159 fBlock = fBlock->fNext; |
160 } | 160 } |
161 return fRemaining != 0; | 161 return fRemaining != 0; |
162 } | 162 } |
163 | 163 |
164 SkRWBuffer::SkRWBuffer(size_t initialCapacity) : fHead(NULL), fTail(NULL), fTota
lUsed(0) {} | 164 SkRWBuffer::SkRWBuffer(size_t initialCapacity) : fHead(nullptr), fTail(nullptr),
fTotalUsed(0) {} |
165 | 165 |
166 SkRWBuffer::~SkRWBuffer() { | 166 SkRWBuffer::~SkRWBuffer() { |
167 this->validate(); | 167 this->validate(); |
168 fHead->unref(); | 168 fHead->unref(); |
169 } | 169 } |
170 | 170 |
171 void SkRWBuffer::append(const void* src, size_t length) { | 171 void SkRWBuffer::append(const void* src, size_t length) { |
172 this->validate(); | 172 this->validate(); |
173 if (0 == length) { | 173 if (0 == length) { |
174 return; | 174 return; |
175 } | 175 } |
176 | 176 |
177 fTotalUsed += length; | 177 fTotalUsed += length; |
178 | 178 |
179 if (NULL == fHead) { | 179 if (nullptr == fHead) { |
180 fHead = SkBufferHead::Alloc(length); | 180 fHead = SkBufferHead::Alloc(length); |
181 fTail = &fHead->fBlock; | 181 fTail = &fHead->fBlock; |
182 } | 182 } |
183 | 183 |
184 size_t written = fTail->append(src, length); | 184 size_t written = fTail->append(src, length); |
185 SkASSERT(written <= length); | 185 SkASSERT(written <= length); |
186 src = (const char*)src + written; | 186 src = (const char*)src + written; |
187 length -= written; | 187 length -= written; |
188 | 188 |
189 if (length) { | 189 if (length) { |
190 SkBufferBlock* block = SkBufferBlock::Alloc(length); | 190 SkBufferBlock* block = SkBufferBlock::Alloc(length); |
191 fTail->fNext = block; | 191 fTail->fNext = block; |
192 fTail = block; | 192 fTail = block; |
193 written = fTail->append(src, length); | 193 written = fTail->append(src, length); |
194 SkASSERT(written == length); | 194 SkASSERT(written == length); |
195 } | 195 } |
196 this->validate(); | 196 this->validate(); |
197 } | 197 } |
198 | 198 |
199 void* SkRWBuffer::append(size_t length) { | 199 void* SkRWBuffer::append(size_t length) { |
200 this->validate(); | 200 this->validate(); |
201 if (0 == length) { | 201 if (0 == length) { |
202 return NULL; | 202 return nullptr; |
203 } | 203 } |
204 | 204 |
205 fTotalUsed += length; | 205 fTotalUsed += length; |
206 | 206 |
207 if (NULL == fHead) { | 207 if (nullptr == fHead) { |
208 fHead = SkBufferHead::Alloc(length); | 208 fHead = SkBufferHead::Alloc(length); |
209 fTail = &fHead->fBlock; | 209 fTail = &fHead->fBlock; |
210 } else if (fTail->avail() < length) { | 210 } else if (fTail->avail() < length) { |
211 SkBufferBlock* block = SkBufferBlock::Alloc(length); | 211 SkBufferBlock* block = SkBufferBlock::Alloc(length); |
212 fTail->fNext = block; | 212 fTail->fNext = block; |
213 fTail = block; | 213 fTail = block; |
214 } | 214 } |
215 | 215 |
216 fTail->fUsed += length; | 216 fTail->fUsed += length; |
217 this->validate(); | 217 this->validate(); |
218 return (char*)fTail->availData() - length; | 218 return (char*)fTail->availData() - length; |
219 } | 219 } |
220 | 220 |
221 #ifdef SK_DEBUG | 221 #ifdef SK_DEBUG |
222 void SkRWBuffer::validate() const { | 222 void SkRWBuffer::validate() const { |
223 if (fHead) { | 223 if (fHead) { |
224 fHead->validate(fTotalUsed, fTail); | 224 fHead->validate(fTotalUsed, fTail); |
225 } else { | 225 } else { |
226 SkASSERT(NULL == fTail); | 226 SkASSERT(nullptr == fTail); |
227 SkASSERT(0 == fTotalUsed); | 227 SkASSERT(0 == fTotalUsed); |
228 } | 228 } |
229 } | 229 } |
230 #endif | 230 #endif |
231 | 231 |
232 SkROBuffer* SkRWBuffer::newRBufferSnapshot() const { return new SkROBuffer(fHead
, fTotalUsed); } | 232 SkROBuffer* SkRWBuffer::newRBufferSnapshot() const { return new SkROBuffer(fHead
, fTotalUsed); } |
233 | 233 |
234 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 234 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
235 | 235 |
236 class SkROBufferStreamAsset : public SkStreamAsset { | 236 class SkROBufferStreamAsset : public SkStreamAsset { |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
340 const SkROBuffer* fBuffer; | 340 const SkROBuffer* fBuffer; |
341 SkROBuffer::Iter fIter; | 341 SkROBuffer::Iter fIter; |
342 size_t fLocalOffset; | 342 size_t fLocalOffset; |
343 size_t fGlobalOffset; | 343 size_t fGlobalOffset; |
344 }; | 344 }; |
345 | 345 |
346 SkStreamAsset* SkRWBuffer::newStreamSnapshot() const { | 346 SkStreamAsset* SkRWBuffer::newStreamSnapshot() const { |
347 SkAutoTUnref<SkROBuffer> buffer(this->newRBufferSnapshot()); | 347 SkAutoTUnref<SkROBuffer> buffer(this->newRBufferSnapshot()); |
348 return new SkROBufferStreamAsset(buffer); | 348 return new SkROBufferStreamAsset(buffer); |
349 } | 349 } |
OLD | NEW |