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