1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fnd_HeapBase.h
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: 24328 $
14  *---------------------------------------------------------------------------*/
15 
16 /* Please see man pages for details
17 
18 
19 */
20 
21 #ifndef NN_FND_FND_HEAPBASE_H_
22 #define NN_FND_FND_HEAPBASE_H_
23 
24 #include <nn/types.h>
25 #include <nn/util/util_NonCopyable.h>
26 #include <nn/fnd/fnd_LinkedList.h>
27 
28 // Default alignment
29 #define NN_FND_HEAP_DEFAULT_ALIGNMENT 4
30 
31 // Zero out memory when memory is allocated.
32 #define NN_FND_HEAP_OPTION_ZERO_CLEAR (1 << 0)
33 
34 // Memory filled when heap is created, memory is allocated, or memory is freed.
35 #define NN_FND_HEAP_OPTION_DEBUG_FILL (1 << 1)
36 
37 //  If this bit stands, output error
38 #define NN_FND_HEAP_OPTION_ERROR_PRINT (1 << 0)
39 
40 #ifdef __cplusplus
41 
42 namespace nn { namespace fnd {
43 
44 /* Please see man pages for details
45 
46 */
47 enum HeapFillType
48 {
49     HEAP_FILL_TYPE_NOUSE,    //
50     HEAP_FILL_TYPE_ALLOC,    //
51     HEAP_FILL_TYPE_FREE,     //
52 
53     HEAP_FILL_TYPE_MAX
54 };
55 
56 enum HeapInfoPlacement
57 {
58     HEAP_INFOPLACEMENT_HEAD,
59     HEAP_INFOPLACEMENT_TAIL
60 };
61 
62 /* Please see man pages for details
63 
64 */
65 enum HeapAdjustMode
66 {
67     HEAP_ADJUST_TAIL = 1,    //
68     HEAP_ADJUST_HEAD = -1    //
69 };
70 
71 /* Please see man pages for details
72 
73 
74 
75  */
76 class HeapBase : public IntrusiveLinkedList<HeapBase>::Item
77 {
78 public:
79 
80     static const s32 DEFAULT_ALIGNMENT = 4;
81     static const bit32 OPTION_ERROR_PRINT = NN_FND_HEAP_OPTION_ERROR_PRINT;
82 
83 	/* Please see man pages for details
84 
85 
86 
87 
88 	*/
89     static void SetFillValue(HeapFillType type, bit32 val);
90 
91 	/* Please see man pages for details
92 
93 
94 
95 	*/
96     static bit32 GetFillValue(HeapFillType type);
97 
98     /* Please see man pages for details
99 
100     */
101     virtual ~HeapBase() = 0;
102 
103     /* Please see man pages for details
104 
105     */
106     virtual void FreeV(void*) = 0;
107 
108     /* Please see man pages for details
109 
110     */
111     virtual void* GetStartAddress() const = 0;
112 
113     /* Please see man pages for details
114 
115     */
116     virtual size_t GetTotalSize() const = 0;
117 
118     /* Please see man pages for details
119 
120     */
121     virtual void Dump() const = 0;
122 
123     /* Please see man pages for details
124 
125 
126 
127 
128 
129     */
130     virtual bool HasAddress(const void* addr) const = 0;
131 
132     /* Please see man pages for details
133 
134 
135 
136     */
GetParent()137     HeapBase* GetParent() { return m_Parent; }
138 
139     /* Please see man pages for details
140 
141 
142 
143     */
144     HeapBase* GetRoot();
145 
146     /* Please see man pages for details
147 
148 
149 
150 
151 
152 
153 
154 
155 
156     */
157     HeapBase* FindHeap(void* addr);
158 
159     /* Please see man pages for details
160 
161 
162 
163 
164 
165 
166 
167     */
168     void Destroy(HeapBase* child);
169 
170 protected:
171 
172     HeapBase(bit32 option = 0) : m_Parent(0), m_Option(0) { Initialize(option); }
173 
Initialize(bit32 option)174     void Initialize(bit32 option) { m_Option = option; }
175 
176     /* Please see man pages for details
177 
178 
179 
180 
181 
182     */
183     void SetParent(HeapBase* parent);
184 
RoundDown(uptr addr,s32 alignment)185     static uptr RoundDown(uptr addr, s32 alignment)
186     {
187         return (addr / alignment) * alignment;
188     }
189 
RoundUp(uptr addr,s32 alignment)190     static uptr RoundUp(uptr addr, s32 alignment)
191     {
192         return RoundDown(addr + alignment - 1, alignment);
193     }
194 
FillMemoryZero(uptr addr,size_t size)195     void FillMemoryZero(uptr addr, size_t size)
196     {
197         if(m_Option & NN_FND_HEAP_OPTION_ZERO_CLEAR)
198         {
199             FillMemory32(addr, addr + size, 0);
200         }
201     }
202 
203 #ifdef NN_BUILD_VERBOSE
DebugFillMemory(uptr addr,size_t size,HeapFillType type)204     void DebugFillMemory(uptr addr, size_t size, HeapFillType type)
205     {
206         // TODO: Replace if there is a faster function
207         if(this->m_Option & NN_FND_HEAP_OPTION_DEBUG_FILL)
208         {
209             FillMemory32(addr, addr + size, GetFillValue(type));
210         }
211     }
212 #else
DebugFillMemory(uptr,size_t,HeapFillType)213     inline void DebugFillMemory(uptr, size_t, HeapFillType) {}
214 #endif
215 
216 private:
217     HeapBase* m_Parent;
218 
219     IntrusiveLinkedList<HeapBase> m_Children;
220 
221     bit32 m_Option;
222 
223     static void FillMemory(uptr addr, uptr end, bit8 value);
224     static void FillMemory32(uptr addr, uptr end, bit32 value);
225 };
226 
227 }}
228 
229 #endif
230 
231 #endif
232