1 /*---------------------------------------------------------------------------*
2   Project:  Horizon
3   File:     fnd_HeapBase.h
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: 47798 $
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 fill 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 
77  */
78 class HeapBase : public IntrusiveLinkedList<HeapBase>::Item
79 {
80 public:
81 
82     static const s32 DEFAULT_ALIGNMENT = 4;
83     static const bit32 OPTION_ERROR_PRINT = NN_FND_HEAP_OPTION_ERROR_PRINT;
84 
85 	/* Please see man pages for details
86 
87 
88 
89 
90 	*/
91     static void SetFillValue(HeapFillType type, bit32 val);
92 
93 	/* Please see man pages for details
94 
95 
96 
97 	*/
98     static bit32 GetFillValue(HeapFillType type);
99 
100     /* Please see man pages for details
101 
102     */
103     virtual ~HeapBase() = 0;
104 
105     /* Please see man pages for details
106 
107     */
108     virtual void FreeV(void*) = 0;
109 
110     /* Please see man pages for details
111 
112     */
113     virtual void* GetStartAddress() const = 0;
114 
115     /* Please see man pages for details
116 
117     */
118     virtual size_t GetTotalSize() const = 0;
119 
120     /* Please see man pages for details
121 
122     */
123     virtual void Dump() const = 0;
124 
125     /* Please see man pages for details
126 
127 
128 
129 
130 
131     */
132     virtual bool HasAddress(const void* addr) const = 0;
133 
134     /* Please see man pages for details
135 
136 
137 
138     */
GetParent()139     HeapBase* GetParent() { return m_Parent; }
140 
141     /* Please see man pages for details
142 
143 
144 
145     */
146     HeapBase* GetRoot();
147 
148     /* Please see man pages for details
149 
150 
151 
152 
153 
154 
155 
156 
157 
158     */
159     HeapBase* FindHeap(void* addr);
160 
161     /* Please see man pages for details
162 
163 
164 
165 
166 
167 
168 
169     */
170     void Destroy(HeapBase* child);
171 
172 protected:
173 
174     HeapBase(bit32 option = 0) : m_Parent(0), m_Option(0) { Initialize(option); }
175 
Initialize(bit32 option)176     void Initialize(bit32 option) { m_Option = option; }
177 
178     /* Please see man pages for details
179 
180 
181 
182 
183 
184     */
185     void SetParent(HeapBase* parent);
186 
RoundDown(uptr addr,s32 alignment)187     static uptr RoundDown(uptr addr, s32 alignment)
188     {
189         return (addr / alignment) * alignment;
190     }
191 
RoundUp(uptr addr,s32 alignment)192     static uptr RoundUp(uptr addr, s32 alignment)
193     {
194         return RoundDown(addr + alignment - 1, alignment);
195     }
196 
FillMemoryZero(uptr addr,size_t size)197     void FillMemoryZero(uptr addr, size_t size)
198     {
199         if(m_Option & NN_FND_HEAP_OPTION_ZERO_CLEAR)
200         {
201             FillMemory32(addr, addr + size, 0);
202         }
203     }
204 
205 #ifdef NN_BUILD_VERBOSE
DebugFillMemory(uptr addr,size_t size,HeapFillType type)206     void DebugFillMemory(uptr addr, size_t size, HeapFillType type)
207     {
208         // TODO: If there is a faster function, replace with it
209         if(this->m_Option & NN_FND_HEAP_OPTION_DEBUG_FILL)
210         {
211             FillMemory32(addr, addr + size, GetFillValue(type));
212         }
213     }
214 #else
DebugFillMemory(uptr,size_t,HeapFillType)215     inline void DebugFillMemory(uptr, size_t, HeapFillType) {}
216 #endif
217 
218 private:
219     HeapBase* m_Parent;
220 
221     IntrusiveLinkedList<HeapBase> m_Children;
222 
223     bit32 m_Option;
224 
225     static void FillMemory(uptr addr, uptr end, bit8 value);
226     static void FillMemory32(uptr addr, uptr end, bit32 value);
227 };
228 
229 }}
230 
231 #endif
232 
233 #endif
234