/*---------------------------------------------------------------------------* Project: Horizon File: fnd_DetailHeapCommonImpl.h Copyright (C)2009 Nintendo Co., Ltd. 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. $Rev: 12449 $ *---------------------------------------------------------------------------*/ #ifndef NN_FND_DETAIL_FND_DETAIL_HEAPCOMMONIMPL_H_ #define NN_FND_DETAIL_FND_DETAIL_HEAPCOMMONIMPL_H_ #include #ifdef __cplusplus namespace nn { namespace fnd { namespace detail { /// @cond /* ======================================================================= 型定義 ======================================================================== */ typedef s32 NNSiIntPtr; // void*ポインタと相互変換可能な符号付き整数型 typedef u32 NNSiUIntPtr; // void*ポインタと相互変換可能な符号無し整数型 /* ======================================================================== マクロ関数 ======================================================================== */ /*---------------------------------------------------------------------------* Name: NNSi_FndRoundUp Description: 指定した境界値に合わせるため、上位方向に切り上げます。 Arguments: value: 対象となるデータ alignment: 境界値 Returns: 指定した境界に切り上げられた値を返します。 *---------------------------------------------------------------------------*/ #define NNSi_FndRoundUp(value, alignment) \ (((value) + (alignment-1)) & ~(alignment-1)) #define NNSi_FndRoundUpPtr(ptr, alignment) \ ((void*)NNSi_FndRoundUp(NNSiGetUIntPtr(ptr), alignment)) /*---------------------------------------------------------------------------* Name: NNSi_FndRoundDown Description: 指定した境界値に合わせるため、下位方向に切り下げます。 Arguments: value: 対象となるデータ alignment: 境界値 Returns: 指定した境界に切り下げられた値を返します。 *---------------------------------------------------------------------------*/ #define NNSi_FndRoundDown(value, alignment) \ ((value) & ~(alignment-1)) #define NNSi_FndRoundDownPtr(ptr, alignment) \ ((void*)NNSi_FndRoundDown(NNSiGetUIntPtr(ptr), alignment)) /*---------------------------------------------------------------------------* Name: NNSi_FndGetBitValue Description: 特定のビット位置の値を取得します。 Arguments: data: 取得するビットデータを含むデータ。 st: 開始ビット(0から始まる)。 bits: ビット数(31以下とする)。 Returns: 特定のビット位置の値を返します。 *---------------------------------------------------------------------------*/ #define NNSi_FndGetBitValue(data, st, bits) \ (((data) >>(st)) & ((1 <<(bits)) -1)) /*---------------------------------------------------------------------------* Name: NNSi_FndSetBitValue Description: 特定のビット位置に値をセットします。 Arguments: data: セットするビットデータを格納する変数。 st: 開始ビット(0から始まる)。 bits: ビット数(31以下とする)。 val: セットするビットデータ。 Returns: なし。 *---------------------------------------------------------------------------*/ #define NNSi_FndSetBitValue(data, st, bits, val) \ do \ { \ u32 maskBits = (u32)((1 <<(bits)) -1); \ u32 newVal = (val) & maskBits; /* 安全のためマスク */ \ (void)(maskBits <<= st); \ (data) &= ~maskBits; /* セットする領域をクリア */ \ (data) |= newVal <<(st); \ } while(false); /* ======================================================================== inline関数 ======================================================================== */ /* ------------------------------------------------------------------------ ポインタ操作 ------------------------------------------------------------------------ */ inline NNSiUIntPtr NNSiGetUIntPtr(const void* ptr) { return (NNSiUIntPtr)ptr; } inline u32 GetOffsetFromPtr(const void* start, const void* end) { return NNSiGetUIntPtr(end) - NNSiGetUIntPtr(start); } inline void* AddU32ToPtr(void* ptr, u32 val) { return (void*)( NNSiGetUIntPtr(ptr) + val ); } inline const void* AddU32ToCPtr(const void* ptr, u32 val) { return (const void*)( NNSiGetUIntPtr(ptr) + val ); } inline void* SubU32ToPtr(void* ptr, u32 val) { return (void*)( NNSiGetUIntPtr(ptr) - val ); } inline const void* SubU32ToCPtr(const void* ptr, u32 val) { return (const void*)( NNSiGetUIntPtr(ptr) - val ); } inline int ComparePtr(const void* a, const void* b) { const u8* wa = reinterpret_cast(a); const u8* wb = reinterpret_cast(b); return wa - wb; } inline u16 GetOptForHeap(const NNSiFndHeapHead* pHeapHd) { return (u16)NNSi_FndGetBitValue(pHeapHd->attribute, 0, 8); } inline void SetOptForHeap( NNSiFndHeapHead* pHeapHd, u16 optFlag ) { NNSi_FndSetBitValue(pHeapHd->attribute, 0, 8, optFlag); } /* ------------------------------------------------------------------------ メモリ充填 ------------------------------------------------------------------------ */ // TORIAEZU: とりあえずなmemset inline void MI_CpuFill32(void* dest, u32 data, u32 size) { u32* pDest = reinterpret_cast(dest); NN_TASSERT_(((u32)pDest & 0xfffffffc) == (u32)pDest); // dest は4バイトアライメントされてるはず NN_TASSERT_((size & 0xfffffffc) == size); // size は4バイトアライメントされてるはず for(int i = 0; i < size / 4; ++i) { pDest[i] = data; } } inline void FillAllocMemory( NNSiFndHeapHead* pHeapHd, void* address, u32 size ) { if (GetOptForHeap(pHeapHd) & NN_OS_HEAP_OPT_0_CLEAR) { MI_CpuFill32(address, 0, size); } else { #if ! defined(NN_SWITCH_DISABLE_DEBUG_PRINT) if (GetOptForHeap(pHeapHd) & NN_OS_HEAP_OPT_DEBUG_FILL) { MI_CpuFill32(address, GetFillValForHeap(NN_OS_HEAP_FILL_ALLOC), size); } #endif } } #if defined(NN_SWITCH_DISABLE_DEBUG_PRINT) #define FillFreeMemory(pHeapHd, address, size) ((void) 0) #else inline void FillFreeMemory( NNSiFndHeapHead* pHeapHd, void* address, u32 size ) { if (GetOptForHeap(pHeapHd) & NN_OS_HEAP_OPT_DEBUG_FILL) { MI_CpuFill32(address, GetFillValForHeap(NN_OS_HEAP_FILL_FREE), size); } } #endif #if defined(NN_SWITCH_DISABLE_DEBUG_PRINT) #define FillNoUseMemory(pHeapHd, address, size) ((void) 0) #else inline void FillNoUseMemory( NNSiFndHeapHead* pHeapHd, void* address, u32 size ) { if (GetOptForHeap(pHeapHd) & NN_OS_HEAP_OPT_DEBUG_FILL) { MI_CpuFill32(address, GetFillValForHeap(NN_OS_HEAP_FILL_NOUSE), size); } } #endif /* ======================================================================= 関数プロトタイプ ======================================================================== */ void NNSi_FndInitHeapHead( NNSiFndHeapHead* pHeapHd, u32 signature, void* heapStart, void* heapEnd, u16 optFlag); void NNSi_FndFinalizeHeap( NNSiFndHeapHead* pHeapHd); void NNSi_FndDumpHeapHead( NNSiFndHeapHead const* pHeapHd); /// @endcond }}} // namespace nn::os #endif // __cplusplus /* NN_OS_HEAPCOMMONI_H_ */ #endif /* NN_OS_OS_HEAPCOMMONIMPL_H_ */