/*---------------------------------------------------------------------------* Project: Horizon File: fnd_DetailHeapCommon.cpp 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: 18703 $ *---------------------------------------------------------------------------*/ #include #include #include "./fnd_DetailHeap.h" #include "./fnd_DetailHeapCommon.h" #include "./fnd_DetailHeapCommonImpl.h" namespace nn { namespace fnd { namespace detail { /* ======================================================================== static変数 ======================================================================== */ /* ------------------------------------------------------------------------ リスト関連 ------------------------------------------------------------------------ */ static NNSFndList sRootList; // ルートのヒープリスト static bool sRootListInitialized = false; // sRootListが初期化されていれば真 /* ------------------------------------------------------------------------ フィル関連 ------------------------------------------------------------------------ */ #ifdef NN_BUILD_VERBOSE static u32 sFillVals[NN_OS_HEAP_FILL_MAX] = { 0xC3C3C3C3, // ヒープ作成時に埋める値 0xF3F3F3F3, // メモリブロック確保時に埋める値 0xD3D3D3D3, // メモリブロック解放時に埋める値 }; // #ifdef NN_BUILD_VERBOSE #endif /* ======================================================================== static関数 ======================================================================== */ /* ------------------------------------------------------------------------ リスト関連 ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: FindContainHeap Description: 指定されたメモリブロックを含有するヒープをリストから再帰的に 探し出します。 Arguments: pList: リストへのポインタ memBlock: メモリブロックへのポインタ Returns: 指定されたメモリブロックを確保したヒープが見つかれば、 そのヒープへのポインタを返します。 見つからなかった時は NULL を返します。 *---------------------------------------------------------------------------*/ static NNSiFndHeapHead* FindContainHeap( NNSFndList* pList, const void* memBlock ) { NNSiFndHeapHead* pHeapHd = NULL; while (NULL != (pHeapHd = reinterpret_cast( GetNextListObject(pList, pHeapHd)))) { if ( NNSiGetUIntPtr(pHeapHd->heapStart) <= NNSiGetUIntPtr(memBlock) && NNSiGetUIntPtr(memBlock) < NNSiGetUIntPtr(pHeapHd->heapEnd) ) { NNSiFndHeapHead* pChildHeapHd = FindContainHeap(&pHeapHd->childList, memBlock); if(pChildHeapHd) { return pChildHeapHd; } return pHeapHd; } } return NULL; } /*---------------------------------------------------------------------------* Name: FindListContainHeap Description: ヒープを含有する親ヒープを検索し、その親ヒープのリストへの ポインタを返します。 Arguments: pHeapHd: 検索対象のヒープのヘッダへのポインタ。 Returns: 指定したヒープを含有する親ヒープが見つかれば、 親ヒープの子リストへのポインタを返します。 親ヒープが見つからなければルートリストへのポインタが返ります。 *---------------------------------------------------------------------------*/ static NNSFndList* FindListContainHeap(NNSiFndHeapHead* pHeapHd) { NNSFndList* pList = &sRootList; NNSiFndHeapHead* pContainHeap = FindContainHeap(&sRootList, pHeapHd); if(pContainHeap) { pList = &pContainHeap->childList; } return pList; } #if 1 static inline void // static void DumpHeapList() {} #else static void DumpHeapList() { NNSiFndHeapHead* pHeapHd = NULL; int count = 0; NN_TLOG_("Dump Heap List\n"); while (NULL != (pHeapHd = GetNextListObject(&sRootList, pHeapHd))) { count++; NN_TLOG_("[%d] -> %p %08X\n", count, pHeapHd, pHeapHd->signature); } } #endif /* ======================================================================== 外部関数(非公開) ======================================================================== */ /*---------------------------------------------------------------------------* Name: NNSi_FndInitHeapHead Description: ヒープヘッダの初期化を行います。 Arguments: pHeapHd: ヒープヘッダへのポインタ。 signature: シグネチャ。 heapStart: ヒープメモリの開始アドレス。 heapEnd: ヒープメモリの終了アドレス +1。 optFlag: ヒープオプション。 Returns: なし。 *---------------------------------------------------------------------------*/ void NNSi_FndInitHeapHead( NNSiFndHeapHead* pHeapHd, u32 signature, void* heapStart, void* heapEnd, u16 optFlag ) { pHeapHd->signature = signature; pHeapHd->heapStart = heapStart; pHeapHd->heapEnd = heapEnd; pHeapHd->attribute = 0; SetOptForHeap(pHeapHd, optFlag); FillNoUseMemory( pHeapHd, heapStart, GetOffsetFromPtr(heapStart, heapEnd)); NN_OS_INIT_LIST(&pHeapHd->childList, NNSiFndHeapHead, link); // ヒープのリスト操作 if(! sRootListInitialized) { NN_OS_INIT_LIST(&sRootList, NNSiFndHeapHead, link); sRootListInitialized = true; } AppendListObject(FindListContainHeap(pHeapHd), pHeapHd); DumpHeapList(); } /*---------------------------------------------------------------------------* Name: NNSi_FndFinalizeHeap Description: ヒープ共通の後始末を行います。 Arguments: pHeapHd: ヒープヘッダへのポインタ。 Returns: なし。 *---------------------------------------------------------------------------*/ void NNSi_FndFinalizeHeap(NNSiFndHeapHead* pHeapHd) { // ヒープのリスト操作 RemoveListObject(FindListContainHeap(pHeapHd), pHeapHd); DumpHeapList(); } /*---------------------------------------------------------------------------* Name: NNSi_FndDumpHeapHead Description: ヒープヘッダの情報を表示します。 Arguments: pHeapHd: ヒープヘッダへのポインタ。 Returns: なし。 *---------------------------------------------------------------------------*/ void NNSi_FndDumpHeapHead(NNSiFndHeapHead const* pHeapHd) { NN_TLOG_("[NNS Foundation "); switch(pHeapHd->signature) { case NNSI_EXPHEAP_SIGNATURE: NN_TLOG_("Exp"); break; case NNSI_FRMHEAP_SIGNATURE: NN_TLOG_("Frame"); break; case NNSI_UNTHEAP_SIGNATURE: NN_TLOG_("Unit"); break; default: NN_TASSERT_(false); } NN_TLOG_(" Heap]\n"); NN_TLOG_(" whole [%p - %p)\n", pHeapHd, pHeapHd->heapEnd); } /* ======================================================================== 外部関数(公開) ======================================================================== */ /*---------------------------------------------------------------------------* Name: FindContainHeap Description: メモリブロックを含有するヒープを検索します。 Arguments: memBlock: 検索対象のメモリブロック。 Returns: 指定したメモリブロックを含むヒープが見つかれば、 そのヒープのハンドルを返します。 見つからなければ、NN_OS_HEAP_INVALID_HANDLE が返ります。 *---------------------------------------------------------------------------*/ Heap FindContainHeap(const void* memBlock) { return FindContainHeap(&sRootList, memBlock); } /*---------------------------------------------------------------------------* Name: DumpHeap Description: ヒープ内部の情報を表示します。 これはデバッグ用の関数です。 Arguments: heap: フレームヒープのハンドル。 Returns: なし。 *---------------------------------------------------------------------------*/ #if ! defined(NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK) void DumpHeap(Heap heap) { NNSiFndHeapHead* pHeapHd = heap; switch(pHeapHd->signature) { case NNSI_EXPHEAP_SIGNATURE: NNSi_FndDumpHeap(heap); break; default: NN_TLOG_("[NNS Foundation] dump heap : unknown heap. - %p\n", heap); } } // #if ! defined(NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK) #endif /*---------------------------------------------------------------------------* Name: SetFillValForHeap Description: ヒープの作成時やメモリブロックの確保・解放時にメモリに セットする値をセットします。 この関数はデバッグ用の関数です。 最終ROM版(FINALROM)ライブラリでは常に0を返します。 Arguments: type: 取得する値の種類 val: セットする値 Returns: 以前の、メモリブロックの確保時にメモリにセットする値を返します。 *---------------------------------------------------------------------------*/ #ifdef NN_BUILD_VERBOSE u32 SetFillValForHeap( int type, u32 val ) { NN_TASSERT_(type < NN_OS_HEAP_FILL_MAX); { u32 oldVal = sFillVals[type]; sFillVals[type] = val; return oldVal; } } // #ifdef NN_BUILD_VERBOSE #endif /*---------------------------------------------------------------------------* Name: GetFillValForHeap Description: ヒープの作成時やメモリブロックの確保・解放時にメモリに セットする値を取得します。 この関数はデバッグ用の関数です。 最終ROM版(FINALROM)ライブラリでは常に0を返します。 Arguments: type: 取得する値の種類 Returns: 指定された種類のメモリにセットする値を返します。 *---------------------------------------------------------------------------*/ #ifdef NN_BUILD_VERBOSE u32 GetFillValForHeap(int type) { NN_TASSERT_(type < NN_OS_HEAP_FILL_MAX); return sFillVals[type]; } // #ifdef NN_BUILD_VERBOSE #endif }}} // namespace nn::os::fnd