/*---------------------------------------------------------------------------* Project: MEM library File: heapCommoni.h Programmers: Makoto Takano Copyright 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_HEAPCOMMONI_H__ #define MEM_HEAPCOMMONI_H__ #include #include #ifdef __cplusplus extern "C" { #endif /* ======================================================================= Type Definitions ======================================================================== */ typedef s32 IntPtr; // Signed integer type mutually convertible with void* pointer typedef u32 UIntPtr; // Signed integer type mutually convertible with void* pointer /* ======================================================================== Macro functions ======================================================================== */ /*---------------------------------------------------------------------------* Name: RoundUp Description: Rounds up to align with specified boundary. Arguments: value: Targeted data alignment: Boundary value Returns: Returns a value that has been rounded up to the specified boundary value. *---------------------------------------------------------------------------*/ #define RoundUp( value, alignment ) \ (((value) + ((alignment)-1)) & ~((alignment)-1)) #define RoundUpPtr( ptr, alignment ) \ ( (void*)RoundUp(GetUIntPtr(ptr), (alignment)) ) /*---------------------------------------------------------------------------* Name: RoundDown Description: Rounds down to align with specified boundary. Arguments: value: Targeted data alignment: Boundary value Returns: Returns a value that has been rounded down to the specified boundary. *---------------------------------------------------------------------------*/ #define RoundDown( value, alignment ) \ ( (value) & ~((alignment)-1) ) #define RoundDownPtr(ptr, alignment) \ ( (void*)RoundDown(GetUIntPtr(ptr), (alignment)) ) /* ======================================================================= Function Prototype ======================================================================== */ void MEMiInitHeapHead( MEMiHeapHead* pHeapHd, u32 signature, void* heapStart, void* heapEnd, u16 optFlag ); void MEMiFinalizeHeap( MEMiHeapHead* pHeapHd ); void MEMiDumpHeapHead( MEMiHeapHead* pHeapHd ); /* ======================================================================== inline function ======================================================================== */ /* ------------------------------------------------------------------------ Pointer Operations ------------------------------------------------------------------------ */ // Convert from pointer to u32 static inline UIntPtr GetUIntPtr( const void* ptr ) { return (UIntPtr)(ptr); } // Gets the offset value of the two pointers. static inline u32 GetOffsetFromPtr( const void* start, const void* end ) { return GetUIntPtr(end) - GetUIntPtr(start); } // Adds u32 value to the pointer. static inline void* AddU32ToPtr( void* ptr, u32 val ) { return (void*)( GetUIntPtr(ptr) + val ); } // Adds u32 value to the pointer. static inline const void* AddU32ToCPtr( const void* ptr, u32 val ) { return (const void*)( GetUIntPtr(ptr) + val ); } // Subtracts u32 value from the pointer. static inline void* SubU32ToPtr( void* ptr, u32 val ) { return (void*)( GetUIntPtr(ptr) - val ); } // Subtracts u32 value from the const pointer. static inline const void* SubU32ToCPtr( const void* ptr, u32 val ) { return (const void*)( GetUIntPtr(ptr) - val ); } // Compares the two pointer addresses. static inline int ComparePtr( const void* a, const void* b ) { const u8* wa = (const u8*)a; const u8* wb = (const u8*)b; return wa - wb; } /*---------------------------------------------------------------------------* Name: GetOptForHeap Description: Gets the option flag from the heap. Arguments: pHeapHd Heap handle Returns: Value of the option flag. *---------------------------------------------------------------------------*/ static inline u16 GetOptForHeap( const MEMiHeapHead* pHeapHd ) { return (u16)pHeapHd->attribute.fields.optFlag; } /*---------------------------------------------------------------------------* Name: SetOptForHeap Description: Set the option flags for heap. Arguments: pHeapHd Heap handle Returns: None. *---------------------------------------------------------------------------*/ static inline void SetOptForHeap( MEMiHeapHead* pHeapHd, u16 optFlag ) { pHeapHd->attribute.fields.optFlag = (u8)optFlag; } /* ------------------------------------------------------------------------ Exclusive control of heap. ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: LockHeap Description: Lock function for performing exclusive control of heap. Arguments: pHeapHd Heap handle Returns: None *---------------------------------------------------------------------------*/ static inline void LockHeap( MEMiHeapHead* pHeapHd ) { if ( GetOptForHeap( pHeapHd ) & MEM_HEAP_OPT_THREAD_SAFE ) { OSLockMutex( &pHeapHd->mutex ); } } /*---------------------------------------------------------------------------* Name: LockHeap Description: Unlock function for performing exclusive control of heap. Arguments: pHeapHd Heap handle Returns: None *---------------------------------------------------------------------------*/ static inline void UnlockHeap( MEMiHeapHead* pHeapHd ) { if ( GetOptForHeap( pHeapHd ) & MEM_HEAP_OPT_THREAD_SAFE ) { OSUnlockMutex( &pHeapHd->mutex ); } } /* ------------------------------------------------------------------------ Fill memory ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: FillAllocMemory Description: Memory fill immediately after allocation Arguments: pHeapHd Heap handle address Address size Size Returns: None *---------------------------------------------------------------------------*/ static inline void FillAllocMemory( MEMiHeapHead* pHeapHd, void* address, u32 size ) { if ( GetOptForHeap(pHeapHd) & MEM_HEAP_OPT_0_CLEAR ) { (void)memset( address, 0, size ); } else { #if defined( _DEBUG ) if ( GetOptForHeap(pHeapHd) & MEM_HEAP_OPT_DEBUG_FILL ) { (void)memset( address, (int)MEMGetFillValForHeap(MEM_HEAP_FILL_ALLOC), size ); } #endif } } #if ! defined(_DEBUG) #define FillFreeMemory( pHeapHd, address, size ) ((void) 0) #define FillNoUseMemory( pHeapHd, address, size ) ((void) 0) #else /*---------------------------------------------------------------------------* Name: FillFreeMemory Description: Memory fill immediately after release Arguments: pHeapHd Heap handle address Address size Size Returns: None *---------------------------------------------------------------------------*/ static inline void FillFreeMemory( MEMiHeapHead* pHeapHd, void* address, u32 size ) { if ( GetOptForHeap(pHeapHd) & MEM_HEAP_OPT_DEBUG_FILL ) { (void)memset( address, (int)MEMGetFillValForHeap(MEM_HEAP_FILL_FREE), size ); } } /*---------------------------------------------------------------------------* Name: FillNoUseMemory Description: Unused memory memory fill Arguments: pHeapHd Heap handle address Address size Size Returns: None *---------------------------------------------------------------------------*/ static inline void FillNoUseMemory( MEMiHeapHead* pHeapHd, void* address, u32 size ) { if ( GetOptForHeap(pHeapHd) & MEM_HEAP_OPT_DEBUG_FILL ) { (void)memset( address, (int)MEMGetFillValForHeap(MEM_HEAP_FILL_NOUSE), size ); } } #endif #ifdef __cplusplus } /* extern "C"*/ #endif /* MEM_HEAPCOMMONI_H__*/ #endif