2020-03-08 09:06:23 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2020-03-08 09:06:23 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <stratosphere.hpp>
|
|
|
|
|
|
|
|
namespace ams::fs {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-12-07 05:34:10 +01:00
|
|
|
constinit bool g_used_default_allocator = false;
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
void *DefaultAllocate(size_t size) {
|
|
|
|
g_used_default_allocator = true;
|
2021-01-18 07:23:19 +01:00
|
|
|
return ams::Malloc(size);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void DefaultDeallocate(void *ptr, size_t size) {
|
2021-10-07 00:20:48 +02:00
|
|
|
AMS_UNUSED(size);
|
2021-01-18 07:23:19 +01:00
|
|
|
ams::Free(ptr);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
|
2021-12-07 05:34:10 +01:00
|
|
|
constinit os::SdkMutex g_mutex;
|
2021-01-18 07:23:19 +01:00
|
|
|
constinit AllocateFunction g_allocate_func = DefaultAllocate;
|
|
|
|
constinit DeallocateFunction g_deallocate_func = DefaultDeallocate;
|
2020-03-08 09:06:23 +01:00
|
|
|
|
|
|
|
constexpr size_t RequiredAlignment = alignof(u64);
|
|
|
|
|
|
|
|
Result SetAllocatorImpl(AllocateFunction allocator, DeallocateFunction deallocator) {
|
|
|
|
/* Ensure SetAllocator is used correctly. */
|
|
|
|
R_UNLESS(g_allocate_func == DefaultAllocate, fs::ResultAllocatorAlreadyRegistered());
|
|
|
|
R_UNLESS(g_deallocate_func == DefaultDeallocate, fs::ResultAllocatorAlreadyRegistered());
|
|
|
|
R_UNLESS(allocator != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(deallocator != nullptr, fs::ResultNullptrArgument());
|
|
|
|
R_UNLESS(!g_used_default_allocator, fs::ResultDefaultAllocatorUsed());
|
|
|
|
|
|
|
|
/* Set allocators. */
|
|
|
|
g_allocate_func = allocator;
|
|
|
|
g_deallocate_func = deallocator;
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetAllocator(AllocateFunction allocator, DeallocateFunction deallocator) {
|
|
|
|
R_ABORT_UNLESS(SetAllocatorImpl(allocator, deallocator));
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace impl {
|
|
|
|
|
2021-12-07 05:34:10 +01:00
|
|
|
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());
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
2021-12-07 05:34:10 +01:00
|
|
|
|
|
|
|
/* Return allocated pointer. */
|
2020-03-08 09:06:23 +01:00
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-12-07 05:34:10 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-03-08 09:06:23 +01:00
|
|
|
void Deallocate(void *ptr, size_t size) {
|
2021-12-07 05:34:10 +01:00
|
|
|
/* 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);
|
2020-03-08 09:06:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|