diff --git a/src/common/jit_code_buffer.cpp b/src/common/jit_code_buffer.cpp index d59d47078..15a6dc835 100644 --- a/src/common/jit_code_buffer.cpp +++ b/src/common/jit_code_buffer.cpp @@ -171,20 +171,48 @@ void JitCodeBuffer::Destroy() if (m_owns_buffer) { #if defined(_WIN32) - VirtualFree(m_code_ptr, 0, MEM_RELEASE); + if (!VirtualFree(m_code_ptr, 0, MEM_RELEASE)) + Log_ErrorPrintf("Failed to free code pointer %p", m_code_ptr); #elif defined(__linux__) || defined(__ANDROID__) || defined(__APPLE__) || defined(__HAIKU__) || defined(__FreeBSD__) - munmap(m_code_ptr, m_total_size); + if (munmap(m_code_ptr, m_total_size) != 0) + Log_ErrorPrintf("Failed to free code pointer %p", m_code_ptr); #endif } else if (m_code_ptr) { #if defined(_WIN32) DWORD old_protect = 0; - VirtualProtect(m_code_ptr, m_total_size, m_old_protection, &old_protect); + if (!VirtualProtect(m_code_ptr, m_total_size, m_old_protection, &old_protect)) + Log_ErrorPrintf("Failed to restore protection on %p", m_code_ptr); #else - mprotect(m_code_ptr, m_total_size, m_old_protection); + if (mprotect(m_code_ptr, m_total_size, m_old_protection) != 0) + Log_ErrorPrintf("Failed to restore protection on %p", m_code_ptr); #endif } + + m_code_ptr = nullptr; + m_free_code_ptr = nullptr; + m_code_size = 0; + m_code_reserve_size = 0; + m_code_used = 0; + m_far_code_ptr = nullptr; + m_free_far_code_ptr = nullptr; + m_far_code_size = 0; + m_far_code_used = 0; + m_total_size = 0; + m_guard_size = 0; + m_old_protection = 0; + m_owns_buffer = false; +} + +void JitCodeBuffer::ReserveCode(u32 size) +{ + Assert(m_code_used == 0); + Assert(size < m_code_size); + + m_code_reserve_size += size; + m_free_code_ptr += size; + m_code_size -= size; } void JitCodeBuffer::CommitCode(u32 length) @@ -221,7 +249,7 @@ void JitCodeBuffer::Reset() { WriteProtect(false); - m_free_code_ptr = m_code_ptr + m_guard_size; + m_free_code_ptr = m_code_ptr + m_guard_size + m_code_reserve_size; m_code_used = 0; std::memset(m_free_code_ptr, 0, m_code_size); FlushInstructionCache(m_free_code_ptr, m_code_size); diff --git a/src/common/jit_code_buffer.h b/src/common/jit_code_buffer.h index e4a3f4185..72d2b88b4 100644 --- a/src/common/jit_code_buffer.h +++ b/src/common/jit_code_buffer.h @@ -16,15 +16,16 @@ public: void Destroy(); void Reset(); - u8* GetCodePointer() const { return m_code_ptr; } - u32 GetTotalSize() const { return m_total_size; } + ALWAYS_INLINE u8* GetCodePointer() const { return m_code_ptr; } + ALWAYS_INLINE u32 GetTotalSize() const { return m_total_size; } - u8* GetFreeCodePointer() const { return m_free_code_ptr; } - u32 GetFreeCodeSpace() const { return static_cast(m_code_size - m_code_used); } + ALWAYS_INLINE u8* GetFreeCodePointer() const { return m_free_code_ptr; } + ALWAYS_INLINE u32 GetFreeCodeSpace() const { return static_cast(m_code_size - m_code_used); } + void ReserveCode(u32 size); void CommitCode(u32 length); - u8* GetFreeFarCodePointer() const { return m_free_far_code_ptr; } - u32 GetFreeFarCodeSpace() const { return static_cast(m_far_code_size - m_far_code_used); } + ALWAYS_INLINE u8* GetFreeFarCodePointer() const { return m_free_far_code_ptr; } + ALWAYS_INLINE u32 GetFreeFarCodeSpace() const { return static_cast(m_far_code_size - m_far_code_used); } void CommitFarCode(u32 length); /// Adjusts the free code pointer to the specified alignment, padding with bytes. @@ -45,6 +46,7 @@ private: u8* m_code_ptr = nullptr; u8* m_free_code_ptr = nullptr; u32 m_code_size = 0; + u32 m_code_reserve_size = 0; u32 m_code_used = 0; u8* m_far_code_ptr = nullptr;