1 /*---------------------------------------------------------------------------*
2   Project:     MEM library
3   File:        expHeap.h
4   Programmers: Makoto Takano
5 
6   Copyright (C)2005-2007 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 back
34 };
35 
36 // Memory allocation mode
37 enum
38 {
39     /*
40         When this attribute value is set, a memory block is allocated from the first unused region found that is larger than the size of the memory block for which allocation is attempted.
41 
42 
43     */
44     MEM_EXPHEAP_ALLOC_MODE_FIRST = 0,
45 
46     /*
47         When this attribute value is set, search for an empty region whose size is closest to the size of the memory block for which allocation is attempted, and allocate a memory block from this region.
48 
49 
50     */
51     MEM_EXPHEAP_ALLOC_MODE_NEAR = 1
52 };
53 
54 
55 /* =======================================================================
56     Type Definitions
57    ======================================================================== */
58 
59 typedef struct MEMiExpHeapMBlockHead MEMiExpHeapMBlockHead;
60 
61 // Header information for a memory block (16 bytes)
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          : 14;
109             u16            useMarginOfAlign   :  1; // If this attribute value is set, gaps in memory that occur during alignment will be reused as empty regions.
110                                                     //
111             u16            allocMode          :  1; // Memory allocation mode
112         }
113         fields;
114     }
115     feature;
116 };
117 
118 
119 // Callback function type called for every memory block
120 typedef void        (*MEMHeapVisitor)( void* memBlock, MEMHeapHandle heap, u32 userParam );
121 
122 
123 /* =======================================================================
124     Macro Functions
125    ======================================================================== */
126 
127 
128 /* =======================================================================
129     Function Prototype
130    ======================================================================== */
131 
132 #if defined(_DEBUG)
133 
134 void        MEMiDumpExpHeap( MEMHeapHandle heap );
135 
136 
137 // #if defined(_DEBUG)
138 #endif
139 
140 
141 MEMHeapHandle  MEMCreateExpHeapEx(
142                         void*       startAddress,
143                         u32         size,
144                         u16         optFlag );
145 
146 void*       MEMDestroyExpHeap( MEMHeapHandle heap );
147 
148 void*       MEMAllocFromExpHeapEx(
149                         MEMHeapHandle heap,
150                         u32           size,
151                         int           alignment );
152 
153 u32         MEMResizeForMBlockExpHeap(
154                         MEMHeapHandle     heap,
155                         void*             memBlock,
156                         u32               size );
157 
158 void        MEMFreeToExpHeap( MEMHeapHandle heap, void* memBlock );
159 
160 u32         MEMGetTotalFreeSizeForExpHeap( MEMHeapHandle heap );
161 
162 u32         MEMGetAllocatableSizeForExpHeapEx( MEMHeapHandle heap, int alignment );
163 
164 BOOL        MEMiIsEmptyExpHeap( MEMHeapHandle heap );
165 
166 u16         MEMSetAllocModeForExpHeap( MEMHeapHandle heap, u16 mode );
167 u16         MEMGetAllocModeForExpHeap( MEMHeapHandle heap );
168 
169 BOOL        MEMUseMarginOfAlignmentForExpHeap( MEMHeapHandle heap, BOOL reuse );
170 
171 u16         MEMSetGroupIDForExpHeap( MEMHeapHandle heap, u16 groupID );
172 u16         MEMGetGroupIDForExpHeap( MEMHeapHandle heap );
173 
174 void        MEMVisitAllocatedForExpHeap(
175                         MEMHeapHandle    heap,
176                         MEMHeapVisitor   visitor,
177                         u32              userParam );
178 
179 
180 u32         MEMGetSizeForMBlockExpHeap( const void* memBlock );
181 
182 u16         MEMGetGroupIDForMBlockExpHeap( const void* memBlock );
183 
184 u16         MEMGetAllocDirForMBlockExpHeap( const void* memBlock );
185 
186 u32         MEMAdjustExpHeap( MEMHeapHandle heap );
187 
188 
189 /* =======================================================================
190     Inline functions
191    ======================================================================== */
192 
193 /*---------------------------------------------------------------------------*
194   Name:         MEMCreateExpHeap
195 
196   Description:  Creates an expanded heap.
197 
198   Arguments:    startAddress: Start address of heap area
199                 size:         Size of heap area
200 
201   Returns:      If the function succeeds, a handle for the created expanded heap is returned.
202                 If the function fails, HEAP_INVALID_HANDLE is returned.
203  *---------------------------------------------------------------------------*/
204 static inline MEMHeapHandle
MEMCreateExpHeap(void * startAddress,u32 size)205 MEMCreateExpHeap(
206     void*   startAddress,
207     u32     size
208 )
209 {
210     return MEMCreateExpHeapEx( startAddress, size, 0 );
211 }
212 
213 
214 /*---------------------------------------------------------------------------*
215   Name:         MEMAllocFromExpHeap
216 
217   Description:  Allocates a memory block from the expanded heap.
218                 Alignment of the memory block is 4-byte fixed.
219 
220   Arguments:    heap:   Handle for the expanded heap.
221                 size:   Size of the memory block to be allocated (in bytes)
222 
223   Returns:      Returns a pointer to the allocated memory block if it was successfully allocated.
224 
225                 If the operation fails, NULL is returned.
226  *---------------------------------------------------------------------------*/
227 static inline void*
MEMAllocFromExpHeap(MEMHeapHandle heap,u32 size)228 MEMAllocFromExpHeap(
229     MEMHeapHandle    heap,
230     u32              size
231 )
232 {
233     return MEMAllocFromExpHeapEx( heap, size, MEM_HEAP_DEFAULT_ALIGNMENT );
234 }
235 
236 /*---------------------------------------------------------------------------*
237   Name:         MEMGetAllocatableSizeForExpHeap
238 
239   Description:  Gets a memory block of the maximum allocatable size from the expanded heap.
240                 Alignment of the memory block is 4-byte fixed.
241 
242   Arguments:    heap:     Handle for the expanded heap.
243 
244   Returns:      Returns the maximum allocatable size from the expanded heap (in bytes).
245  *---------------------------------------------------------------------------*/
246 static inline u32
MEMGetAllocatableSizeForExpHeap(MEMHeapHandle heap)247 MEMGetAllocatableSizeForExpHeap( MEMHeapHandle heap )
248 {
249     return MEMGetAllocatableSizeForExpHeapEx( heap, MEM_HEAP_DEFAULT_ALIGNMENT );
250 }
251 
252 
253 
254 #if ! defined(_DEBUG)
255 
256 #define     MEMCheckExpHeap( heap, optFlag )                      (TRUE)
257 #define     MEMCheckForMBlockExpHeap( memBlock, heap, optFlag )   (TRUE)
258 
259 // #if ! defined(_DEBUG)
260 #else
261 
262 BOOL        MEMCheckExpHeap( MEMHeapHandle heap, u32 optFlag );
263 
264 BOOL        MEMCheckForMBlockExpHeap(
265                     const void*       memBlock,
266                     MEMHeapHandle     heap,
267                     u32               optFlag );
268 
269 // #if defined(_DEBUG)
270 #endif
271 
272 
273 
274 #ifdef __cplusplus
275 } /* extern "C" */
276 #endif
277 
278 /* MEM_EXPHEAP_H__ */
279 #endif
280