diff --git a/DynamicHooks/registers.cpp b/DynamicHooks/registers.cpp index c9d28d3..77fe503 100644 --- a/DynamicHooks/registers.cpp +++ b/DynamicHooks/registers.cpp @@ -151,14 +151,15 @@ CRegisters::CRegisters(ke::Vector registers) // ======================================================================== // >> 128-bit XMM registers // ======================================================================== - m_xmm0 = CreateRegister(registers, XMM0, 16); - m_xmm1 = CreateRegister(registers, XMM1, 16); - m_xmm2 = CreateRegister(registers, XMM2, 16); - m_xmm3 = CreateRegister(registers, XMM3, 16); - m_xmm4 = CreateRegister(registers, XMM4, 16); - m_xmm5 = CreateRegister(registers, XMM5, 16); - m_xmm6 = CreateRegister(registers, XMM6, 16); - m_xmm7 = CreateRegister(registers, XMM7, 16); + // Copying data from xmm0-7 requires the memory address to be 16-byte aligned. + m_xmm0 = CreateRegister(registers, XMM0, 16, 16); + m_xmm1 = CreateRegister(registers, XMM1, 16, 16); + m_xmm2 = CreateRegister(registers, XMM2, 16, 16); + m_xmm3 = CreateRegister(registers, XMM3, 16, 16); + m_xmm4 = CreateRegister(registers, XMM4, 16, 16); + m_xmm5 = CreateRegister(registers, XMM5, 16, 16); + m_xmm6 = CreateRegister(registers, XMM6, 16, 16); + m_xmm7 = CreateRegister(registers, XMM7, 16, 16); // 64-bit mode only /* @@ -360,13 +361,13 @@ CRegisters::~CRegisters() DeleteRegister(m_st7); } -CRegister* CRegisters::CreateRegister(ke::Vector& registers, Register_t reg, int iSize) +CRegister* CRegisters::CreateRegister(ke::Vector& registers, Register_t reg, uint16_t iSize, uint16_t iAlignment) { for(size_t i = 0; i < registers.length(); i++) { if (registers[i] == reg) { - return new CRegister(iSize); + return new CRegister(iSize, iAlignment); } } return NULL; diff --git a/DynamicHooks/registers.h b/DynamicHooks/registers.h index 049d66b..acb0642 100644 --- a/DynamicHooks/registers.h +++ b/DynamicHooks/registers.h @@ -216,15 +216,31 @@ enum Register_t class CRegister { public: - CRegister(int iSize) + CRegister(uint16_t iSize, uint16_t iAlignment = 0) { m_iSize = iSize; - m_pAddress = malloc(iSize); + m_iAlignment = iAlignment; + if (iAlignment > 0) +#ifdef KE_WINDOWS + m_pAddress = _aligned_malloc(iSize, iAlignment); +#else + m_pAddress = aligned_alloc(iAlignment, iSize); +#endif + else + m_pAddress = malloc(iSize); } ~CRegister() { + +#ifdef KE_WINDOWS + if (m_iAlignment > 0) + _aligned_free(m_pAddress); + else + free(m_pAddress); +#else free(m_pAddress); +#endif } template @@ -252,7 +268,8 @@ public: } public: - int m_iSize; + uint16_t m_iSize; + uint16_t m_iAlignment; void* m_pAddress; }; @@ -269,7 +286,7 @@ public: CRegister* GetRegister(Register_t reg); private: - CRegister* CreateRegister(ke::Vector& registers, Register_t reg, int iSize); + CRegister* CreateRegister(ke::Vector& registers, Register_t reg, uint16_t iSize, uint16_t iAlignment = 0); void DeleteRegister(CRegister* pRegister); public: