1 /*---------------------------------------------------------------------------*
2   Project:     MEM library
3   File:        frameHeap.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_FRAMEHEAP_H__
16 #define MEM_FRAMEHEAP_H__
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 
23 #include <revolution/types.h>
24 #include <revolution/mem/heapCommon.h>
25 
26 /* =======================================================================
27     Constant Definitions
28    ======================================================================== */
29 
30 #define MEM_FRMHEAP_FREE_HEAD (1 <<0)
31 #define MEM_FRMHEAP_FREE_TAIL (1 <<1)
32 #define MEM_FRMHEAP_FREE_ALL  (MEM_FRMHEAP_FREE_HEAD | MEM_FRMHEAP_FREE_TAIL)
33 
34 
35 /* =======================================================================
36     Type Definitions
37    ======================================================================== */
38 
39 typedef struct MEMiFrmHeapState MEMiFrmHeapState;
40 
41 // Structure for state storage
42 struct MEMiFrmHeapState
43 {
44     u32                 tagName;        // Tag name
45     void*               headAllocator;  // Head location of frame heap
46     void*               tailAllocator;  // Tail location of frame heap
47     MEMiFrmHeapState*   pPrevState;     // Pointer for last state storage
48 };
49 
50 typedef struct MEMiFrmHeapHead MEMiFrmHeapHead;
51 
52 // Header information for frame heap
53 struct MEMiFrmHeapHead
54 {
55     void*               headAllocator;  // Pointer for head memory allocation
56     void*               tailAllocator;  // Pointer for tail memory allocation
57 
58     MEMiFrmHeapState*   pState;         // State storage parameter
59 };
60 
61 
62 /* =======================================================================
63     Macro Functions
64    ======================================================================== */
65 
66 
67 /* =======================================================================
68     Function Prototype
69    ======================================================================== */
70 
71 void*       MEMiGetFreeStartForFrmHeap( MEMHeapHandle heap );
72 
73 void*       MEMiGetFreeEndForFrmHeap  ( MEMHeapHandle heap );
74 
75 #if defined(_DEBUG)
76 
77 void        MEMiDumpFrmHeap( MEMHeapHandle heap );
78 
79 // #if defined(_DEBUG)
80 #endif
81 
82 MEMHeapHandle  MEMCreateFrmHeapEx(
83                                 void*   startAddress,
84                                 u32     size,
85                                 u16     optFlag );
86 
87 void*            MEMDestroyFrmHeap( MEMHeapHandle heap );
88 
89 void*            MEMAllocFromFrmHeapEx(
90                                 MEMHeapHandle   heap,
91                                 u32             size,
92                                 int             alignment );
93 
94 void             MEMFreeToFrmHeap( MEMHeapHandle heap, int mode );
95 
96 u32              MEMGetAllocatableSizeForFrmHeapEx( MEMHeapHandle heap, int alignment );
97 
98 BOOL             MEMRecordStateForFrmHeap( MEMHeapHandle heap, u32 tagName );
99 
100 BOOL             MEMFreeByStateToFrmHeap( MEMHeapHandle heap, u32 tagName );
101 
102 u32              MEMAdjustFrmHeap( MEMHeapHandle heap );
103 
104 u32              MEMResizeForMBlockFrmHeap(
105                                 MEMHeapHandle    heap,
106                                 void*            memBlock,
107                                 u32              newSize );
108 
109 
110 /* =======================================================================
111     Inline function
112    ======================================================================== */
113 
114 /*---------------------------------------------------------------------------*
115   Name:         MEMCreateFrmHeap
116 
117   Description:  Creates a frame heap.
118 
119   Arguments:    startAddress: 	Start address of heap area
120                 size: 	Size of heap area
121 
122   Returns:      Returns the handle for the created frame heap if the function succeeds.
123                 MEM_INVALID_HEAP_HANDLE is returned if the function fails.
124  *---------------------------------------------------------------------------*/
125 static inline MEMHeapHandle
MEMCreateFrmHeap(void * startAddress,u32 size)126 MEMCreateFrmHeap(
127     void*   startAddress,
128     u32     size
129 )
130 {
131     return MEMCreateFrmHeapEx( startAddress, size, 0 );
132 }
133 
134 
135 /*---------------------------------------------------------------------------*
136   Name:         MEMAllocFromFrmHeap
137 
138   Description:  Allocates a memory block from the frame heap.
139                 Alignment of the memory block is 4-byte fixed.
140 
141   Arguments:    heap: 	Handle for the frame heap
142                 size: 	Size of the memory block to be allocated (in bytes)
143 
144   Returns:      Returns the pointer to the allocated memory block if the memory block was successfully allocated.
145 
146                 If the operation fails, NULL is returned.
147  *---------------------------------------------------------------------------*/
148 static inline void*
MEMAllocFromFrmHeap(MEMHeapHandle heap,u32 size)149 MEMAllocFromFrmHeap(
150     MEMHeapHandle   heap,
151     u32             size
152 )
153 {
154     return MEMAllocFromFrmHeapEx( heap, size, MEM_HEAP_DEFAULT_ALIGNMENT );
155 }
156 
157 
158 /*---------------------------------------------------------------------------*
159   Name:         MEMGetAllocatableSizeForFrmHeap
160 
161   Description:  Gets the maximum allocatable size in the frame heap.
162                 Alignment of the memory block is 4-byte fixed.
163 
164   Arguments:    heap: 	Handle for the frame heap
165 
166   Returns:      Returns the maximum allocatable size in the frame heap (in bytes).
167  *---------------------------------------------------------------------------*/
168 static inline u32
MEMGetAllocatableSizeForFrmHeap(MEMHeapHandle heap)169 MEMGetAllocatableSizeForFrmHeap( MEMHeapHandle heap )
170 {
171     return MEMGetAllocatableSizeForFrmHeapEx( heap, MEM_HEAP_DEFAULT_ALIGNMENT );
172 }
173 
174 
175 #ifdef __cplusplus
176 } /* extern "C" */
177 #endif
178 
179 /* MEM_FRAMEHEAP_H__ */
180 #endif
181