/*---------------------------------------------------------------------------* Copyright (C) 2010-2011 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Project: MEM library File: heapCommon.h Programmers: Takano Makoto Copyright (C) 2005 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ #ifndef MEM_HEAPCOMMON_H__ #define MEM_HEAPCOMMON_H__ #ifdef __cplusplus extern "C" { #endif #include #include #include #include /* ======================================================================== Macro Constants ======================================================================== */ // Invalid heap handle. #define MEM_HEAP_INVALID_HANDLE NULL // Default alignment size when memory is allocated from the heap. #define MEM_HEAP_DEFAULT_ALIGNMENT 4 /* ------------------------------------------------------------------------ Fill Constants ------------------------------------------------------------------------ */ // Zero out temporarily allocated memory. #define MEM_HEAP_OPT_0_CLEAR (1 << 0) // Temporary memory fill when a heap is created or new E memory is allocated or freed. #define MEM_HEAP_OPT_DEBUG_FILL (1 << 1) // Presence or absence of a locking mechanism. #define MEM_HEAP_OPT_THREAD_SAFE (1 << 2) /* ------------------------------------------------------------------------ Heap Check Constants ------------------------------------------------------------------------ */ // If this bit stands, output the error. #define MEM_HEAP_ERROR_PRINT (1 << 0) // Signature of the expanded heap. #define MEMi_EXPHEAP_SIGNATURE 0x45585048 // ('EXPH') // Signature of the frame heap. #define MEMi_FRMHEAP_SIGNATURE 0x46524D48 // ('FRMH') // Signature of the unit heap. #define MEMi_UNTHEAP_SIGNATURE 0x554E5448 // ('UNTH') // User-defined signature. #define MEMi_USRHEAP_SIGNATURE 0x55535248 // ('USRH') // Block heap. #define MEMi_BLKHEAP_SIGNATURE 0x424C4B48 // ('BLKH') /* ======================================================================== enum Constant ======================================================================== */ typedef enum { MEM_HEAP_TYPE_EXP, // Expanded heap. MEM_HEAP_TYPE_FRM, // Frame heap. MEM_HEAP_TYPE_UNIT, // Unit heap. MEM_HEAP_TYPE_USER, // User-defined heap. MEM_HEAP_TYPE_BLK, // Block heap. MEM_HEAP_TYPE_UNKNOWN // Unknown heap handle. } MEMHeapType; enum { MEM_HEAP_FILL_NOUSE, // When debug fill is not used. MEM_HEAP_FILL_ALLOC, // When debug fill is allocated. MEM_HEAP_FILL_FREE, // When debug fill is freed. MEM_HEAP_FILL_MAX }; /* ======================================================================= Type Definitions ======================================================================== */ typedef OSSpinLock MEMSLock; typedef struct MEMiHeapHead MEMiHeapHead; // Common header among the heaps. struct MEMiHeapHead { u32 signature; MEMLink link; MEMList childList; void* heapStart; // Heap start address. void* heapEnd; // Heap end (+1) address. // OSMutex mutex; // For exclusive use between threads. MEMSLock slock; // Spin lock. union // Attribute. { u32 val; struct { u32 _reserved : 24; u32 optFlag : 8; // Option flag. } fields; } attribute; }; typedef MEMiHeapHead* MEMHeapHandle; // Type to represent the heap handle. /* ======================================================================= Function Prototypes ======================================================================== */ MEMHeapHandle MEMFindContainHeap( const void* memBlock ); MEMHeapHandle MEMFindParentHeap( MEMHeapHandle heap ); /* ======================================================================== Inline Functions ======================================================================== */ /*---------------------------------------------------------------------------* Name: MEMGetHeapStartAddress Description: Gets the start address of the memory area used by the heap. Arguments: heap: Heap handle. Returns: Returns the start address of the memory area used by the heap. *---------------------------------------------------------------------------*/ static inline void* MEMGetHeapStartAddress( MEMHeapHandle heap ) { if (heap->signature == MEMi_BLKHEAP_SIGNATURE) return heap->heapStart; return (void*)heap; } /*---------------------------------------------------------------------------* Name: MEMGetHeapStartAddressUsable Description: Gets the start address of the memory area assigned by the heap. Arguments: heap: Heap handle. Returns: Returns the start address of the memory area assigned by the heap. *---------------------------------------------------------------------------*/ static inline void* MEMGetHeapStartAddressUsable( MEMHeapHandle heap ) { return heap->heapStart; } /*---------------------------------------------------------------------------* Name: MEMGetHeapEndAddress Description: Gets the end address +1 of the memory area used by the heap. Arguments: heap: Heap handle. Returns: Return the end address +1 of the memory area used by the heap. *---------------------------------------------------------------------------*/ static inline void* MEMGetHeapEndAddress( MEMHeapHandle heap ) { return heap->heapEnd; } /*---------------------------------------------------------------------------* Name: MEMGetHeapTotalUsableSize Description: Gets the memory size allocated to the heap. The header is not included. Arguments: heap: Heap handle. Returns: Returns the memory size allocated to the heap. The header is not included. *---------------------------------------------------------------------------*/ static inline s32 MEMGetHeapTotalUsableSize( MEMHeapHandle heap ) { return ((s32)(heap->heapEnd) - (s32)(heap->heapStart)); } /*---------------------------------------------------------------------------* Name: MemGetHeapTotalSize Description: Gets the memory size allocated to the heap (including that of the header). Arguments: heap Heap handle. Returns: Returns the memory size allocated to the heap (total memory size including header). *---------------------------------------------------------------------------*/ static inline s32 MEMGetHeapTotalSize( MEMHeapHandle heap ) { if (heap->signature == MEMi_BLKHEAP_SIGNATURE) return ((s32)(heap->heapEnd) - (s32)(heap->heapStart)); return ((s32)(heap->heapEnd) - (s32)(heap)); } /*---------------------------------------------------------------------------* Name: MEMGetHeapType Description: Gets the type of heap specific to the heap handle. Arguments: heap: Heap handle. Returns: Gets the heap type from the heap handle. *---------------------------------------------------------------------------*/ static inline MEMHeapType MEMGetHeapType( MEMHeapHandle heap ) { switch ( heap->signature ) { case MEMi_EXPHEAP_SIGNATURE: return MEM_HEAP_TYPE_EXP; case MEMi_FRMHEAP_SIGNATURE: return MEM_HEAP_TYPE_FRM; case MEMi_UNTHEAP_SIGNATURE: return MEM_HEAP_TYPE_UNIT; case MEMi_USRHEAP_SIGNATURE: return MEM_HEAP_TYPE_USER; case MEMi_BLKHEAP_SIGNATURE: return MEM_HEAP_TYPE_BLK; default: return MEM_HEAP_TYPE_UNKNOWN; } } /*---------------------------------------------------------------------------* Name: MEMIsExpHeap Description: Determines whether the heap handle is for an expanded heap. Arguments: heap Heap handle. Returns: Returns true if it is for an expanded heap. Returns false otherwise. *---------------------------------------------------------------------------*/ static inline BOOL MEMIsExpHeap( MEMHeapHandle heap ) { return ( heap->signature == MEMi_EXPHEAP_SIGNATURE ); } /*---------------------------------------------------------------------------* Name: MEMIsFrmHeap Description: Determines whether the heap handle is for a frame heap. Arguments: heap Heap handle. Returns: Returns true if it is for a frame heap. Returns false otherwise. *---------------------------------------------------------------------------*/ static inline BOOL MEMIsFrmHeap( MEMHeapHandle heap ) { return ( heap->signature == MEMi_FRMHEAP_SIGNATURE ); } /*---------------------------------------------------------------------------* Name: MEMIsUnitHeap Description: Determines whether the heap handle is for a unit heap. Arguments: heap Heap handle. Returns: Returns true if it is for a unit heap. Returns false otherwise. *---------------------------------------------------------------------------*/ static inline BOOL MEMIsUnitHeap( MEMHeapHandle heap ) { return ( heap->signature == MEMi_UNTHEAP_SIGNATURE ); } /*---------------------------------------------------------------------------* Name: MEMIsBlockHeap Description: Determines whether a heap is a block heap. Arguments: heap Heap handle. Returns: Returns true if it is a block heap. *---------------------------------------------------------------------------*/ static inline BOOL MEMIsBlockHeap( MEMHeapHandle heap ) { return ( heap->signature == MEMi_BLKHEAP_SIGNATURE ); } /* ======================================================================= Debug Functions ======================================================================== */ void MEMDumpHeap( MEMHeapHandle heap ); BOOL MEMCheckHeap( MEMHeapHandle heap ); u32 MEMSetFillValForHeap( int type, u32 val ); u32 MEMGetFillValForHeap( int type ); #ifdef __cplusplus } /* extern "C" */ #endif /* MEM_HEAPCOMMON_H__ */ #endif