ams: prefer construct_at/destroy_at over placement new/explicit destructor

This commit is contained in:
Michael Scire
2021-03-21 20:30:40 -07:00
parent aff0da9427
commit d84dcb653d
49 changed files with 217 additions and 171 deletions

View File

@@ -85,8 +85,8 @@ namespace ams::creport {
this->heap_handle = lmem::CreateExpHeap(this->heap_storage, sizeof(this->heap_storage), lmem::CreateOption_None);
/* Allocate members. */
this->module_list = new (lmem::AllocateFromExpHeap(this->heap_handle, sizeof(ModuleList))) ModuleList;
this->thread_list = new (lmem::AllocateFromExpHeap(this->heap_handle, sizeof(ThreadList))) ThreadList;
this->module_list = std::construct_at(static_cast<ModuleList *>(lmem::AllocateFromExpHeap(this->heap_handle, sizeof(ModuleList))));
this->thread_list = std::construct_at(static_cast<ThreadList *>(lmem::AllocateFromExpHeap(this->heap_handle, sizeof(ThreadList))));
this->dying_message = static_cast<u8 *>(lmem::AllocateFromExpHeap(this->heap_handle, DyingMessageSizeMax));
if (this->dying_message != nullptr) {
std::memset(this->dying_message, 0, DyingMessageSizeMax);
@@ -321,8 +321,8 @@ namespace ams::creport {
}
/* Finalize our heap. */
this->module_list->~ModuleList();
this->thread_list->~ThreadList();
std::destroy_at(this->module_list);
std::destroy_at(this->thread_list);
lmem::FreeToExpHeap(this->heap_handle, this->module_list);
lmem::FreeToExpHeap(this->heap_handle, this->thread_list);
if (this->dying_message != nullptr) {

View File

@@ -66,13 +66,13 @@ namespace ams::dmnt::cheat::impl {
FrozenAddressMapEntry *AllocateFrozenAddress(u64 address, FrozenAddressValue value) {
FrozenAddressMapEntry *entry = static_cast<FrozenAddressMapEntry *>(lmem::AllocateFromUnitHeap(g_frozen_address_map_heap));
if (entry != nullptr) {
new (entry) FrozenAddressMapEntry(address, value);
std::construct_at(entry, address, value);
}
return entry;
}
void DeallocateFrozenAddress(FrozenAddressMapEntry *entry) {
entry->~FrozenAddressMapEntry();
std::destroy_at(entry);
lmem::FreeToUnitHeap(g_frozen_address_map_heap, entry);
}
@@ -1160,7 +1160,7 @@ namespace ams::dmnt::cheat::impl {
g_frozen_address_map_heap = lmem::CreateUnitHeap(g_frozen_address_map_memory, sizeof(g_frozen_address_map_memory), sizeof(FrozenAddressMapEntry), lmem::CreateOption_ThreadSafe);
/* Create the cheat process manager (spawning its threads). */
new (GetPointer(g_cheat_process_manager)) CheatProcessManager;
util::ConstructAt(g_cheat_process_manager);
}
bool GetHasActiveCheatProcess() {

View File

@@ -148,7 +148,7 @@ namespace ams::dmnt::cheat::impl {
}
void InitializeDebugEventsManager() {
new (GetPointer(g_events_manager)) DebugEventsManager;
util::ConstructAt(g_events_manager);
}
Result ContinueCheatProcess(Handle cheat_dbg_hnd) {

View File

@@ -91,15 +91,20 @@ namespace ams::pm::impl {
std::memset(this->process_info_allocated, 0, sizeof(this->process_info_allocated));
}
void *AllocateProcessInfoStorage() {
template<typename... Args>
ProcessInfo *AllocateProcessInfo(Args &&... args) {
std::scoped_lock lk(this->lock);
for (size_t i = 0; i < MaxProcessInfos; i++) {
if (!this->process_info_allocated[i]) {
this->process_info_allocated[i] = true;
std::memset(&this->process_info_storages[i], 0, sizeof(this->process_info_storages[i]));
return GetPointer(this->process_info_storages[i]);
std::memset(this->process_info_storages + i, 0, sizeof(this->process_info_storages[i]));
return util::ConstructAt(this->process_info_storages[i], std::forward<Args>(args)...);
}
}
return nullptr;
}
@@ -110,7 +115,7 @@ namespace ams::pm::impl {
AMS_ABORT_UNLESS(index < MaxProcessInfos);
AMS_ABORT_UNLESS(this->process_info_allocated[index]);
process_info->~ProcessInfo();
util::DestroyAt(this->process_info_storages[index]);
this->process_info_allocated[index] = false;
}
};
@@ -251,9 +256,8 @@ namespace ams::pm::impl {
os::ProcessId process_id = os::GetProcessId(process_handle);
/* Make new process info. */
void *process_info_storage = g_process_info_allocator.AllocateProcessInfoStorage();
AMS_ABORT_UNLESS(process_info_storage != nullptr);
ProcessInfo *process_info = new (process_info_storage) ProcessInfo(process_handle, process_id, pin_id, location, override_status);
ProcessInfo *process_info = g_process_info_allocator.AllocateProcessInfo(process_handle, process_id, pin_id, location, override_status);
AMS_ABORT_UNLESS(process_info != nullptr);
/* Link new process info. */
{