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