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;
*(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;
}
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
#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) {
// 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);
// 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"