summaryrefslogtreecommitdiff
path: root/src/common/code_block.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/code_block.h')
-rw-r--r--src/common/code_block.h40
1 files changed, 18 insertions, 22 deletions
diff --git a/src/common/code_block.h b/src/common/code_block.h
index 2fa4a0090..6a55a8e30 100644
--- a/src/common/code_block.h
+++ b/src/common/code_block.h
@@ -5,7 +5,6 @@
5#pragma once 5#pragma once
6 6
7#include <cstddef> 7#include <cstddef>
8
9#include "common/common_types.h" 8#include "common/common_types.h"
10#include "common/memory_util.h" 9#include "common/memory_util.h"
11 10
@@ -14,24 +13,27 @@
14// having to prefix them with gen-> or something similar. 13// having to prefix them with gen-> or something similar.
15// Example implementation: 14// Example implementation:
16// class JIT : public CodeBlock<ARMXEmitter> {} 15// class JIT : public CodeBlock<ARMXEmitter> {}
17template<class T> class CodeBlock : public T, NonCopyable 16template <class T>
18{ 17class CodeBlock : public T, NonCopyable {
19private: 18private:
20 // A privately used function to set the executable RAM space to something invalid. 19 // A privately used function to set the executable RAM space to something invalid.
21 // For debugging usefulness it should be used to set the RAM to a host specific breakpoint instruction 20 // For debugging usefulness it should be used to set the RAM to a host specific breakpoint
21 // instruction
22 virtual void PoisonMemory() = 0; 22 virtual void PoisonMemory() = 0;
23 23
24protected: 24protected:
25 u8 *region; 25 u8* region;
26 size_t region_size; 26 size_t region_size;
27 27
28public: 28public:
29 CodeBlock() : region(nullptr), region_size(0) {} 29 CodeBlock() : region(nullptr), region_size(0) {}
30 virtual ~CodeBlock() { if (region) FreeCodeSpace(); } 30 virtual ~CodeBlock() {
31 if (region)
32 FreeCodeSpace();
33 }
31 34
32 // Call this before you generate any code. 35 // Call this before you generate any code.
33 void AllocCodeSpace(int size) 36 void AllocCodeSpace(int size) {
34 {
35 region_size = size; 37 region_size = size;
36 region = (u8*)AllocateExecutableMemory(region_size); 38 region = (u8*)AllocateExecutableMemory(region_size);
37 T::SetCodePtr(region); 39 T::SetCodePtr(region);
@@ -39,15 +41,13 @@ public:
39 41
40 // Always clear code space with breakpoints, so that if someone accidentally executes 42 // Always clear code space with breakpoints, so that if someone accidentally executes
41 // uninitialized, it just breaks into the debugger. 43 // uninitialized, it just breaks into the debugger.
42 void ClearCodeSpace() 44 void ClearCodeSpace() {
43 {
44 PoisonMemory(); 45 PoisonMemory();
45 ResetCodePtr(); 46 ResetCodePtr();
46 } 47 }
47 48
48 // Call this when shutting down. Don't rely on the destructor, even though it'll do the job. 49 // Call this when shutting down. Don't rely on the destructor, even though it'll do the job.
49 void FreeCodeSpace() 50 void FreeCodeSpace() {
50 {
51#ifdef __SYMBIAN32__ 51#ifdef __SYMBIAN32__
52 ResetExecutableMemory(region); 52 ResetExecutableMemory(region);
53#else 53#else
@@ -57,33 +57,29 @@ public:
57 region_size = 0; 57 region_size = 0;
58 } 58 }
59 59
60 bool IsInSpace(const u8 *ptr) 60 bool IsInSpace(const u8* ptr) {
61 {
62 return (ptr >= region) && (ptr < (region + region_size)); 61 return (ptr >= region) && (ptr < (region + region_size));
63 } 62 }
64 63
65 // Cannot currently be undone. Will write protect the entire code region. 64 // Cannot currently be undone. Will write protect the entire code region.
66 // Start over if you need to change the code (call FreeCodeSpace(), AllocCodeSpace()). 65 // Start over if you need to change the code (call FreeCodeSpace(), AllocCodeSpace()).
67 void WriteProtect() 66 void WriteProtect() {
68 {
69 WriteProtectMemory(region, region_size, true); 67 WriteProtectMemory(region, region_size, true);
70 } 68 }
71 69
72 void ResetCodePtr() 70 void ResetCodePtr() {
73 {
74 T::SetCodePtr(region); 71 T::SetCodePtr(region);
75 } 72 }
76 73
77 size_t GetSpaceLeft() const 74 size_t GetSpaceLeft() const {
78 {
79 return region_size - (T::GetCodePtr() - region); 75 return region_size - (T::GetCodePtr() - region);
80 } 76 }
81 77
82 u8 *GetBasePtr() { 78 u8* GetBasePtr() {
83 return region; 79 return region;
84 } 80 }
85 81
86 size_t GetOffset(const u8 *ptr) const { 82 size_t GetOffset(const u8* ptr) const {
87 return ptr - region; 83 return ptr - region;
88 } 84 }
89}; 85};