1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fnd_UnitHeap.cpp
4 
5   Copyright (C)2009-2012 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: 46347 $
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_(unit >= sizeof(void*));
27     NN_TASSERT_(alignment % sizeof(void*) == 0);
28     HeapBase::Initialize(option);
29     this->m_Unit = RoundUp(unit, alignment);
30     this->m_Addr = RoundUp(addr, alignment);
31     this->m_Size = RoundDown((addr+size)-m_Addr, m_Unit);
32     this->m_Alignment = alignment;
33     this->m_Count = 0;
34 
35     DebugFillMemory(addr, size, HEAP_FILL_TYPE_NOUSE);
36 
37     Node* freeNode = 0;
38     for (uptr addr2 = m_Addr + m_Size - m_Unit; addr2 >= m_Addr; addr2 -= m_Unit)
39     {
40         reinterpret_cast<Node*>(addr2)->next = freeNode;
41         freeNode = reinterpret_cast<Node*>(addr2);
42     }
43     NN_TASSERT_(reinterpret_cast<uptr>(freeNode) == m_Addr);
44     this->m_FreeNode = freeNode;
45 }
46 
GetRequiredHeapSize(size_t unit,size_t numUnit,s32 alignment)47 size_t UnitHeapBase::GetRequiredHeapSize(size_t unit, size_t numUnit, s32 alignment)
48 {
49     return RoundUp(unit, alignment) * numUnit;
50 }
51 
IsFreeNode(uptr addr) const52 bool UnitHeapBase::IsFreeNode(uptr addr) const
53 {
54     if(this->m_FreeNode == NULL)
55     {
56         return false;
57     }
58     else
59     {
60         Node* pNode = this->m_FreeNode;
61         while( pNode != NULL )
62         {
63             if(reinterpret_cast<uptr>(pNode) == addr)
64             {
65                 return true;
66             }
67             pNode = pNode->next;
68         }
69     }
70 
71     return false;
72 }
73 
Dump() const74 void UnitHeapBase::Dump() const
75 {
76 #if ! defined(NN_SWITCH_DISABLE_DEBUG_PRINT_FOR_SDK)
77 
78     NN_TLOG_("     address(from - to):     size\n");   // header line
79 
80     // ---------------- UsedBlock dump ----------------
81     NN_TLOG_("    (Used Nodes)\n" );
82     if(this->m_Count == 0)
83     {
84         NN_TLOG_("    NONE\n");
85     }
86     else
87     {
88         for( int i = 0; i < m_Size / m_Unit; i++ )
89         {
90             uptr start = this->m_Addr + m_Unit * i;
91             if(! IsFreeNode(start))
92             {
93                 NN_TLOG_("    %08x - %08x: %8d\n",
94                     start, start + this->m_Unit, this->m_Unit);
95             }
96         }
97     }
98 
99     // ---------------- FreeBlock dump ----------------
100     NN_TLOG_("    (Free Nodes)\n" );
101     if(this->m_FreeNode == NULL)
102     {
103         NN_TLOG_("    NONE\n");
104     }
105     else
106     {
107         Node* pNode = this->m_FreeNode;
108         while(pNode != NULL)
109         {
110             uptr start = reinterpret_cast<uptr>(pNode);
111             NN_TLOG_("    %08x - %08x: %8d\n",
112                 start, start + this->m_Unit, this->m_Unit);
113             pNode = pNode->next;
114         }
115     }
116 
117     u32 usedSize = this->m_Unit * this->m_Count;
118     NN_TLOG_("\n");
119     NN_TLOG_("    %d / %d bytes (%d%%) used\n",
120         usedSize, this->m_Size, 100 * usedSize / this->m_Size);
121     NN_TLOG_("\n");
122 
123 #endif
124 }
125 
126 }}
127