1 /*---------------------------------------------------------------------------*
2 Project: MEM library
3 File: expHeap.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
15 #ifndef MEM_EXPHEAP_H__
16 #define MEM_EXPHEAP_H__
17
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21
22
23 #include <revolution/mem/heapCommon.h>
24
25 /* =======================================================================
26 Constant Definitions
27 ======================================================================== */
28
29 // Memory allocation direction
30 enum
31 {
32 MEM_EXPHEAP_ALLOC_DIR_FRONT, // Allocate from front
33 MEM_EXPHEAP_ALLOC_DIR_REAR // Allocate from rear
34 };
35
36 // Memory allocation mode
37 enum
38 {
39 /*
40 If this attribute value is set, the function allocates a memory block from the first unused region found that is larger than the memory block size.
41
42
43 */
44 MEM_EXPHEAP_ALLOC_MODE_FIRST,
45
46 /*
47 If this attribute value is set, the function searches for the unused region closest in size to the memory block to be allocated, and allocates the memory block from that unused region.
48
49
50 */
51 MEM_EXPHEAP_ALLOC_MODE_NEAR
52 };
53
54
55 /* =======================================================================
56 Type Definitions
57 ======================================================================== */
58
59 typedef struct MEMiExpHeapMBlockHead MEMiExpHeapMBlockHead;
60
61 // Header information for memory block
62 struct MEMiExpHeapMBlockHead
63 {
64 u16 signature; // Signature
65 union // Attribute
66 {
67 u16 val;
68 struct
69 {
70 u16 allocDir : 1; // Memory allocation direction
71 u16 alignment : 7; // Alignment
72 u16 groupID : 8; // Group ID
73 }
74 fields;
75 }
76 attribute;
77
78 u32 blockSize; // Block size (data area only)
79
80 MEMiExpHeapMBlockHead* pMBHeadPrev; // Previous block
81 MEMiExpHeapMBlockHead* pMBHeadNext; // Next block
82 };
83
84 typedef struct MEMiExpMBlockList MEMiExpMBlockList;
85
86 // Memory block list
87 struct MEMiExpMBlockList
88 {
89 MEMiExpHeapMBlockHead* head; // Pointer for memory block linked to header
90 MEMiExpHeapMBlockHead* tail; // Pointer to the memory block linked to the tail of the expanded heap
91 };
92
93 typedef struct MEMiExpHeapHead MEMiExpHeapHead;
94
95 // Header information for expanded heap
96 struct MEMiExpHeapHead
97 {
98 MEMiExpMBlockList mbFreeList; // Free list
99 MEMiExpMBlockList mbUsedList; // Used list
100
101 u16 groupID; // Current group ID (lower 8 bits only)
102
103 union // Attribute
104 {
105 u16 val;
106 struct
107 {
108 u16 _reserved : 15;
109 u16 allocMode : 1; // Memory allocation mode
110 }
111 fields;
112 }
113 feature;
114 };
115
116
117
118 // Callback function type called for every memory block
119 typedef void (*MEMHeapVisitor)( void* memBlock, MEMHeapHandle heap, u32 userParam );
120
121
122 /* =======================================================================
123 Macro Functions
124 ======================================================================== */
125
126
127 /* =======================================================================
128 Function Prototype
129 ======================================================================== */
130
131 #if defined(_DEBUG)
132
133 void MEMiDumpExpHeap( MEMHeapHandle heap );
134
135
136 // #if defined(_DEBUG)
137 #endif
138
139
140 MEMHeapHandle MEMCreateExpHeapEx(
141 void* startAddress,
142 u32 size,
143 u16 optFlag );
144
145 void* MEMDestroyExpHeap( MEMHeapHandle heap );
146
147 void* MEMAllocFromExpHeapEx(
148 MEMHeapHandle heap,
149 u32 size,
150 int alignment );
151
152 u32 MEMResizeForMBlockExpHeap(
153 MEMHeapHandle heap,
154 void* memBlock,
155 u32 size );
156
157 void MEMFreeToExpHeap( MEMHeapHandle heap, void* memBlock );
158
159 u32 MEMGetTotalFreeSizeForExpHeap( MEMHeapHandle heap );
160
161 u32 MEMGetAllocatableSizeForExpHeapEx( MEMHeapHandle heap, int alignment );
162
163 BOOL MEMiIsEmptyExpHeap( MEMHeapHandle heap );
164
165 u16 MEMSetAllocModeForExpHeap( MEMHeapHandle heap, u16 mode );
166
167 u16 MEMGetAllocModeForExpHeap( MEMHeapHandle heap );
168
169 u16 MEMSetGroupIDForExpHeap( MEMHeapHandle heap, u16 groupID );
170
171 u16 MEMGetGroupIDForExpHeap( MEMHeapHandle heap );
172
173 void MEMVisitAllocatedForExpHeap(
174 MEMHeapHandle heap,
175 MEMHeapVisitor visitor,
176 u32 userParam );
177
178 u32 MEMGetSizeForMBlockExpHeap( const void* memBlock );
179
180 u16 MEMGetGroupIDForMBlockExpHeap( const void* memBlock );
181
182 u16 MEMGetAllocDirForMBlockExpHeap( const void* memBlock );
183
184 u32 MEMAdjustExpHeap( MEMHeapHandle heap );
185
186
187 /* =======================================================================
188 Inline function
189 ======================================================================== */
190
191 /*---------------------------------------------------------------------------*
192 Name: MEMCreateExpHeap
193
194 Description: Creates an expanded heap.
195
196 Arguments: startAddress: Start address of heap area
197 size: Size of heap area
198
199 Returns: If the function succeeds, a handle for the created expanded heap is returned.
200 If the function fails, HEAP_INVALID_HANDLE is returned.
201 *---------------------------------------------------------------------------*/
202 static inline MEMHeapHandle
MEMCreateExpHeap(void * startAddress,u32 size)203 MEMCreateExpHeap(
204 void* startAddress,
205 u32 size
206 )
207 {
208 return MEMCreateExpHeapEx( startAddress, size, 0 );
209 }
210
211
212 /*---------------------------------------------------------------------------*
213 Name: MEMAllocFromExpHeap
214
215 Description: Allocates a memory block from the expanded heap.
216 Alignment of the memory block is 4-byte fixed.
217
218 Arguments: heap: Handle for expanded heap
219 size: Size of the memory block to be allocated (in bytes)
220
221 Returns: Returns the pointer to the allocated memory block if the memory block was successfully allocated.
222
223 If the operation fails, NULL is returned.
224 *---------------------------------------------------------------------------*/
225 static inline void*
MEMAllocFromExpHeap(MEMHeapHandle heap,u32 size)226 MEMAllocFromExpHeap(
227 MEMHeapHandle heap,
228 u32 size
229 )
230 {
231 return MEMAllocFromExpHeapEx( heap, size, MEM_HEAP_DEFAULT_ALIGNMENT );
232 }
233
234 /*---------------------------------------------------------------------------*
235 Name: MEMGetAllocatableSizeForExpHeap
236
237 Description: Gets a memory block of the maximum allocatable size from the expanded heap.
238 Alignment of the memory block is 4-byte fixed.
239
240 Arguments: heap: Handle for expanded heap
241
242 Returns: Returns the maximum allocatable size from the expanded heap (in bytes).
243 *---------------------------------------------------------------------------*/
244 static inline u32
MEMGetAllocatableSizeForExpHeap(MEMHeapHandle heap)245 MEMGetAllocatableSizeForExpHeap( MEMHeapHandle heap )
246 {
247 return MEMGetAllocatableSizeForExpHeapEx( heap, MEM_HEAP_DEFAULT_ALIGNMENT );
248 }
249
250
251
252 #if ! defined(_DEBUG)
253
254 #define MEMCheckExpHeap( heap, optFlag ) (TRUE)
255 #define MEMCheckForMBlockExpHeap( memBlock, heap, optFlag ) (TRUE)
256
257 // #if ! defined(_DEBUG)
258 #else
259
260 BOOL MEMCheckExpHeap( MEMHeapHandle heap, u32 optFlag );
261
262 BOOL MEMCheckForMBlockExpHeap(
263 const void* memBlock,
264 MEMHeapHandle heap,
265 u32 optFlag );
266
267 // #if defined(_DEBUG)
268 #endif
269
270
271
272 #ifdef __cplusplus
273 } /* extern "C" */
274 #endif
275
276 /* MEM_EXPHEAP_H__ */
277 #endif
278