1 /*---------------------------------------------------------------------------*
2   Project:     MEM library
3   File:        heapCommon.h
4   Programmers: Takano Makoto
5 
6   Copyright 2005 Nintendo. All rights reserved.
7 
8   These coded instructions, statements, and computer programs contain
9   proprietary information of Nintendo of America Inc. and/or Nintendo
10   Company Ltd., and are protected by Federal copyright law.  They may
11   not be disclosed to third parties or copied or duplicated in any form,
12   in whole or in part, without the prior written consent of Nintendo.
13  *---------------------------------------------------------------------------*/
14 #ifndef MEM_HEAPCOMMON_H__
15 #define MEM_HEAPCOMMON_H__
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 
22 #include <revolution/types.h>
23 #include <revolution/os.h>
24 #include <revolution/mem/list.h>
25 
26 
27 /* ========================================================================
28     Macro Constants
29    ======================================================================== */
30 
31 // Invalid heap handle
32 #define MEM_HEAP_INVALID_HANDLE      NULL
33 
34 // Default alignment size when memory is allocated from heap
35 #define MEM_HEAP_DEFAULT_ALIGNMENT   4
36 
37 
38 /* ------------------------------------------------------------------------
39     Fill-related
40    ------------------------------------------------------------------------ */
41 
42 // Zero out memory when memory is allocated.
43 #define MEM_HEAP_OPT_0_CLEAR         (1 << 0)
44 
45 // Memory fill when heap is created, memory is allocated, or memory is freed.
46 #define MEM_HEAP_OPT_DEBUG_FILL      (1 << 1)
47 
48 // Presence or absence of exclusive control
49 #define MEM_HEAP_OPT_THREAD_SAFE     (1 << 2)
50 
51 
52 /* ------------------------------------------------------------------------
53     Heap check related
54    ------------------------------------------------------------------------ */
55 //  If this bit stands, output error
56 #define MEM_HEAP_ERROR_PRINT         (1 << 0)
57 
58 
59 
60 // Signature of expanded heap
61 #define MEMi_EXPHEAP_SIGNATURE       ('EXPH')
62 // Signature of frame heap
63 #define MEMi_FRMHEAP_SIGNATURE       ('FRMH')
64 // Signature of unit heap
65 #define MEMi_UNTHEAP_SIGNATURE       ('UNTH')
66 
67 /* ========================================================================
68     enum constant
69    ======================================================================== */
70 
71 typedef enum
72 {
73     MEM_HEAP_TYPE_EXP,      // Expanded heap
74     MEM_HEAP_TYPE_FRM,      // Frame heap
75     MEM_HEAP_TYPE_UNIT,     // Unit heap
76     MEM_HEAP_TYPE_UNKNOWN   // Unknown heap handle
77 }
78 MEMHeapType;
79 
80 enum
81 {
82     MEM_HEAP_FILL_NOUSE,    // When debug fill is not used
83     MEM_HEAP_FILL_ALLOC,    // When debug fill is allocated
84     MEM_HEAP_FILL_FREE,     // When debug fill is freed
85 
86     MEM_HEAP_FILL_MAX
87 };
88 
89 
90 
91 /* =======================================================================
92     Type Definitions
93    ======================================================================== */
94 
95 typedef struct MEMiHeapHead MEMiHeapHead;
96 
97 // Header common among heaps
98 struct MEMiHeapHead
99 {
100     u32       signature;
101 
102     MEMLink   link;
103     MEMList   childList;
104 
105     void*     heapStart;      // Heap start address
106     void*     heapEnd;        // Heap end (+1) address
107     OSMutex   mutex;          // For exclusive use between threads
108 
109     union                     // Attribute
110     {
111         u32       val;
112         struct
113         {
114             u32   _reserved : 24;
115             u32   optFlag   :  8; // Option flag.
116         }
117         fields;
118     }
119     attribute;
120 };
121 
122 
123 typedef MEMiHeapHead* MEMHeapHandle;   // Type to represent heap handle
124 
125 
126 /* =======================================================================
127     Function Prototype
128    ======================================================================== */
129 
130 MEMHeapHandle    MEMFindContainHeap( const void* memBlock );
131 
132 MEMHeapHandle    MEMFindParentHeap( MEMHeapHandle heap );
133 
134 
135 /* ========================================================================
136     Inline function
137    ======================================================================== */
138 
139 /*---------------------------------------------------------------------------*
140   Name:         MEMGetHeapStartAddress
141 
142   Description:  Get start address of memory area used by heap
143 
144   Arguments:    heap: 	Heap handle
145 
146   Returns:      Return start address of memory area used by heap
147  *---------------------------------------------------------------------------*/
148 static inline void*
MEMGetHeapStartAddress(MEMHeapHandle heap)149 MEMGetHeapStartAddress( MEMHeapHandle heap )
150 {
151     return (void*)heap;
152 }
153 
154 /*---------------------------------------------------------------------------*
155   Name:         MEMGetHeapEndAddress
156 
157   Description:  Get end address +1 of memory area used by heap
158 
159   Arguments:    heap: 	Heap handle
160 
161   Returns:      Return end address +1 of memory area used by heap
162  *---------------------------------------------------------------------------*/
163 static inline void*
MEMGetHeapEndAddress(MEMHeapHandle heap)164 MEMGetHeapEndAddress( MEMHeapHandle heap )
165 {
166     return heap->heapEnd;
167 }
168 
169 /*---------------------------------------------------------------------------*
170   Name:         MEMGetHeapTotalUsableSize
171 
172   Description:  Gets the memory size allocated to the heap.
173                 The header is not included.
174 
175   Arguments:    heap: 	Heap handle
176 
177   Returns:      Returns the memory size allocated to the heap. The header is not included.
178  *---------------------------------------------------------------------------*/
179 static inline s32
MEMGetHeapTotalUsableSize(MEMHeapHandle heap)180 MEMGetHeapTotalUsableSize( MEMHeapHandle heap )
181 {
182     return ((s32)(heap->heapEnd) - (s32)(heap->heapStart));
183 }
184 
185 /*---------------------------------------------------------------------------*
186   Name:         MemGetHeapTotalSize
187 
188   Description:  Gets the memory size allocated to the heap (including that of the header).
189 
190 
191   Arguments:    heap:    	heap handle
192 
193   Returns:      Returns the memory size allocated to the heap (including that of the header).
194 
195  *---------------------------------------------------------------------------*/
196 static inline s32
MEMGetHeapTotalSize(MEMHeapHandle heap)197 MEMGetHeapTotalSize( MEMHeapHandle heap )
198 {
199     return ((s32)(heap->heapEnd) - (s32)(heap));
200 }
201 
202 
203 /*---------------------------------------------------------------------------*
204   Name:         MEMGetHeapType
205 
206   Description:  Gets the type of heap specific to the heap handle.
207 
208   Arguments:    heap: 	Heap handle
209 
210   Returns:      Gets the heap type from the heap handle.
211  *---------------------------------------------------------------------------*/
212 static inline MEMHeapType
MEMGetHeapType(MEMHeapHandle heap)213 MEMGetHeapType( MEMHeapHandle heap )
214 {
215     switch ( heap->signature )
216     {
217     case MEMi_EXPHEAP_SIGNATURE:    return MEM_HEAP_TYPE_EXP;
218     case MEMi_FRMHEAP_SIGNATURE:    return MEM_HEAP_TYPE_FRM;
219     case MEMi_UNTHEAP_SIGNATURE:    return MEM_HEAP_TYPE_UNIT;
220     default:                        return MEM_HEAP_TYPE_UNKNOWN;
221     }
222 }
223 
224 
225 /*---------------------------------------------------------------------------*
226   Name:         MEMIsExpHeap
227 
228   Description:  Determines whether the heap handle is for an expanded heap.
229 
230   Arguments:    heap:    	heap handle
231 
232   Returns:      Returns TRUE if an expanded heap.
233                 Else returns FALSE.
234  *---------------------------------------------------------------------------*/
235 static inline BOOL
MEMIsExpHeap(MEMHeapHandle heap)236 MEMIsExpHeap( MEMHeapHandle heap )
237 {
238     return ( heap->signature == MEMi_EXPHEAP_SIGNATURE );
239 }
240 
241 
242 /*---------------------------------------------------------------------------*
243   Name:         MEMIsFrmHeap
244 
245   Description:  Determines whether the heap handle is for a frame heap.
246 
247   Arguments:    heap:    	heap handle
248 
249   Returns:      Returns TRUE if a frame heap.
250                 Else returns FALSE.
251  *---------------------------------------------------------------------------*/
252 static inline BOOL
MEMIsFrmHeap(MEMHeapHandle heap)253 MEMIsFrmHeap( MEMHeapHandle heap )
254 {
255     return ( heap->signature == MEMi_FRMHEAP_SIGNATURE );
256 }
257 
258 
259 /*---------------------------------------------------------------------------*
260   Name:         MEMIsUnitHeap
261 
262   Description:  Determines whether the heap handle is for a unit heap.
263 
264   Arguments:    heap:    	heap handle
265 
266   Returns:      Returns TRUE if a unit heap.
267                 Else returns FALSE.
268  *---------------------------------------------------------------------------*/
269 static inline BOOL
MEMIsUnitHeap(MEMHeapHandle heap)270 MEMIsUnitHeap( MEMHeapHandle heap )
271 {
272     return ( heap->signature == MEMi_UNTHEAP_SIGNATURE );
273 }
274 
275 
276 
277 /* =======================================================================
278     Debug Functions
279    ======================================================================== */
280 #if ! defined(_DEBUG)
281 
282 #define   MEMDumpHeap( heap )                  ((void)0)
283 #define   MEMSetFillValForHeap( type, val )    (0)
284 #define   MEMGetFillValForHeap( type )         (0)
285 
286 #else
287 
288 void      MEMDumpHeap( MEMHeapHandle heap );
289 
290 u32       MEMSetFillValForHeap( int type, u32 val );
291 
292 u32       MEMGetFillValForHeap( int type );
293 
294 #endif
295 
296 
297 #ifdef __cplusplus
298 } /* extern "C" */
299 #endif
300 
301 /* MEM_HEAPCOMMON_H__ */
302 #endif
303 
304