Implement reserved memory in virtual memory block

This commit is contained in:
elisha464 2014-01-21 19:55:48 +02:00
parent 4e4dd43646
commit 66a481d4bc
2 changed files with 38 additions and 4 deletions

View File

@ -516,7 +516,7 @@ VirtualMemoryBlock::VirtualMemoryBlock() : MemoryBlock()
bool VirtualMemoryBlock::IsInMyRange(const u64 addr) bool VirtualMemoryBlock::IsInMyRange(const u64 addr)
{ {
return addr >= GetStartAddr() && addr < GetStartAddr() + GetSize(); return addr >= GetStartAddr() && addr < GetStartAddr() + GetSize() - GetResevedAmount();
} }
bool VirtualMemoryBlock::IsInMyRange(const u64 addr, const u32 size) bool VirtualMemoryBlock::IsInMyRange(const u64 addr, const u32 size)
@ -549,10 +549,11 @@ u64 VirtualMemoryBlock::Map(u64 realaddr, u32 size, u64 addr)
} }
else else
{ {
for(u64 addr = GetStartAddr(); addr <= GetEndAddr() - size;) for(u64 addr = GetStartAddr(); addr <= GetEndAddr() - GetResevedAmount() - size;)
{ {
bool is_good_addr = true; bool is_good_addr = true;
// check if address is already mapped
for(u32 i=0; i<m_mapped_memory.GetCount(); ++i) for(u32 i=0; i<m_mapped_memory.GetCount(); ++i)
{ {
if((addr >= m_mapped_memory[i].addr && addr < m_mapped_memory[i].addr + m_mapped_memory[i].size) || if((addr >= m_mapped_memory[i].addr && addr < m_mapped_memory[i].addr + m_mapped_memory[i].size) ||
@ -579,7 +580,7 @@ bool VirtualMemoryBlock::UnmapRealAddress(u64 realaddr)
{ {
for(u32 i=0; i<m_mapped_memory.GetCount(); ++i) for(u32 i=0; i<m_mapped_memory.GetCount(); ++i)
{ {
if(m_mapped_memory[i].realAddress == realaddr) if(m_mapped_memory[i].realAddress == realaddr && IsInMyRange(m_mapped_memory[i].addr, m_mapped_memory[i].size))
{ {
m_mapped_memory.RemoveAt(i); m_mapped_memory.RemoveAt(i);
return true; return true;
@ -593,7 +594,7 @@ bool VirtualMemoryBlock::UnmapAddress(u64 addr)
{ {
for(u32 i=0; i<m_mapped_memory.GetCount(); ++i) for(u32 i=0; i<m_mapped_memory.GetCount(); ++i)
{ {
if(m_mapped_memory[i].addr == addr) if(m_mapped_memory[i].addr == addr && IsInMyRange(m_mapped_memory[i].addr, m_mapped_memory[i].size))
{ {
m_mapped_memory.RemoveAt(i); m_mapped_memory.RemoveAt(i);
return true; return true;
@ -704,4 +705,27 @@ void VirtualMemoryBlock::Delete()
m_mapped_memory.Clear(); m_mapped_memory.Clear();
MemoryBlock::Delete(); MemoryBlock::Delete();
}
bool VirtualMemoryBlock::Reserve(u32 size)
{
if(size + GetResevedAmount() > GetEndAddr() - GetStartAddr())
return false;
m_reserve_size += size;
return true;
}
bool VirtualMemoryBlock::Unreserve(u32 size)
{
if(size > GetResevedAmount())
return false;
m_reserve_size -= size;
return true;
}
u32 VirtualMemoryBlock::GetResevedAmount()
{
return m_reserve_size;
} }

View File

@ -226,6 +226,7 @@ private:
class VirtualMemoryBlock : public MemoryBlock class VirtualMemoryBlock : public MemoryBlock
{ {
Array<VirtualMemInfo> m_mapped_memory; Array<VirtualMemInfo> m_mapped_memory;
u32 m_reserve_size;
public: public:
VirtualMemoryBlock(); VirtualMemoryBlock();
@ -245,6 +246,15 @@ public:
// Unmap address (please specify only starting point, no midway memory will be unmapped) // Unmap address (please specify only starting point, no midway memory will be unmapped)
virtual bool UnmapAddress(u64 addr); virtual bool UnmapAddress(u64 addr);
// Reserve a certain amount so no one can use it, returns true on succces, false on failure
virtual bool Reserve(u32 size);
// Unreserve a certain amount of bytes, returns true on succcess, false if size is bigger than the reserved amount
virtual bool Unreserve(u32 size);
// Return the total amount of reserved memory
virtual u32 GetResevedAmount();
virtual bool Read8(const u64 addr, u8* value); virtual bool Read8(const u64 addr, u8* value);
virtual bool Read16(const u64 addr, u16* value); virtual bool Read16(const u64 addr, u16* value);
virtual bool Read32(const u64 addr, u32* value); virtual bool Read32(const u64 addr, u32* value);