| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
#pragma once |
| 5 |
|
| 6 |
#include <cstddef> |
| 7 |
#include <vector> |
| 8 |
|
| 9 |
#include "Common/Assert.h" |
| 10 |
#include "Common/CommonTypes.h" |
| 11 |
#include "Common/MemoryUtil.h" |
| 12 |
|
| 13 |
namespace Common |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
template <class T, bool executable = true> |
| 21 |
class CodeBlock : public T |
| 22 |
{ |
| 23 |
private: |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
virtual void PoisonMemory() = 0; |
| 28 |
|
| 29 |
protected: |
| 30 |
u8* region = nullptr; |
| 31 |
|
| 32 |
size_t region_size = 0; |
| 33 |
|
| 34 |
size_t total_region_size = 0; |
| 35 |
|
| 36 |
bool m_is_child = false; |
| 37 |
std::vector<CodeBlock*> m_children; |
| 38 |
|
| 39 |
public: |
| 40 |
CodeBlock() = default; |
| 41 |
virtual ~CodeBlock() |
| 42 |
{ |
| 43 |
if (region) |
| 44 |
FreeCodeSpace(); |
| 45 |
} |
| 46 |
CodeBlock(const CodeBlock&) = delete; |
| 47 |
CodeBlock& operator=(const CodeBlock&) = delete; |
| 48 |
CodeBlock(CodeBlock&&) = delete; |
| 49 |
CodeBlock& operator=(CodeBlock&&) = delete; |
| 50 |
|
| 51 |
|
| 52 |
void AllocCodeSpace(size_t size) |
| 53 |
{ |
| 54 |
region_size = size; |
| 55 |
total_region_size = size; |
| 56 |
if constexpr (executable) |
| 57 |
region = static_cast<u8*>(Common::AllocateExecutableMemory(total_region_size)); |
| 58 |
else |
| 59 |
region = static_cast<u8*>(Common::AllocateMemoryPages(total_region_size)); |
| 60 |
T::SetCodePtr(region, region + size); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
void ClearCodeSpace() |
| 66 |
{ |
| 67 |
PoisonMemory(); |
| 68 |
ResetCodePtr(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
void FreeCodeSpace() |
| 73 |
{ |
| 74 |
ASSERT(!m_is_child); |
| 75 |
Common::FreeMemoryPages(region, total_region_size); |
| 76 |
region = nullptr; |
| 77 |
region_size = 0; |
| 78 |
total_region_size = 0; |
| 79 |
for (CodeBlock* child : m_children) |
| 80 |
{ |
| 81 |
child->region = nullptr; |
| 82 |
child->region_size = 0; |
| 83 |
child->total_region_size = 0; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
bool IsInSpace(const u8* ptr) const { return ptr >= region && ptr < (region + region_size); } |
| 88 |
bool IsInSpaceOrChildSpace(const u8* ptr) const |
| 89 |
{ |
| 90 |
return ptr >= region && ptr < (region + total_region_size); |
| 91 |
} |
| 92 |
void WriteProtect(bool allow_execute) |
| 93 |
{ |
| 94 |
Common::WriteProtectMemory(region, region_size, allow_execute); |
| 95 |
} |
| 96 |
void UnWriteProtect(bool allow_execute) |
| 97 |
{ |
| 98 |
Common::UnWriteProtectMemory(region, region_size, allow_execute); |
| 99 |
} |
| 100 |
void ResetCodePtr() { T::SetCodePtr(region, region + region_size); } |
| 101 |
size_t GetSpaceLeft() const |
| 102 |
{ |
| 103 |
ASSERT(static_cast<size_t>(T::GetCodePtr() - region) < region_size); |
| 104 |
return region_size - (T::GetCodePtr() - region); |
| 105 |
} |
| 106 |
|
| 107 |
bool IsAlmostFull() const |
| 108 |
{ |
| 109 |
|
| 110 |
return GetSpaceLeft() < 0x10000; |
| 111 |
} |
| 112 |
|
| 113 |
bool HasChildren() const { return region_size != total_region_size; } |
| 114 |
u8* AllocChildCodeSpace(size_t child_size) |
| 115 |
{ |
| 116 |
ASSERT_MSG(DYNA_REC, child_size <= GetSpaceLeft(), "Insufficient space for child allocation."); |
| 117 |
u8* child_region = region + region_size - child_size; |
| 118 |
region_size -= child_size; |
| 119 |
ResetCodePtr(); |
| 120 |
return child_region; |
| 121 |
} |
| 122 |
void AddChildCodeSpace(CodeBlock* child, size_t child_size) |
| 123 |
{ |
| 124 |
u8* child_region = AllocChildCodeSpace(child_size); |
| 125 |
child->m_is_child = true; |
| 126 |
child->region = child_region; |
| 127 |
child->region_size = child_size; |
| 128 |
child->total_region_size = child_size; |
| 129 |
child->ResetCodePtr(); |
| 130 |
m_children.emplace_back(child); |
| 131 |
} |
| 132 |
}; |
| 133 |
} |