sys_mmapper: Fix IPC-enabled instances memory consumption

This commit is contained in:
Eladash 2021-05-22 05:54:52 +03:00 committed by Ivan
parent 5db38c0127
commit 9ba3e6d3f7
2 changed files with 24 additions and 49 deletions

View File

@ -42,6 +42,17 @@ lv2_memory::lv2_memory(u32 size, u32 align, u64 flags, u64 key, bool pshared, lv
#endif #endif
} }
CellError lv2_memory::on_id_create()
{
if (!exists && !ct->take(size))
{
return CELL_ENOMEM;
}
exists++;
return {};
}
template <bool exclusive = false> template <bool exclusive = false>
error_code create_lv2_shm(bool pshared, u64 ipc_key, u64 size, u32 align, u64 flags, lv2_memory_container* ct) error_code create_lv2_shm(bool pshared, u64 ipc_key, u64 size, u32 align, u64 flags, lv2_memory_container* ct)
{ {
@ -168,14 +179,8 @@ error_code sys_mmapper_allocate_shared_memory(ppu_thread& ppu, u64 ipc_key, u64
// Get "default" memory container // Get "default" memory container
auto& dct = g_fxo->get<lv2_memory_container>(); auto& dct = g_fxo->get<lv2_memory_container>();
if (!dct.take(size))
{
return CELL_ENOMEM;
}
if (auto error = create_lv2_shm(ipc_key != SYS_MMAPPER_NO_SHM_KEY, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, &dct)) if (auto error = create_lv2_shm(ipc_key != SYS_MMAPPER_NO_SHM_KEY, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, &dct))
{ {
dct.used -= size;
return error; return error;
} }
@ -222,30 +227,15 @@ error_code sys_mmapper_allocate_shared_memory_from_container(ppu_thread& ppu, u6
} }
} }
const auto ct = idm::get<lv2_memory_container>(cid, [&](lv2_memory_container& ct) -> CellError const auto ct = idm::get<lv2_memory_container>(cid);
{
// Try to get "physical memory"
if (!ct.take(size))
{
return CELL_ENOMEM;
}
return {};
});
if (!ct) if (!ct)
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
if (ct.ret) if (auto error = create_lv2_shm(ipc_key != SYS_MMAPPER_NO_SHM_KEY, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, ct.get()))
{ {
return ct.ret;
}
if (auto error = create_lv2_shm(ipc_key != SYS_MMAPPER_NO_SHM_KEY, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, ct.ptr.get()))
{
ct->used -= size;
return error; return error;
} }
@ -342,14 +332,8 @@ error_code sys_mmapper_allocate_shared_memory_ext(ppu_thread& ppu, u64 ipc_key,
// Get "default" memory container // Get "default" memory container
auto& dct = g_fxo->get<lv2_memory_container>(); auto& dct = g_fxo->get<lv2_memory_container>();
if (!dct.take(size))
{
return CELL_ENOMEM;
}
if (auto error = create_lv2_shm<true>(true, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, &dct)) if (auto error = create_lv2_shm<true>(true, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, &dct))
{ {
dct.used -= size;
return error; return error;
} }
@ -438,30 +422,15 @@ error_code sys_mmapper_allocate_shared_memory_from_container_ext(ppu_thread& ppu
} }
} }
const auto ct = idm::get<lv2_memory_container>(cid, [&](lv2_memory_container& ct) -> CellError const auto ct = idm::get<lv2_memory_container>(cid);
{
// Try to get "physical memory"
if (!ct.take(size))
{
return CELL_ENOMEM;
}
return {};
});
if (!ct) if (!ct)
{ {
return CELL_ESRCH; return CELL_ESRCH;
} }
if (ct.ret) if (auto error = create_lv2_shm<true>(true, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, ct.get()))
{ {
return ct.ret;
}
if (auto error = create_lv2_shm<true>(true, ipc_key, size, flags & SYS_MEMORY_PAGE_SIZE_64K ? 0x10000 : 0x100000, flags, ct.ptr.get()))
{
ct->used -= size;
return error; return error;
} }
@ -550,6 +519,13 @@ error_code sys_mmapper_free_shared_memory(ppu_thread& ppu, u32 mem_id)
} }
lv2_obj::on_id_destroy(mem, mem.key, +mem.pshared); lv2_obj::on_id_destroy(mem, mem.key, +mem.pshared);
if (!mem.exists)
{
// Return "physical memory" to the memory container
mem.ct->used -= mem.size;
}
return {}; return {};
}); });
@ -563,9 +539,6 @@ error_code sys_mmapper_free_shared_memory(ppu_thread& ppu, u32 mem_id)
return mem.ret; return mem.ret;
} }
// Return "physical memory" to the memory container
mem->ct->used -= mem->size;
return CELL_OK; return CELL_OK;
} }

View File

@ -29,6 +29,8 @@ struct lv2_memory : lv2_obj
atomic_t<u32> counter{0}; atomic_t<u32> counter{0};
lv2_memory(u32 size, u32 align, u64 flags, u64 key, bool pshared, lv2_memory_container* ct); lv2_memory(u32 size, u32 align, u64 flags, u64 key, bool pshared, lv2_memory_container* ct);
CellError on_id_create();
}; };
enum : u64 enum : u64