2019-09-09 07:01:26 +00:00
|
|
|
#pragma once
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
class JitCodeBuffer
|
|
|
|
{
|
|
|
|
public:
|
2020-07-31 07:09:18 +00:00
|
|
|
JitCodeBuffer();
|
|
|
|
JitCodeBuffer(u32 size, u32 far_code_size);
|
|
|
|
JitCodeBuffer(void* buffer, u32 size, u32 far_code_size, u32 guard_size);
|
2019-09-09 07:01:26 +00:00
|
|
|
~JitCodeBuffer();
|
|
|
|
|
2021-07-04 09:09:11 +00:00
|
|
|
bool IsValid() const { return (m_code_ptr != nullptr); }
|
|
|
|
|
2020-07-31 07:09:18 +00:00
|
|
|
bool Allocate(u32 size = 64 * 1024 * 1024, u32 far_code_size = 0);
|
|
|
|
bool Initialize(void* buffer, u32 size, u32 far_code_size = 0, u32 guard_size = 0);
|
|
|
|
void Destroy();
|
2019-11-22 07:57:02 +00:00
|
|
|
void Reset();
|
|
|
|
|
2021-07-10 08:00:58 +00:00
|
|
|
ALWAYS_INLINE u8* GetCodePointer() const { return m_code_ptr; }
|
|
|
|
ALWAYS_INLINE u32 GetTotalSize() const { return m_total_size; }
|
2021-07-02 02:24:37 +00:00
|
|
|
|
2021-07-10 08:00:58 +00:00
|
|
|
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);
|
2019-12-03 10:21:30 +00:00
|
|
|
void CommitCode(u32 length);
|
2019-11-22 07:57:02 +00:00
|
|
|
|
2021-07-10 08:00:58 +00:00
|
|
|
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); }
|
2019-12-03 10:21:30 +00:00
|
|
|
void CommitFarCode(u32 length);
|
2019-09-09 07:01:26 +00:00
|
|
|
|
|
|
|
/// Adjusts the free code pointer to the specified alignment, padding with bytes.
|
|
|
|
/// Assumes alignment is a power-of-two.
|
|
|
|
void Align(u32 alignment, u8 padding_value);
|
|
|
|
|
2019-12-20 11:23:52 +00:00
|
|
|
/// Flushes the instruction cache on the host for the specified range.
|
|
|
|
static void FlushInstructionCache(void* address, u32 size);
|
|
|
|
|
2021-03-19 08:47:31 +00:00
|
|
|
/// For Apple Silicon - Toggles write protection on the JIT space.
|
|
|
|
#if defined(__APPLE__) && defined(__aarch64__)
|
|
|
|
static void WriteProtect(bool enabled);
|
|
|
|
#else
|
|
|
|
ALWAYS_INLINE static void WriteProtect(bool enabled) {}
|
|
|
|
#endif
|
|
|
|
|
2019-09-09 07:01:26 +00:00
|
|
|
private:
|
2020-07-31 07:09:18 +00:00
|
|
|
u8* m_code_ptr = nullptr;
|
|
|
|
u8* m_free_code_ptr = nullptr;
|
|
|
|
u32 m_code_size = 0;
|
2021-07-10 08:00:58 +00:00
|
|
|
u32 m_code_reserve_size = 0;
|
2020-07-31 07:09:18 +00:00
|
|
|
u32 m_code_used = 0;
|
|
|
|
|
|
|
|
u8* m_far_code_ptr = nullptr;
|
|
|
|
u8* m_free_far_code_ptr = nullptr;
|
|
|
|
u32 m_far_code_size = 0;
|
|
|
|
u32 m_far_code_used = 0;
|
|
|
|
|
|
|
|
u32 m_total_size = 0;
|
|
|
|
u32 m_guard_size = 0;
|
|
|
|
u32 m_old_protection = 0;
|
|
|
|
bool m_owns_buffer = false;
|
2019-09-09 07:01:26 +00:00
|
|
|
};
|