fs: implement newer allocator templates

This commit is contained in:
Michael Scire 2021-12-06 20:34:10 -08:00 committed by SciresM
parent ace409ccec
commit 64b4cc25fc
3 changed files with 144 additions and 16 deletions

View file

@ -19,7 +19,7 @@ namespace ams::fs {
namespace {
bool g_used_default_allocator;
constinit bool g_used_default_allocator = false;
void *DefaultAllocate(size_t size) {
g_used_default_allocator = true;
@ -31,7 +31,7 @@ namespace ams::fs {
ams::Free(ptr);
}
constinit os::SdkMutex g_lock;
constinit os::SdkMutex g_mutex;
constinit AllocateFunction g_allocate_func = DefaultAllocate;
constinit DeallocateFunction g_deallocate_func = DefaultDeallocate;
@ -59,26 +59,61 @@ namespace ams::fs {
namespace impl {
void *Allocate(size_t size) {
void *ptr;
{
std::scoped_lock lk(g_lock);
ptr = g_allocate_func(size);
if (!util::IsAligned(reinterpret_cast<uintptr_t>(ptr), RequiredAlignment)) {
R_ABORT_UNLESS(fs::ResultAllocatorAlignmentViolation());
}
void LockAllocatorMutex() {
g_mutex.Lock();
}
void UnlockAllocatorMutex() {
g_mutex.Unlock();
}
void *AllocateUnsafe(size_t size) {
/* Check pre-conditions. */
AMS_ASSERT(g_mutex.IsLockedByCurrentThread());
/* Allocate. */
void * const ptr = g_allocate_func(size);
/* Check alignment. */
if (AMS_UNLIKELY(!util::IsAligned(reinterpret_cast<uintptr_t>(ptr), RequiredAlignment))) {
R_ABORT_UNLESS(fs::ResultAllocatorAlignmentViolation());
}
/* Return allocated pointer. */
return ptr;
}
void Deallocate(void *ptr, size_t size) {
if (ptr == nullptr) {
return;
}
std::scoped_lock lk(g_lock);
void DeallocateUnsafe(void *ptr, size_t size) {
/* Check pre-conditions. */
AMS_ASSERT(g_mutex.IsLockedByCurrentThread());
/* Deallocate the pointer. */
g_deallocate_func(ptr, size);
}
void *Allocate(size_t size) {
/* Check pre-conditions. */
AMS_ASSERT(g_allocate_func != nullptr);
/* Lock the allocator. */
std::scoped_lock lk(g_mutex);
return AllocateUnsafe(size);
}
void Deallocate(void *ptr, size_t size) {
/* Check pre-conditions. */
AMS_ASSERT(g_deallocate_func != nullptr);
/* If the pointer is non-null, deallocate it. */
if (ptr != nullptr) {
/* Lock the allocator. */
std::scoped_lock lk(g_mutex);
DeallocateUnsafe(ptr, size);
}
}
}
}