diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-12 20:47:53 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-06-12 20:47:53 +0000 |
commit | 51c62a6cfadc302bb65bb5a98e358c93223dc73f (patch) | |
tree | 34371459b9ab82133c9a2b723029fadc2fc3ef3c | |
parent | 64a0ec36555352ec31aa7c5a7630a5d042b010ba (diff) |
add readPtr and writePtr to SkReader32 and SkWriter32
add template helper SkSWriter32, which allocates initial storage buffer
Review URL: https://codereview.appspot.com/6299075
git-svn-id: http://skia.googlecode.com/svn/trunk@4237 2bbb7eff-a529-9590-31e7-b0007b416f81
-rw-r--r-- | include/core/SkReader32.h | 14 | ||||
-rw-r--r-- | include/core/SkWriter32.h | 29 | ||||
-rw-r--r-- | tests/Writer32Test.cpp | 36 |
3 files changed, 74 insertions, 5 deletions
diff --git a/include/core/SkReader32.h b/include/core/SkReader32.h index 9e25e84a55..c127d8988c 100644 --- a/include/core/SkReader32.h +++ b/include/core/SkReader32.h @@ -58,7 +58,19 @@ public: SkASSERT(fCurr <= fStop); return value; } - + + void* readPtr() { + void* ptr; + // we presume this "if" is resolved at compile-time + if (4 == sizeof(void*)) { + ptr = *(void**)fCurr; + } else { + memcpy(&ptr, fCurr, sizeof(void*)); + } + fCurr += sizeof(void*); + return ptr; + } + SkScalar readScalar() { SkASSERT(ptr_align_4(fCurr)); SkScalar value = *(const SkScalar*)fCurr; diff --git a/include/core/SkWriter32.h b/include/core/SkWriter32.h index 9abaa467d2..793e815b3d 100644 --- a/include/core/SkWriter32.h +++ b/include/core/SkWriter32.h @@ -77,6 +77,17 @@ public: *(int32_t*)this->reserve(sizeof(value)) = value; } + void writePtr(void* ptr) { + // Since we "know" that we're always 4-byte aligned, we can tell the + // compiler that here, by assigning to an int32 ptr. + int32_t* addr = (int32_t*)this->reserve(sizeof(void*)); + if (4 == sizeof(void*)) { + *(void**)addr = ptr; + } else { + memcpy(addr, &ptr, sizeof(void*)); + } + } + void writeScalar(SkScalar value) { *(SkScalar*)this->reserve(sizeof(value)) = value; } @@ -176,4 +187,22 @@ private: Block* newBlock(size_t bytes); }; +/** + * Helper class to allocated SIZE bytes as part of the writer, and to provide + * that storage to the constructor as its initial storage buffer. + * + * This wrapper ensures proper alignment rules are met for the storage. + */ +template <size_t SIZE> class SkSWriter32 : public SkWriter32 { +public: + SkSWriter32(size_t minSize) : SkWriter32(minSize, fData.fStorage, SIZE) {} + +private: + union { + void* fPtrAlignment; + double fDoubleAlignment; + char fStorage[SIZE]; + } fData; +}; + #endif diff --git a/tests/Writer32Test.cpp b/tests/Writer32Test.cpp index 35fb29f38e..311f37d8fb 100644 --- a/tests/Writer32Test.cpp +++ b/tests/Writer32Test.cpp @@ -12,6 +12,34 @@ #include "SkWriter32.h" #include "Test.h" +static void test_ptr(skiatest::Reporter* reporter) { + SkSWriter32<32> writer(32); + + void* p0 = reporter; + void* p1 = &writer; + + // try writing ptrs where at least one of them may be at a non-multiple of + // 8 boundary, to confirm this works on 64bit machines. + + writer.writePtr(p0); + writer.write8(0x33); + writer.writePtr(p1); + writer.write8(0x66); + + size_t size = writer.size(); + REPORTER_ASSERT(reporter, 2 * sizeof(void*) + 2 * sizeof(int32_t)); + + char buffer[32]; + SkASSERT(sizeof(buffer) >= size); + writer.flatten(buffer); + + SkReader32 reader(buffer, size); + REPORTER_ASSERT(reporter, reader.readPtr() == p0); + REPORTER_ASSERT(reporter, reader.readInt() == 0x33); + REPORTER_ASSERT(reporter, reader.readPtr() == p1); + REPORTER_ASSERT(reporter, reader.readInt() == 0x66); +} + static void test1(skiatest::Reporter* reporter, SkWriter32* writer) { const uint32_t data[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (size_t i = 0; i < SK_ARRAY_COUNT(data); ++i) { @@ -79,8 +107,7 @@ static void Tests(skiatest::Reporter* reporter) { // small storage { - intptr_t storage[8]; - SkWriter32 writer(100, storage, sizeof(storage)); + SkSWriter32<8 * sizeof(intptr_t)> writer(100); test1(reporter, &writer); writer.reset(); // should just rewind our storage test2(reporter, &writer); @@ -88,12 +115,13 @@ static void Tests(skiatest::Reporter* reporter) { // large storage { - intptr_t storage[1024]; - SkWriter32 writer(100, storage, sizeof(storage)); + SkSWriter32<1024 * sizeof(intptr_t)> writer(100); test1(reporter, &writer); writer.reset(); // should just rewind our storage test2(reporter, &writer); } + + test_ptr(reporter); } #include "TestClassDef.h" |