From f2e4e04805dc092de11e5c8d313e57e6b5ece297 Mon Sep 17 00:00:00 2001 From: James Benton Date: Wed, 10 Aug 2016 18:27:20 +0100 Subject: [PATCH] coreinit: Add block heap definitions. --- include/coreinit/blockheap.h | 132 +++++++++++++++++++++++++++++++++++ rpl/libcoreinit/exports.h | 11 +++ 2 files changed, 143 insertions(+) create mode 100644 include/coreinit/blockheap.h diff --git a/include/coreinit/blockheap.h b/include/coreinit/blockheap.h new file mode 100644 index 0000000..d4d5c33 --- /dev/null +++ b/include/coreinit/blockheap.h @@ -0,0 +1,132 @@ +#pragma once +#include +#include "memheap.h" + +/** + * \defgroup coreinit_blockheap Block Heap + * \ingroup coreinit + * @{ + */ + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct MEMBlockHeapBlock MEMBlockHeapBlock; +typedef struct MEMBlockHeapTracking MEMBlockHeapTracking; +typedef struct MEMBlockHeap MEMBlockHeap; + +struct MEMBlockHeapTracking +{ + UNKNOWN(0x8); + + //! Pointer to first memory block + MEMBlockHeapBlock *blocks; + + //! Number of blocks in this tracking heap + uint32_t blockCount; +}; +CHECK_OFFSET(MEMBlockHeapTracking, 0x08, blocks); +CHECK_OFFSET(MEMBlockHeapTracking, 0x0C, blockCount); +CHECK_SIZE(MEMBlockHeapTracking, 0x10); + +struct MEMBlockHeapBlock +{ + //! First address of the data region this block has allocated + void *start; + + //! End address of the data region this block has allocated + void *end; + + //! TRUE if the block is free, FALSE if allocated + BOOL isFree; + + //! Link to previous block, note that this is only set for allocated blocks + MEMBlockHeapBlock *prev; + + //! Link to next block, always set + MEMBlockHeapBlock *next; +}; +CHECK_OFFSET(MEMBlockHeapBlock, 0x00, start); +CHECK_OFFSET(MEMBlockHeapBlock, 0x04, end); +CHECK_OFFSET(MEMBlockHeapBlock, 0x08, isFree); +CHECK_OFFSET(MEMBlockHeapBlock, 0x0c, prev); +CHECK_OFFSET(MEMBlockHeapBlock, 0x10, next); +CHECK_SIZE(MEMBlockHeapBlock, 0x14); + +struct MEMBlockHeap +{ + MEMHeapHeader header; + + //! Default tracking heap, tracks only defaultBlock + MEMBlockHeapTracking defaultTrack; + + //! Default block, used so we don't have an empty block list + MEMBlockHeapBlock defaultBlock; + + //! First block in this heap + MEMBlockHeapBlock *firstBlock; + + //! Last block in this heap + MEMBlockHeapBlock *lastBlock; + + //! First free block + MEMBlockHeapBlock *firstFreeBlock; + + //! Free block count + uint32_t numFreeBlocks; +}; +CHECK_OFFSET(MEMBlockHeap, 0x00, header); +CHECK_OFFSET(MEMBlockHeap, 0x40, defaultTrack); +CHECK_OFFSET(MEMBlockHeap, 0x50, defaultBlock); +CHECK_OFFSET(MEMBlockHeap, 0x64, firstBlock); +CHECK_OFFSET(MEMBlockHeap, 0x68, lastBlock); +CHECK_OFFSET(MEMBlockHeap, 0x6C, firstFreeBlock); +CHECK_OFFSET(MEMBlockHeap, 0x70, numFreeBlocks); +CHECK_SIZE(MEMBlockHeap, 0x74); + +MEMBlockHeap * +MEMInitBlockHeap(MEMBlockHeap *heap, + void *start, + void *end, + MEMBlockHeapTracking *blocks, + uint32_t size, + uint32_t flags); + +void * +MEMDestroyBlockHeap(MEMBlockHeap *heap); + +int +MEMAddBlockHeapTracking(MEMBlockHeap *heap, + MEMBlockHeapTracking *tracking, + uint32_t size); + +void * +MEMAllocFromBlockHeapAt(MEMBlockHeap *heap, + void *addr, + uint32_t size); + +void * +MEMAllocFromBlockHeapEx(MEMBlockHeap *heap, + uint32_t size, + int32_t align); + +void +MEMFreeToBlockHeap(MEMBlockHeap *heap, + void *data); + +uint32_t +MEMGetAllocatableSizeForBlockHeapEx(MEMBlockHeap *heap, + int32_t align); + +uint32_t +MEMGetTrackingLeftInBlockHeap(MEMBlockHeap *heap); + +uint32_t +MEMGetTotalFreeSizeForBlockHeap(MEMBlockHeap *heap); + +#ifdef __cplusplus +} +#endif + +/** @} */ diff --git a/rpl/libcoreinit/exports.h b/rpl/libcoreinit/exports.h index 898d9b9..3f546ac 100644 --- a/rpl/libcoreinit/exports.h +++ b/rpl/libcoreinit/exports.h @@ -30,6 +30,17 @@ EXPORT(MEMGetArena); EXPORT(MEMGetBaseHeapHandle); EXPORT(MEMSetBaseHeapHandle); +// coreinit/blockheap.h +EXPORT(MEMInitBlockHeap); +EXPORT(MEMDestroyBlockHeap); +EXPORT(MEMAddBlockHeapTracking); +EXPORT(MEMAllocFromBlockHeapAt); +EXPORT(MEMAllocFromBlockHeapEx); +EXPORT(MEMFreeToBlockHeap); +EXPORT(MEMGetAllocatableSizeForBlockHeapEx); +EXPORT(MEMGetTrackingLeftInBlockHeap); +EXPORT(MEMGetTotalFreeSizeForBlockHeap); + // coreinit/cache.h EXPORT(DCInvalidateRange); EXPORT(DCFlushRange);