OLD | NEW |
---|---|
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2008 The Android Open Source Project | 3 * Copyright 2008 The Android Open Source Project |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 | 8 |
9 | 9 |
10 #ifndef SkWriter32_DEFINED | 10 #ifndef SkWriter32_DEFINED |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim it) { | 72 if (fInternal.isEmpty() && this->externalCount() + count <= fExternalLim it) { |
73 p = fExternal + fCount; | 73 p = fExternal + fCount; |
74 } else { | 74 } else { |
75 p = fInternal.append(count); | 75 p = fInternal.append(count); |
76 } | 76 } |
77 | 77 |
78 fCount += count; | 78 fCount += count; |
79 return p; | 79 return p; |
80 } | 80 } |
81 | 81 |
82 // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). | 82 // Read/overwrite a record at offset, which must be a multiple of 4. |
83 uint32_t read32At(size_t offset) { return this->atOffset(offset); } | 83 // These calls are valid only when the record was written atomically |
84 void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; } | 84 // using the write* methods below. |
85 template<typename T> | |
86 const T& readTAt(size_t offset) const { | |
87 return this->atOffset<T>(offset); | |
88 } | |
89 template<typename T> | |
90 void overwriteTAt(const T& value, size_t offset) { | |
mtklein
2014/02/10 22:37:34
I have a slight preference for it to go back the o
f(malita)
2014/02/11 03:30:56
Seemed more natural to follow the name hints ("ove
| |
91 this->atOffset<T>(offset) = value; | |
92 } | |
85 | 93 |
86 bool writeBool(bool value) { | 94 bool writeBool(bool value) { |
87 this->write32(value); | 95 this->write32(value); |
88 return value; | 96 return value; |
89 } | 97 } |
90 | 98 |
91 void writeInt(int32_t value) { | 99 void writeInt(int32_t value) { |
92 this->write32(value); | 100 this->write32(value); |
93 } | 101 } |
94 | 102 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 && stream->write(fInternal.begin(), fInternal.bytes()); | 239 && stream->write(fInternal.begin(), fInternal.bytes()); |
232 } | 240 } |
233 | 241 |
234 // read from the stream, and write up to length bytes. Return the actual | 242 // read from the stream, and write up to length bytes. Return the actual |
235 // number of bytes written. | 243 // number of bytes written. |
236 size_t readFromStream(SkStream* stream, size_t length) { | 244 size_t readFromStream(SkStream* stream, size_t length) { |
237 return stream->read(this->reservePad(length), length); | 245 return stream->read(this->reservePad(length), length); |
238 } | 246 } |
239 | 247 |
240 private: | 248 private: |
241 uint32_t& atOffset(size_t offset) { | 249 template<typename T> |
250 T& atOffset(size_t offset) const { | |
242 SkASSERT(SkAlign4(offset) == offset); | 251 SkASSERT(SkAlign4(offset) == offset); |
243 const int count = SkToInt(offset/4); | 252 const int count = SkToInt(offset / 4); |
244 SkASSERT(count < fCount); | 253 SkASSERT(offset + sizeof(T) <= (unsigned)fCount * 4); |
245 | 254 |
246 if (count < this->externalCount()) { | 255 if (count < this->externalCount()) { |
247 return fExternal[count]; | 256 // A record should not span fExternal->fInternal. |
257 SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() * 4); | |
258 return *(T*)&fExternal[count]; | |
248 } | 259 } |
249 return fInternal[count - this->externalCount()]; | 260 return *(T*)&fInternal[count - this->externalCount()]; |
250 } | 261 } |
251 | 262 |
252 | |
253 // Number of uint32_t written into fExternal. <= fExternalLimit. | 263 // Number of uint32_t written into fExternal. <= fExternalLimit. |
254 int externalCount() const { return fCount - fInternal.count(); } | 264 int externalCount() const { return fCount - fInternal.count(); } |
255 | 265 |
256 int fCount; // Total number of uint32_t written. | 266 int fCount; // Total number of uint32_t written. |
257 int fExternalLimit; // Number of uint32_t we can write to fExter nal. | 267 int fExternalLimit; // Number of uint32_t we can write to fExter nal. |
258 uint32_t* fExternal; // Unmanaged memory block. | 268 uint32_t* fExternal; // Unmanaged memory block. |
259 SkTDArray<uint32_t> fInternal; // Managed memory block. | 269 SkTDArray<uint32_t> fInternal; // Managed memory block. |
260 }; | 270 }; |
261 | 271 |
262 /** | 272 /** |
263 * Helper class to allocated SIZE bytes as part of the writer, and to provide | 273 * Helper class to allocated SIZE bytes as part of the writer, and to provide |
264 * that storage to the constructor as its initial storage buffer. | 274 * that storage to the constructor as its initial storage buffer. |
265 * | 275 * |
266 * This wrapper ensures proper alignment rules are met for the storage. | 276 * This wrapper ensures proper alignment rules are met for the storage. |
267 */ | 277 */ |
268 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { | 278 template <size_t SIZE> class SkSWriter32 : public SkWriter32 { |
269 public: | 279 public: |
270 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} | 280 SkSWriter32() : SkWriter32(fData.fStorage, SIZE) {} |
271 | 281 |
272 private: | 282 private: |
273 union { | 283 union { |
274 void* fPtrAlignment; | 284 void* fPtrAlignment; |
275 double fDoubleAlignment; | 285 double fDoubleAlignment; |
276 char fStorage[SIZE]; | 286 char fStorage[SIZE]; |
277 } fData; | 287 } fData; |
278 }; | 288 }; |
279 | 289 |
280 #endif | 290 #endif |
OLD | NEW |