Improve lv2_memory object

This commit is contained in:
Nekotekina 2017-09-12 18:33:08 +03:00
parent 0e691e2c51
commit 22d8b57027
5 changed files with 35 additions and 15 deletions

View File

@ -123,7 +123,7 @@ error_code sys_memory_free(u32 addr)
verify(HERE), area; verify(HERE), area;
// Deallocate memory // Deallocate memory
u32 cid, size = area->dealloc(addr, &cid); u32 cid, size = area->dealloc(addr, nullptr, &cid);
if (!size) if (!size)
{ {

View File

@ -255,7 +255,7 @@ error_code sys_mmapper_map_shared_memory(u32 addr, u32 mem_id, u64 flags)
return CELL_OK; return CELL_OK;
} }
if (!area->falloc(addr, mem->size)) if (!area->falloc(addr, mem->size, mem->data.data()))
{ {
mem->addr = 0; mem->addr = 0;
return CELL_EBUSY; return CELL_EBUSY;
@ -289,7 +289,7 @@ error_code sys_mmapper_search_and_map(u32 start_addr, u32 mem_id, u64 flags, vm:
return CELL_OK; return CELL_OK;
} }
const u32 addr = area->alloc(mem->size, mem->align); const u32 addr = area->alloc(mem->size, mem->align, mem->data.data());
if (!addr) if (!addr)
{ {
@ -328,7 +328,7 @@ error_code sys_mmapper_unmap_shared_memory(u32 addr, vm::ptr<u32> mem_id)
return CELL_EINVAL; return CELL_EINVAL;
} }
verify(HERE), area->dealloc(addr), mem->addr.exchange(0) == addr; verify(HERE), area->dealloc(addr, mem->data.data()), mem->addr.exchange(0) == addr;
return CELL_OK; return CELL_OK;
} }

View File

@ -14,12 +14,15 @@ struct lv2_memory : lv2_obj
atomic_t<u32> addr{}; // Actual mapping address atomic_t<u32> addr{}; // Actual mapping address
std::vector<uchar> data;
lv2_memory(u32 size, u32 align, u64 flags, const std::shared_ptr<lv2_memory_container>& ct) lv2_memory(u32 size, u32 align, u64 flags, const std::shared_ptr<lv2_memory_container>& ct)
: size(size) : size(size)
, align(align) , align(align)
, flags(flags) , flags(flags)
, ct(ct) , ct(ct)
{ {
data.resize(size);
} }
}; };

View File

