lmem: Implement UnitHeap, ExpHeap (#823)

* lmem: implement ExpHeap

* lmem: Implement Slab^H^H^H^HUnitHeap
This commit is contained in:
SciresM 2020-02-24 19:44:27 -08:00 committed by GitHub
parent 191414c832
commit 5a8aebf74d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 1705 additions and 0 deletions

View file

@ -0,0 +1,90 @@
/*
* Copyright (c) 2018-2019 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 "../../os.hpp"
namespace ams::lmem::impl {
/* NOTE: Nintendo does not use util::IntrusiveListNode. */
/* They seem to manually manage linked list pointers. */
/* This is pretty gross, so we're going to use util::IntrusiveListNode. */
struct ExpHeapMemoryBlockHead {
u16 magic;
u32 attributes;
size_t block_size;
util::IntrusiveListNode list_node;
};
static_assert(std::is_trivially_destructible<ExpHeapMemoryBlockHead>::value);
using ExpHeapMemoryBlockList = typename util::IntrusiveListMemberTraits<&ExpHeapMemoryBlockHead::list_node>::ListType;
struct ExpHeapHead {
ExpHeapMemoryBlockList free_list;
ExpHeapMemoryBlockList used_list;
u16 group_id;
u16 mode;
bool use_alignment_margins;
char pad[3];
};
static_assert(sizeof(ExpHeapHead) == 0x28);
static_assert(std::is_trivially_destructible<ExpHeapHead>::value);
struct FrameHeapHead {
void *next_block_head;
void *next_block_tail;
};
static_assert(sizeof(FrameHeapHead) == 0x10);
static_assert(std::is_trivially_destructible<FrameHeapHead>::value);
struct UnitHead {
UnitHead *next;
};
struct UnitHeapList {
UnitHead *head;
};
struct UnitHeapHead {
UnitHeapList free_list;
size_t unit_size;
s32 alignment;
s32 num_units;
};
static_assert(sizeof(UnitHeapHead) == 0x18);
static_assert(std::is_trivially_destructible<UnitHeapHead>::value);
union ImplementationHeapHead {
ExpHeapHead exp_heap_head;
FrameHeapHead frame_heap_head;
UnitHeapHead unit_heap_head;
};
struct HeapHead {
u32 magic;
util::IntrusiveListNode list_node;
typename util::IntrusiveListMemberTraits<&HeapHead::list_node>::ListType child_list;
void *heap_start;
void *heap_end;
os::Mutex mutex;
u8 option;
ImplementationHeapHead impl_head;
};
static_assert(std::is_trivially_destructible<HeapHead>::value);
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (c) 2018-2019 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 "impl/lmem_impl_common.hpp"
namespace ams::lmem {
enum CreateOption {
CreateOption_None = (0),
CreateOption_ZeroClear = (1 << 0),
CreateOption_DebugFill = (1 << 1),
CreateOption_ThreadSafe = (1 << 2),
};
enum FillType {
FillType_Unallocated,
FillType_Allocated,
FillType_Freed,
FillType_Count,
};
namespace impl {
struct HeapHead;
}
using HeapHandle = impl::HeapHead *;
using HeapCommonHead = impl::HeapHead;
struct MemoryRange {
uintptr_t address;
size_t size;
};
constexpr inline s32 DefaultAlignment = 0x8;
/* Common API. */
u32 GetDebugFillValue(FillType fill_type);
void SetDebugFillValue(FillType fill_type, u32 value);
size_t GetTotalSize(HeapHandle handle);
void *GetStartAddress(HeapHandle handle);
bool ContainsAddress(HeapHandle handle, const void *address);
}

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2018-2019 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 "lmem_common.hpp"
namespace ams::lmem {
enum AllocationMode {
AllocationMode_FirstFit,
AllocationMode_BestFit,
};
enum AllocationDirection {
AllocationDirection_Front,
AllocationDirection_Back,
};
using HeapVisitor = void (*)(void *block, HeapHandle handle, uintptr_t user_data);
HeapHandle CreateExpHeap(void *address, size_t size, u32 option);
void DestroyExpHeap(HeapHandle handle);
MemoryRange AdjustExpHeap(HeapHandle handle);
void *AllocateFromExpHeap(HeapHandle handle, size_t size);
void *AllocateFromExpHeap(HeapHandle handle, size_t size, s32 alignment);
void FreeToExpHeap(HeapHandle handle, void *block);
size_t ResizeExpHeapMemoryBlock(HeapHandle handle, void *block, size_t size);
size_t GetExpHeapTotalFreeSize(HeapHandle handle);
size_t GetExpHeapAllocatableSize(HeapHandle handle, s32 alignment);
AllocationMode GetExpHeapAllocationMode(HeapHandle handle);
AllocationMode SetExpHeapAllocationMode(HeapHandle handle, AllocationMode new_mode);
bool GetExpHeapUseMarginsOfAlignment(HeapHandle handle);
bool SetExpHeapUseMarginsOfAlignment(HeapHandle handle, bool use_margins);
u16 GetExpHeapGroupId(HeapHandle handle);
u16 SetExpHeapGroupId(HeapHandle handle, u16 group_id);
size_t GetExpHeapMemoryBlockSize(const void *memory_block);
u16 GetExpHeapMemoryBlockGroupId(const void *memory_block);
AllocationDirection GetExpHeapMemoryBlockAllocationDirection(const void *memory_block);
void VisitExpHeapAllocatedBlocks(HeapHandle handle, HeapVisitor visitor, uintptr_t user_data);
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2018-2019 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 "lmem_common.hpp"
namespace ams::lmem {
enum InfoPlacement {
InfoPlacement_Head,
InfoPlacement_Tail,
};
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option);
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option, s32 alignment, InfoPlacement info_placement);
HeapHandle CreateUnitHeap(void *address, size_t size, size_t unit_size, u32 option, s32 alignment, HeapCommonHead *heap_head);
void DestroyUnitHeap(HeapHandle handle);
void InvalidateUnitHeap(HeapHandle handle);
void ExtendUnitHeap(HeapHandle handle, size_t size);
void *AllocateFromUnitHeap(HeapHandle handle);
void FreeToUnitHeap(HeapHandle handle, void *block);
size_t GetUnitHeapUnitSize(HeapHandle handle);
s32 GetUnitHeapAlignment(HeapHandle handle);
size_t GetUnitHeapFreeCount(HeapHandle handle);
size_t GetUnitHeapUsedCount(HeapHandle handle);
size_t GetUnitHeapRequiredSize(size_t unit_size, size_t unit_count, s32 alignment, bool internal_metadata);
}