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