@ -491,7 +491,7 @@ namespace vm
fmt::throw_exception("Invalid memory location (%u)" HERE, (uint)location); fmt::throw_exception("Invalid memory location (%u)" HERE, (uint)location);
} }
return block->alloc(size, align, sup); return block->alloc(size, align, nullptr, sup);
} }
u32 falloc(u32 addr, u32 size, memory_location_t location, u32 sup) u32 falloc(u32 addr, u32 size, memory_location_t location, u32 sup)
@ -503,7 +503,7 @@ namespace vm
fmt::throw_exception("Invalid memory location (%u, addr=0x%x)" HERE, (uint)location, addr); fmt::throw_exception("Invalid memory location (%u, addr=0x%x)" HERE, (uint)location, addr);
} }
return block->falloc(addr, size, sup); return block->falloc(addr, size, nullptr, sup);
} }
u32 dealloc(u32 addr, memory_location_t location, u32* sup_out) u32 dealloc(u32 addr, memory_location_t location, u32* sup_out)
@ -515,7 +515,7 @@ namespace vm
fmt::throw_exception("Invalid memory location (%u, addr=0x%x)" HERE, (uint)location, addr); fmt::throw_exception("Invalid memory location (%u, addr=0x%x)" HERE, (uint)location, addr);
} }
return block->dealloc(addr, sup_out); return block->dealloc(addr, nullptr, sup_out);
} }
void dealloc_verbose_nothrow(u32 addr, memory_location_t location) noexcept void dealloc_verbose_nothrow(u32 addr, memory_location_t location) noexcept
@ -576,12 +576,12 @@ namespace vm
} }
} }
u32 block_t::alloc(u32 size, u32 align, u32 sup) u32 block_t::alloc(const u32 orig_size, u32 align, const uchar* data, u32 sup)
{ {
writer_lock lock; writer_lock lock;
// Align to minimal page size // Align to minimal page size
size = ::align(size, 4096); const u32 size = ::align(orig_size, 4096);
// Check alignment (it's page allocation, so passing small values there is just silly) // Check alignment (it's page allocation, so passing small values there is just silly)
if (align < 4096 || align != (0x80000000u >> cntlz32(align, true))) if (align < 4096 || align != (0x80000000u >> cntlz32(align, true)))
@ -611,6 +611,11 @@ namespace vm
{ {
if (try_alloc(addr, size, pflags, sup)) if (try_alloc(addr, size, pflags, sup))
{ {
if (data)
{
std::memcpy(vm::base(addr), data, orig_size);
}
return addr; return addr;
} }
} }
@ -618,19 +623,21 @@ namespace vm
return 0; return 0;
} }
u32 block_t::falloc(u32 addr, u32 size, u32 sup) u32 block_t::falloc(u32 addr, const u32 orig_size, const uchar* data, u32 sup)
{ {
writer_lock lock; writer_lock lock;
// align to minimal page size // align to minimal page size
size = ::align(size, 4096); const u32 size = ::align(orig_size, 4096);
// return if addr or size is invalid // return if addr or size is invalid
if (!size || size > this->size || addr < this->addr || addr + size - 1 > this->addr + this->size - 1) if (!size || size > this->size || addr < this->addr || addr + size - 1 > this->addr + this->size - 1)
{ {
return 0; return 0;
} }
u8 pflags = page_readable | page_writable; u8 pflags = page_readable | page_writable;
if ((flags & SYS_MEMORY_PAGE_SIZE_1M) == SYS_MEMORY_PAGE_SIZE_1M) if ((flags & SYS_MEMORY_PAGE_SIZE_1M) == SYS_MEMORY_PAGE_SIZE_1M)
{ {
pflags |= page_1m_size; pflags |= page_1m_size;
@ -645,10 +652,15 @@ namespace vm
return 0; return 0;
} }
if (data)
{
std::memcpy(vm::base(addr), data, orig_size);
}
return addr; return addr;
} }
u32 block_t::dealloc(u32 addr, u32* sup_out) u32 block_t::dealloc(u32 addr, uchar* data_out, u32* sup_out)
{ {
writer_lock lock; writer_lock lock;
@ -662,6 +674,11 @@ namespace vm
// Remove entry // Remove entry
m_map.erase(found); m_map.erase(found);
if (data_out)
{
std::memcpy(data_out, vm::base(addr), size);
}
// Unmap "real" memory pages // Unmap "real" memory pages
_page_unmap(addr, size); _page_unmap(addr, size);

View File

@ -148,13 +148,13 @@ namespace vm
const u64 flags; // Currently unused const u64 flags; // Currently unused
// Search and map memory (don't pass alignment smaller than 4096) // Search and map memory (don't pass alignment smaller than 4096)
u32 alloc(u32 size, u32 align = 4096, u32 sup = 0); u32 alloc(u32 size, u32 align = 4096, const uchar* data = nullptr, u32 sup = 0);
// Try to map memory at fixed location // Try to map memory at fixed location
u32 falloc(u32 addr, u32 size, u32 sup = 0); u32 falloc(u32 addr, u32 size, const uchar* data = nullptr, u32 sup = 0);
// Unmap memory at specified location previously returned by alloc(), return size // Unmap memory at specified location previously returned by alloc(), return size
u32 dealloc(u32 addr, u32* sup_out = nullptr); u32 dealloc(u32 addr, uchar* data_out = nullptr, u32* sup_out = nullptr);
// Internal // Internal
u32 imp_used(const vm::writer_lock&); u32 imp_used(const vm::writer_lock&);