From 29b6d586e669982715f2603e99c3be5c2c43dfb8 Mon Sep 17 00:00:00 2001 From: James Benton Date: Sun, 17 Jul 2016 22:31:12 +0100 Subject: [PATCH] Update frameheap to have correct structure. --- include/coreinit/frameheap.h | 27 +++++++++- include/coreinit/memheap.h | 101 +++++++++++++++++++++++++++++++++++ rpl/libcoreinit/exports.h | 6 +++ 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 include/coreinit/memheap.h diff --git a/include/coreinit/frameheap.h b/include/coreinit/frameheap.h index baef5e4..fd3df2a 100644 --- a/include/coreinit/frameheap.h +++ b/include/coreinit/frameheap.h @@ -1,5 +1,6 @@ #pragma once #include +#include "memheap.h" /** * \defgroup coreinit_frameheap Frame Heap @@ -18,16 +19,38 @@ typedef enum MEMFrameHeapFreeMode } MEMFrameHeapFreeMode; typedef struct MEMFrameHeap MEMFrameHeap; +typedef struct MEMFrameHeapState MEMFrameHeapState; + +struct MEMFrameHeapState +{ + uint32_t tag; + void *head; + void *tail; + MEMFrameHeapState *previous; +}; +CHECK_OFFSET(MEMFrameHeapState, 0x00, tag); +CHECK_OFFSET(MEMFrameHeapState, 0x04, head); +CHECK_OFFSET(MEMFrameHeapState, 0x08, tail); +CHECK_OFFSET(MEMFrameHeapState, 0x0C, previous); +CHECK_SIZE(MEMFrameHeapState, 0x10); struct MEMFrameHeap { + MEMHeapHeader header; + void *head; + void *tail; + MEMFrameHeapState *previousState; }; -UNKNOWN_SIZE(MEMFrameHeap); +CHECK_OFFSET(MEMFrameHeap, 0x00, header); +CHECK_OFFSET(MEMFrameHeap, 0x40, head); +CHECK_OFFSET(MEMFrameHeap, 0x44, tail); +CHECK_OFFSET(MEMFrameHeap, 0x48, previousState); +CHECK_SIZE(MEMFrameHeap, 0x4C); MEMFrameHeap * MEMCreateFrmHeapEx(MEMFrameHeap *heap, uint32_t size, - uint16_t flags); + uint32_t flags); void * MEMDestroyFrmHeap(MEMFrameHeap *heap); diff --git a/include/coreinit/memheap.h b/include/coreinit/memheap.h new file mode 100644 index 0000000..9951e8d --- /dev/null +++ b/include/coreinit/memheap.h @@ -0,0 +1,101 @@ +#pragma once +#include +#include "spinlock.h" +#include "memlist.h" + +/** + * \defgroup coreinit_memheap Common Memory Heap + * \ingroup coreinit + * + * Common memory heap fucntions. + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct MEMHeapHeader MEMHeapHeader; + +typedef enum MEMHeapFillType +{ + MEM_HEAP_FILL_TYPE_UNUSED = 0, + MEM_HEAP_FILL_TYPE_ALLOCATED = 1, + MEM_HEAP_FILL_TYPE_FREED = 2, +} MEMHeapFillType; + +#define MEM_BLOCK_HEAP_TAG 0x424C4B48u +#define MEM_EXPANDED_HEAP_TAG 0x45585048u +#define MEM_FRAME_HEAP_TAG 0x46524D48u +#define MEM_UNIT_HEAP_TAG 0x554E5448u +#define MEM_USER_HEAP_TAG 0x55535248u + + +#define MEM_HEAP_FLAG_ZERO_ALLOCATED (1 << 0) +#define MEM_HEAP_FLAG_DEBUG_MODE (1 << 1) +#define MEM_HEAP_FLAG_USE_LOCK (1 << 2) + +struct MEMHeapHeader +{ + //! Tag indicating which type of heap this is + uint32_t tag; + + //! Link for list this heap is in + MEMMemoryLink link; + + //! List of all child heaps in this heap + MEMMemoryList list; + + //! Pointer to start of allocatable memory + void *dataStart; + + //! Pointer to end of allocatable memory + void *dataEnd; + + //! Lock used when MEM_HEAP_FLAG_USE_LOCK is set. + OSSpinLock lock; + + //! Flags set during heap creation. + uint32_t flags; + + UNKNOWN(0x0C); +}; +CHECK_OFFSET(MEMHeapHeader, 0x00, tag); +CHECK_OFFSET(MEMHeapHeader, 0x04, link); +CHECK_OFFSET(MEMHeapHeader, 0x0C, list); +CHECK_OFFSET(MEMHeapHeader, 0x18, dataStart); +CHECK_OFFSET(MEMHeapHeader, 0x1C, dataEnd); +CHECK_OFFSET(MEMHeapHeader, 0x20, lock); +CHECK_OFFSET(MEMHeapHeader, 0x30, flags); +CHECK_SIZE(MEMHeapHeader, 0x40); + +/** + * Print details about heap to COSWarn + */ +void +MEMDumpHeap(MEMHeapHeader *heap); + +/** + * Find heap which contains a memory block. + */ +MEMHeapHeader * +MEMFindContainHeap(void *block); + +/** + * Get the data fill value used when MEM_HEAP_FLAG_DEBUG_MODE is set. + */ +uint32_t +MEMGetFillValForHeap(MEMHeapFillType type); + +/** + * Set the data fill value used when MEM_HEAP_FLAG_DEBUG_MODE is set. + */ +void +MEMSetFillValForHeap(MEMHeapFillType type, + uint32_t value); + +#ifdef __cplusplus +} +#endif + +/** @} */ diff --git a/rpl/libcoreinit/exports.h b/rpl/libcoreinit/exports.h index 5b1665f..bbe9cf9 100644 --- a/rpl/libcoreinit/exports.h +++ b/rpl/libcoreinit/exports.h @@ -163,6 +163,12 @@ EXPORT(MEMAdjustFrmHeap); EXPORT(MEMResizeForMBlockFrmHeap); EXPORT(MEMGetAllocatableSizeForFrmHeapEx); +// coreinit/memheap.h +EXPORT(MEMDumpHeap); +EXPORT(MEMFindContainHeap); +EXPORT(MEMGetFillValForHeap); +EXPORT(MEMSetFillValForHeap); + // coreinit/memlist.h EXPORT(MEMInitList); EXPORT(MEMAppendListObject);