Index: include/core/SkWriter32.h |
diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h |
index b193cbe7c407663f7a82a613b8a65b5e1e637a47..0cd55945748469b03a8f4d418968b38e9674a8cf 100644 |
--- a/include/core/SkWriter32.h |
+++ b/include/core/SkWriter32.h |
@@ -79,9 +79,23 @@ public: |
return p; |
} |
- // Read or write 4 bytes at offset, which must be a multiple of 4 <= size(). |
- uint32_t read32At(size_t offset) { return this->atOffset(offset); } |
- void write32At(size_t offset, uint32_t val) { this->atOffset(offset) = val; } |
+ /** |
+ * Read a T record at offset, which must be a multiple of 4. Only legal if the record |
+ * was writtern atomically using the write methods below. |
+ */ |
+ template<typename T> |
+ const T& readTAt(size_t offset) const { |
+ return this->atOffset<T>(offset); |
+ } |
+ |
+ /** |
+ * Overwrite a T record at offset, which must be a multiple of 4. Only legal if the record |
+ * was writtern atomically using the write methods below. |
+ */ |
+ template<typename T> |
+ void overwriteTAt(size_t offset, const T& value) { |
+ this->atOffset<T>(offset) = value; |
+ } |
bool writeBool(bool value) { |
this->write32(value); |
@@ -167,14 +181,11 @@ public: |
* filled in with zeroes. |
*/ |
uint32_t* reservePad(size_t size) { |
- uint32_t* p = this->reserve(SkAlign4(size)); |
- uint8_t* tail = (uint8_t*)p + size; |
- switch (SkAlign4(size) - size) { |
- default: SkDEBUGFAIL("SkAlign4(x) - x should always be 0, 1, 2, or 3."); |
- case 3: *tail++ = 0x00; // fallthrough is intentional |
- case 2: *tail++ = 0x00; // fallthrough is intentional |
- case 1: *tail++ = 0x00; |
- case 0: ;/*nothing to do*/ |
+ size_t alignedSize = SkAlign4(size); |
+ uint32_t* p = this->reserve(alignedSize); |
+ if (alignedSize != size) { |
+ SkASSERT(alignedSize >= 4); |
+ p[alignedSize / 4 - 1] = 0; |
mtklein
2014/02/11 13:18:10
Duh. I don't know why I wrote the old one to be s
f(malita)
2014/02/11 14:09:36
Yeah, I kept the conditional mainly for handling r
|
} |
return p; |
} |
@@ -238,18 +249,20 @@ public: |
} |
private: |
- uint32_t& atOffset(size_t offset) { |
+ template<typename T> |
+ T& atOffset(size_t offset) const { |
SkASSERT(SkAlign4(offset) == offset); |
- const int count = SkToInt(offset/4); |
- SkASSERT(count < fCount); |
+ const int count = SkToInt(offset / 4); |
+ SkASSERT(offset + sizeof(T) <= (unsigned)fCount * 4); |
if (count < this->externalCount()) { |
- return fExternal[count]; |
+ // A record should not span fExternal->fInternal. |
+ SkASSERT(offset + sizeof(T) <= (unsigned)this->externalCount() * 4); |
+ return *(T*)&fExternal[count]; |
} |
- return fInternal[count - this->externalCount()]; |
+ return *(T*)&fInternal[count - this->externalCount()]; |
} |
- |
// Number of uint32_t written into fExternal. <= fExternalLimit. |
int externalCount() const { return fCount - fInternal.count(); } |