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