diff options
author | FearlessTobi <thm.frey@gmail.com> | 2024-01-18 23:08:37 +0100 |
---|---|---|
committer | Liam <byteslice@airmail.cc> | 2024-01-25 22:42:06 +0100 |
commit | 2c049ae06dbbdfff7542c23ca4d748879f4beb71 (patch) | |
tree | f93c8be5b6d1781a69d8c28e6aa3500d34c8b55b /src/core/file_sys/fs_memory_management.h | |
parent | result: Make fully constexpr, add ON_RESULT_INCLUDED (diff) | |
download | yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.gz yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.bz2 yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.lz yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.xz yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.tar.zst yuzu-2c049ae06dbbdfff7542c23ca4d748879f4beb71.zip |
Diffstat (limited to 'src/core/file_sys/fs_memory_management.h')
-rw-r--r-- | src/core/file_sys/fs_memory_management.h | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/core/file_sys/fs_memory_management.h b/src/core/file_sys/fs_memory_management.h new file mode 100644 index 000000000..596143ba9 --- /dev/null +++ b/src/core/file_sys/fs_memory_management.h @@ -0,0 +1,48 @@ +// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include <mutex> +#include "common/alignment.h" + +namespace FileSys { + +std::mutex g_mutex; + +constexpr size_t RequiredAlignment = alignof(u64); + +void* AllocateUnsafe(size_t size) { + /* Allocate. */ + void* const ptr = ::operator new(size, std::align_val_t{RequiredAlignment}); + + /* Check alignment. */ + ASSERT(Common::IsAligned(reinterpret_cast<uintptr_t>(ptr), RequiredAlignment)); + + /* Return allocated pointer. */ + return ptr; +} + +void DeallocateUnsafe(void* ptr, size_t size) { + /* Deallocate the pointer. */ + ::operator delete(ptr, std::align_val_t{RequiredAlignment}); +} + +void* Allocate(size_t size) { + /* Lock the allocator. */ + std::scoped_lock lk(g_mutex); + + return AllocateUnsafe(size); +} + +void Deallocate(void* ptr, size_t size) { + /* If the pointer is non-null, deallocate it. */ + if (ptr != nullptr) { + /* Lock the allocator. */ + std::scoped_lock lk(g_mutex); + + DeallocateUnsafe(ptr, size); + } +} + +} // namespace FileSys |