Merge pull request #892 from comex/oh-the-abstraction

Optimize PointerWrap.
This commit is contained in:
comex 2014-08-28 17:28:16 -04:00
commit 683191b6c6
1 changed files with 14 additions and 18 deletions

View File

@ -162,8 +162,8 @@ public:
template <typename T> template <typename T>
void DoArray(T* x, u32 count) void DoArray(T* x, u32 count)
{ {
for (u32 i = 0; i != count; ++i) static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types");
Do(x[i]); DoVoid(x, count * sizeof(T));
} }
void Do(Common::Flag& flag) void Do(Common::Flag& flag)
@ -178,6 +178,10 @@ public:
void Do(T& x) void Do(T& x)
{ {
static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types"); static_assert(IsTriviallyCopyable(T), "Only sane for trivially copyable types");
// Note:
// Usually we can just use x = **ptr, etc. However, this doesn't work
// for unions containing BitFields (long story, stupid language rules)
// or arrays. This will get optimized anyway.
DoVoid((void*)&x, sizeof(x)); DoVoid((void*)&x, sizeof(x));
} }
@ -285,38 +289,30 @@ private:
Do(elem); Do(elem);
} }
__forceinline void DoByte(u8& x) __forceinline
void DoVoid(void *data, u32 size)
{ {
switch (mode) switch (mode)
{ {
case MODE_READ: case MODE_READ:
x = **ptr; memcpy(data, *ptr, size);
break; break;
case MODE_WRITE: case MODE_WRITE:
**ptr = x; memcpy(*ptr, data, size);
break; break;
case MODE_MEASURE: case MODE_MEASURE:
break; break;
case MODE_VERIFY: case MODE_VERIFY:
_dbg_assert_msg_(COMMON, (x == **ptr), _dbg_assert_msg_(COMMON, !memcmp(data, *ptr, size),
"Savestate verification failure: %d (0x%X) (at %p) != %d (0x%X) (at %p).\n", "Savestate verification failure: buf %p != %p (size %u).\n",
x, x, &x, **ptr, **ptr, *ptr); data, *ptr, size);
break;
default:
break; break;
} }
++(*ptr); *ptr += size;
}
void DoVoid(void *data, u32 size)
{
for (u32 i = 0; i != size; ++i)
DoByte(reinterpret_cast<u8*>(data)[i]);
} }
}; };