Common/JitCodeBuffer: Add ability to reserve non-code space

This commit is contained in:
Connor McLaughlin 2021-07-10 18:00:58 +10:00
parent 03ab18909a
commit 6251894d3c
2 changed files with 41 additions and 11 deletions

View File

@ -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);

View File

@ -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<u32>(m_code_size - m_code_used); }
ALWAYS_INLINE u8* GetFreeCodePointer() const { return m_free_code_ptr; }
ALWAYS_INLINE u32 GetFreeCodeSpace() const { return static_cast<u32>(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<u32>(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<u32>(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;