mem: implement most of StandardAllocator
This was tested using `https://github.com/node-dot-cpp/alloc-test` plus a few other by-hand tests. It seems to work for the case we care about (sysmodules without thread cache-ing). External users are advised to build with assertions on and contact SciresM if you find issues. This is a lot of code to have gotten right in one go, and it was written mostly after midnight while sick, so there are probably un-noticed issues.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <stratosphere/mem/impl/mem_impl_common.hpp>
|
||||
|
||||
namespace ams::mem::impl::heap {
|
||||
|
||||
class TlsHeapCache;
|
||||
|
||||
class CachedHeap final {
|
||||
NON_COPYABLE(CachedHeap);
|
||||
private:
|
||||
TlsHeapCache *tls_heap_cache;
|
||||
public:
|
||||
constexpr CachedHeap() : tls_heap_cache() { /* ... */ }
|
||||
~CachedHeap() { this->Finalize(); }
|
||||
|
||||
ALWAYS_INLINE CachedHeap(CachedHeap &&rhs) : tls_heap_cache(rhs.tls_heap_cache) {
|
||||
rhs.tls_heap_cache = nullptr;
|
||||
}
|
||||
ALWAYS_INLINE CachedHeap &operator=(CachedHeap &&rhs) {
|
||||
this->Reset();
|
||||
this->tls_heap_cache = rhs.tls_heap_cache;
|
||||
rhs.tls_heap_cache = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
void *Allocate(size_t n);
|
||||
void *Allocate(size_t n, size_t align);
|
||||
size_t GetAllocationSize(const void *ptr);
|
||||
errno_t Free(void *p);
|
||||
errno_t FreeWithSize(void *p, size_t size);
|
||||
errno_t Reallocate(void *ptr, size_t size, void **p);
|
||||
errno_t Shrink(void *ptr, size_t size);
|
||||
|
||||
void ReleaseAllCache();
|
||||
void Finalize();
|
||||
bool CheckCache();
|
||||
errno_t QueryV(int query, std::va_list vl);
|
||||
errno_t Query(int query, ...);
|
||||
|
||||
void Reset() { this->Finalize(); }
|
||||
void Reset(TlsHeapCache *thc);
|
||||
TlsHeapCache *Release();
|
||||
|
||||
constexpr explicit ALWAYS_INLINE operator bool() const { return this->tls_heap_cache != nullptr; }
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <stratosphere/mem/impl/mem_impl_common.hpp>
|
||||
#include <stratosphere/mem/impl/mem_impl_declarations.hpp>
|
||||
|
||||
namespace ams::mem::impl::heap {
|
||||
|
||||
class CachedHeap;
|
||||
class TlsHeapCentral;
|
||||
|
||||
using HeapWalkCallback = int (*)(void *ptr, size_t size, void *user_data);
|
||||
|
||||
class CentralHeap final {
|
||||
NON_COPYABLE(CentralHeap);
|
||||
NON_MOVEABLE(CentralHeap);
|
||||
public:
|
||||
static constexpr size_t PageSize = 4_KB;
|
||||
static constexpr size_t MinimumAlignment = alignof(u64);
|
||||
using DestructorHandler = void (*)(void *start, void *end);
|
||||
private:
|
||||
TlsHeapCentral *tls_heap_central;
|
||||
bool use_virtual_memory;
|
||||
u32 option;
|
||||
u8 *start;
|
||||
u8 *end;
|
||||
public:
|
||||
constexpr CentralHeap() : tls_heap_central(), use_virtual_memory(), option(), start(), end() { /* ... */ }
|
||||
~CentralHeap() { this->Finalize(); }
|
||||
|
||||
errno_t Initialize(void *start, size_t size, u32 option);
|
||||
void Finalize();
|
||||
|
||||
ALWAYS_INLINE void *Allocate(size_t n) { return this->Allocate(n, MinimumAlignment); }
|
||||
void *Allocate(size_t n, size_t align);
|
||||
size_t GetAllocationSize(const void *ptr);
|
||||
errno_t Free(void *p);
|
||||
errno_t FreeWithSize(void *p, size_t size);
|
||||
errno_t Reallocate(void *ptr, size_t size, void **p);
|
||||
errno_t Shrink(void *ptr, size_t size);
|
||||
|
||||
bool MakeCache(CachedHeap *cached_heap);
|
||||
errno_t WalkAllocatedPointers(HeapWalkCallback callback, void *user_data);
|
||||
errno_t QueryV(int query, std::va_list vl);
|
||||
errno_t Query(int query, ...);
|
||||
private:
|
||||
errno_t QueryVImpl(int query, std::va_list *vl_ptr);
|
||||
};
|
||||
|
||||
static_assert(sizeof(CentralHeap) <= sizeof(::ams::mem::impl::InternalCentralHeapStorage));
|
||||
static_assert(alignof(CentralHeap) <= alignof(void *));
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <vapours.hpp>
|
||||
#include <errno.h>
|
||||
|
||||
namespace ams::mem::impl {
|
||||
|
||||
constexpr inline size_t MaxSize = static_cast<size_t>(std::numeric_limits<s64>::max());
|
||||
|
||||
using errno_t = int;
|
||||
|
||||
enum DumpMode {
|
||||
DumpMode_Basic = (0 << 0),
|
||||
DumpMode_Spans = (1 << 0),
|
||||
DumpMode_Pointers = (1 << 1),
|
||||
DumpMode_Pages = (1 << 2),
|
||||
DumpMode_All = (DumpMode_Pages | DumpMode_Pointers | DumpMode_Spans | DumpMode_Basic),
|
||||
};
|
||||
|
||||
enum AllocQuery {
|
||||
AllocQuery_Dump = 0,
|
||||
AllocQuery_PageSize = 1,
|
||||
AllocQuery_AllocatedSize = 2,
|
||||
AllocQuery_FreeSize = 3,
|
||||
AllocQuery_SystemSize = 4,
|
||||
AllocQuery_MaxAllocatableSize = 5,
|
||||
AllocQuery_IsClean = 6,
|
||||
AllocQuery_HeapHash = 7,
|
||||
AllocQuery_UnifyFreeList = 8,
|
||||
AllocQuery_SetColor = 9,
|
||||
AllocQuery_GetColor = 10,
|
||||
AllocQuery_SetName = 11,
|
||||
AllocQuery_GetName = 12,
|
||||
/* AllocQuery_Thirteen = 13, */
|
||||
AllocQuery_CheckCache = 14,
|
||||
AllocQuery_ClearCache = 15,
|
||||
AllocQuery_FinalizeCache = 16,
|
||||
AllocQuery_FreeSizeMapped = 17,
|
||||
AllocQuery_MaxAllocatableSizeMapped = 18,
|
||||
AllocQuery_DumpJson = 19,
|
||||
};
|
||||
|
||||
enum HeapOption {
|
||||
HeapOption_UseEnvironment = (1 << 0),
|
||||
HeapOption_DisableCache = (1 << 2),
|
||||
};
|
||||
|
||||
struct HeapHash {
|
||||
size_t alloc_count;
|
||||
size_t alloc_size;
|
||||
size_t hash;
|
||||
};
|
||||
static_assert(std::is_pod<HeapHash>::value);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ams::mem::impl {
|
||||
|
||||
namespace heap {
|
||||
|
||||
class CentralHeap;
|
||||
|
||||
}
|
||||
|
||||
using InternalCentralHeapStorage = ::ams::util::TypedStorage<::ams::mem::impl::heap::CentralHeap, sizeof(void *) * 6, alignof(void *)>;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
* Copyright (c) 2018-2020 Atmosphère-NX
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stratosphere/mem/impl/mem_impl_common.hpp>
|
||||
#include <stratosphere/mem/impl/heap/mem_impl_heap_cached_heap.hpp>
|
||||
#include <stratosphere/mem/impl/heap/mem_impl_heap_central_heap.hpp>
|
||||
Reference in New Issue
Block a user