1 /*---------------------------------------------------------------------------*
2 Project: Horizon
3 File: fnd_HeapBase.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_HeapBase.h>
17 #include <nn/assert.h>
18 #include "./detail/fnd_DetailHeapCommon.h"
19
20 namespace nn { namespace fnd {
21
Destroy(HeapBase * child)22 void HeapBase::Destroy(HeapBase* child)
23 {
24 NN_TASSERT_(child);
25 NN_TASSERT_(child->m_Parent == this);
26 m_Children.Erase(child);
27 child->m_Parent = 0;
28 child->~HeapBase();
29 }
30
31
~HeapBase()32 HeapBase::~HeapBase()
33 {
34 if ( m_Parent )
35 {
36 NN_TPANIC_("yet not call HeapBase::Destroy");
37 }
38 if ( !m_Children.IsEmpty() )
39 {
40 NN_TPANIC_("Child Heaps left.");
41 }
42 }
43
GetRoot()44 HeapBase* HeapBase::GetRoot()
45 {
46 HeapBase* heap = this;
47 while (heap->m_Parent)
48 {
49 heap = heap->m_Parent;
50 }
51 return heap;
52 }
53
54
SetFillValue(HeapFillType type,u32 val)55 void HeapBase::SetFillValue(HeapFillType type, u32 val)
56 {
57 #ifdef NN_BUILD_VERBOSE
58 (void)detail::SetFillValForHeap(type, val);
59 #else
60 NN_UNUSED_VAR(type);
61 NN_UNUSED_VAR(val);
62 #endif
63 }
64
GetFillValue(HeapFillType type)65 u32 HeapBase::GetFillValue(HeapFillType type)
66 {
67 #ifdef NN_BUILD_VERBOSE
68 return detail::GetFillValForHeap(type);
69 #else
70 NN_UNUSED_VAR(type);
71 return 0;
72 #endif
73 }
74
FindHeap(void * addr)75 HeapBase* HeapBase::FindHeap(void* addr)
76 {
77 if (this->HasAddress(addr))
78 {
79 HeapBase* heap = this;
80 repeat:
81 for (HeapBase* child = heap->m_Children.GetFront(); child; child = heap->m_Children.GetNext(child))
82 {
83 if (child->HasAddress(addr))
84 {
85 heap = child;
86 goto repeat;
87 }
88 }
89 return heap;
90 }
91 else
92 {
93 return 0;
94 }
95 }
96
SetParent(HeapBase * parent)97 void HeapBase::SetParent(HeapBase* parent)
98 {
99 NN_TASSERT_(!this->m_Parent);
100 this->m_Parent = parent;
101 this->m_Parent->m_Children.PushBack(this);
102 }
103
104 namespace {
105
FillMemory8(uptr begin,uptr end,bit8 v)106 inline void FillMemory8(uptr begin, uptr end, bit8 v)
107 {
108 NN_TASSERT_(begin <= end);
109 bit8*& p = reinterpret_cast<bit8*&>(begin);
110 bit8*& q = reinterpret_cast<bit8*&>(end);
111 while (p != q)
112 {
113 *p++ = v;
114 }
115 }
116
117 }
118
FillMemory32(uptr begin,uptr end,bit32 v)119 void HeapBase::FillMemory32(uptr begin, uptr end, bit32 v)
120 {
121 NN_TASSERT_(begin <= end);
122 NN_TASSERT_(begin % sizeof(bit32) == 0);
123 NN_TASSERT_(end % sizeof(bit32) == 0);
124 bit32*& p = reinterpret_cast<bit32*&>(begin);
125 bit32*& q = reinterpret_cast<bit32*&>(end);
126 while (p != q)
127 {
128 *p++ = v;
129 }
130 }
131
FillMemory(uptr addr,uptr end,bit8 v)132 void HeapBase::FillMemory(uptr addr, uptr end, bit8 v)
133 {
134 // TODO: Replace if there is a faster function
135 uptr begin = addr;
136 uptr rbegin = RoundUp(begin, sizeof(bit32));
137 uptr rend = RoundDown(end, sizeof(bit32));
138 if (rbegin > rend)
139 {
140 FillMemory8(begin, end, v);
141 }
142 else
143 {
144 bit32 vv = v | (v << 8);
145 bit32 vvvv = vv | (vv << 16);
146 FillMemory8(begin, rbegin, v);
147 FillMemory32(rbegin, rend, vvvv);
148 FillMemory8(rend, end, v);
149 }
150 }
151
152 }}
153