mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 06:09:50 -06:00
CodeBlock: Add support for multiple children
This commit is contained in:
@ -5,6 +5,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "Common/Assert.h"
|
#include "Common/Assert.h"
|
||||||
#include "Common/CommonTypes.h"
|
#include "Common/CommonTypes.h"
|
||||||
@ -27,12 +28,13 @@ private:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
u8* region = nullptr;
|
u8* region = nullptr;
|
||||||
|
// Size of region we can use.
|
||||||
size_t region_size = 0;
|
size_t region_size = 0;
|
||||||
size_t parent_region_size = 0;
|
// Original size of the region we allocated.
|
||||||
|
size_t total_region_size = 0;
|
||||||
|
|
||||||
bool m_has_child = false;
|
|
||||||
bool m_is_child = false;
|
bool m_is_child = false;
|
||||||
CodeBlock* m_child = nullptr;
|
std::vector<CodeBlock*> m_children;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual ~CodeBlock()
|
virtual ~CodeBlock()
|
||||||
@ -45,7 +47,8 @@ public:
|
|||||||
virtual void AllocCodeSpace(size_t size, bool need_low = true)
|
virtual void AllocCodeSpace(size_t size, bool need_low = true)
|
||||||
{
|
{
|
||||||
region_size = size;
|
region_size = size;
|
||||||
region = static_cast<u8*>(Common::AllocateExecutableMemory(region_size, need_low));
|
total_region_size = size;
|
||||||
|
region = static_cast<u8*>(Common::AllocateExecutableMemory(total_region_size, need_low));
|
||||||
T::SetCodePtr(region);
|
T::SetCodePtr(region);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -60,14 +63,16 @@ public:
|
|||||||
// Call this when shutting down. Don't rely on the destructor, even though it'll do the job.
|
// Call this when shutting down. Don't rely on the destructor, even though it'll do the job.
|
||||||
void FreeCodeSpace()
|
void FreeCodeSpace()
|
||||||
{
|
{
|
||||||
Common::FreeMemoryPages(region, region_size);
|
_assert_(!m_is_child);
|
||||||
|
Common::FreeMemoryPages(region, total_region_size);
|
||||||
region = nullptr;
|
region = nullptr;
|
||||||
region_size = 0;
|
region_size = 0;
|
||||||
parent_region_size = 0;
|
total_region_size = 0;
|
||||||
if (m_has_child)
|
for (CodeBlock* child : m_children)
|
||||||
{
|
{
|
||||||
m_child->region = nullptr;
|
child->region = nullptr;
|
||||||
m_child->region_size = 0;
|
child->region_size = 0;
|
||||||
|
child->total_region_size = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,7 +83,8 @@ public:
|
|||||||
void ResetCodePtr() { T::SetCodePtr(region); }
|
void ResetCodePtr() { T::SetCodePtr(region); }
|
||||||
size_t GetSpaceLeft() const
|
size_t GetSpaceLeft() const
|
||||||
{
|
{
|
||||||
return (m_has_child ? parent_region_size : region_size) - (T::GetCodePtr() - region);
|
_assert_(static_cast<size_t>(T::GetCodePtr() - region) < region_size);
|
||||||
|
return region_size - (T::GetCodePtr() - region);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsAlmostFull() const
|
bool IsAlmostFull() const
|
||||||
@ -86,16 +92,23 @@ public:
|
|||||||
// This should be bigger than the biggest block ever.
|
// This should be bigger than the biggest block ever.
|
||||||
return GetSpaceLeft() < 0x10000;
|
return GetSpaceLeft() < 0x10000;
|
||||||
}
|
}
|
||||||
void AddChildCodeSpace(CodeBlock* child, size_t size)
|
|
||||||
|
bool HasChildren() const { return region_size != total_region_size; }
|
||||||
|
u8* AllocChildCodeSpace(size_t child_size)
|
||||||
{
|
{
|
||||||
_assert_msg_(DYNA_REC, !m_has_child, "Already have a child! Can't have another!");
|
_assert_msg_(DYNA_REG, child_size < GetSpaceLeft(), "Insufficient space for child allocation.");
|
||||||
m_child = child;
|
u8* child_region = region + region_size - child_size;
|
||||||
m_has_child = true;
|
region_size -= child_size;
|
||||||
m_child->m_is_child = true;
|
return child_region;
|
||||||
u8* child_region = region + region_size - size;
|
}
|
||||||
m_child->region = child_region;
|
void AddChildCodeSpace(CodeBlock* child, size_t child_size)
|
||||||
m_child->region_size = size;
|
{
|
||||||
m_child->ResetCodePtr();
|
u8* child_region = AllocChildCodeSpace(child_size);
|
||||||
parent_region_size = region_size - size;
|
child->m_is_child = true;
|
||||||
|
child->region = child_region;
|
||||||
|
child->region_size = child_size;
|
||||||
|
child->total_region_size = child_size;
|
||||||
|
child->ResetCodePtr();
|
||||||
|
m_children.emplace_back(child);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user