1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: fnd_UnitHeap.cpp
4
5 Copyright (C)2009 Nintendo Co., Ltd. All rights reserved.
6
7 These coded instructions, statements, and computer programs contain
8 proprietary information of Nintendo of America Inc. and/or Nintendo
9 Company Ltd., and are protected by Federal copyright law. They may
10 not be disclosed to third parties or copied or duplicated in any form,
11 in whole or in part, without the prior written consent of Nintendo.
12
13 $Rev: 12372 $
14 *---------------------------------------------------------------------------*/
15
16 #include <nn/fnd/fnd_UnitHeap.h>
17 #include <nn/Assert.h>
18 #include <new>
19
20 namespace nn { namespace fnd {
21
Initialize(size_t unit,uptr addr,size_t size,s32 alignment,bit32 option)22 void UnitHeapBase::Initialize(size_t unit, uptr addr, size_t size, s32 alignment, bit32 option)
23 {
24 NN_TASSERT_(m_FreeNode == 0);
25 NN_TASSERT_(alignment >= sizeof(void*));
26 NN_TASSERT_(alignment % sizeof(void*) == 0);
27 HeapBase::Initialize(option);
28 this->m_Unit = RoundUp(unit, alignment);
29 this->m_Addr = RoundUp(addr, alignment);
30 this->m_Size = RoundDown(size, unit);
31 this->m_Alignment = alignment;
32 this->m_Count = 0;
33
34 DebugFillMemory(addr, size, HEAP_FILL_TYPE_NOUSE);
35
36 Node* freeNode = 0;
37 for (uptr addr2 = m_Addr + m_Size - m_Unit; addr2 >= m_Addr; addr2 -= m_Unit)
38 {
39 reinterpret_cast<Node*>(addr2)->next = freeNode;
40 freeNode = reinterpret_cast<Node*>(addr2);
41 }
42 NN_TASSERT_(reinterpret_cast<uptr>(freeNode) == m_Addr);
43 this->m_FreeNode = freeNode;
44 }
45
GetRequiredHeapSize(size_t unit,size_t numUnit,s32 alignment)46 size_t UnitHeapBase::GetRequiredHeapSize(size_t unit, size_t numUnit, s32 alignment)
47 {
48 return RoundUp(unit, alignment) * numUnit;
49 }
50
Dump() const51 void UnitHeapBase::Dump() const
52 {
53 #if ! defined(NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK)
54
55 NN_TLOG_(" address(from - to): size\n"); // ヘッダー行
56
57 // ---------------- UsedBlock のダンプ ----------------
58 NN_TLOG_(" (Used Nodes)\n" );
59 if(this->m_Count == 0)
60 {
61 NN_TLOG_(" NONE\n");
62 }
63 else
64 {
65 uptr start = this->m_Addr;
66 for( int i = 0; i < this->m_Count; i++ )
67 {
68 NN_TLOG_(" %08x - %08x: %8d\n",
69 start, start + this->m_Unit, this->m_Unit);
70 start += this->m_Unit;
71 }
72 }
73
74 // ---------------- FreeBlock のダンプ ----------------
75 NN_TLOG_(" (Free Nodes)\n" );
76 if(this->m_FreeNode == NULL)
77 {
78 NN_TLOG_(" NONE\n");
79 }
80 else
81 {
82 Node* pNode = this->m_FreeNode;
83 for( int i = 0; i < this->m_Count; i++ )
84 {
85 NN_TLOG_(" %08x - %08x: %8d\n",
86 pNode, pNode->next, pNode->next - pNode);
87 pNode = pNode->next;
88 }
89 }
90
91 u32 usedSize = this->m_Unit * this->m_Count;
92 NN_TLOG_("\n");
93 NN_TLOG_(" %d / %d bytes (%d%%) used\n",
94 usedSize, this->m_Size, 100 * usedSize / this->m_Size);
95 NN_TLOG_("\n");
96
97 #endif
98 }
99
100 }}
101