1 /*---------------------------------------------------------------------------*
2 
3   Copyright (C) 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:        unitHeap.h
15   Programmers: Takano Makoto
16 
17   Copyright (C) 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_UNITHEAP_H__
27 #define MEM_UNITHEAP_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     Type Definitions
39    ======================================================================== */
40 
41 typedef struct MEMiUntHeapMBlockHead MEMiUntHeapMBlockHead;
42 
43 // Header information for the memory block.
44 struct MEMiUntHeapMBlockHead
45 {
46     MEMiUntHeapMBlockHead*  pMBlkHdNext;    // Next block.
47 };
48 
49 
50 typedef struct MEMiUntMBlockList MEMiUntMBlockList;
51 
52 // Memory block list.
53 struct MEMiUntMBlockList
54 {
55     MEMiUntHeapMBlockHead*  head;           // Pointer to the memory block linked to the header.
56 };
57 
58 typedef struct MEMiUntHeapHead MEMiUntHeapHead;
59 
60 // Header information for the unit heap.
61 struct MEMiUntHeapHead
62 {
63     MEMiUntMBlockList    mbFreeList;     // Free list.
64     u32                  mBlkSize;       // Memory block size.
65 };
66 
67 
68 
69 /* ========================================================================
70     Macro Functions
71    ======================================================================== */
72 
73 
74 /* ========================================================================
75     Function Prototypes
76    ======================================================================== */
77 
78 void        MEMiDumpUnitHeap( MEMHeapHandle heap );
79 
80 MEMHeapHandle  MEMCreateUnitHeapEx(
81                         void*           startAddress,
82                         u32             heapSize,
83                         u32             memBlockSize,
84                         int             alignment,
85                         u16             optFlag );
86 
87 void*       MEMDestroyUnitHeap  ( MEMHeapHandle heap );
88 
89 void*       MEMAllocFromUnitHeap( MEMHeapHandle heap );
90 
91 void        MEMFreeToUnitHeap(
92                         MEMHeapHandle  heap,
93                         void*          memBlock );
94 
95 u32         MEMCountFreeBlockForUnitHeap( MEMHeapHandle heap );
96 
97 u32         MEMCalcHeapSizeForUnitHeap(
98                         u32             memBlockSize,
99                         u32             memBlockNum,
100                         int             alignment );
101 
102 /* =======================================================================
103     Inline Functions
104    ======================================================================== */
105 /*---------------------------------------------------------------------------*
106   Name:         MEMCreateUnitHeap
107 
108   Description:  Creates the unit heap.
109 
110   Arguments:    startAddress:  Start address of the heap area.
111                 heapSize:      Size of the heap area.
112                 memBlockSize:  Memory block size.
113 
114   Returns:      If the function succeeds, a handle for the created unit heap is returned.
115                 If the function fails, NNS_FND_INVALID_HEAP_HANDLE is returned.
116  *---------------------------------------------------------------------------*/
117 static inline MEMHeapHandle
MEMCreateUnitHeap(void * startAddress,u32 heapSize,u32 memBlockSize)118 MEMCreateUnitHeap(
119     void*   startAddress,
120     u32     heapSize,
121     u32     memBlockSize
122 )
123 {
124     return MEMCreateUnitHeapEx( startAddress, heapSize, memBlockSize, MEM_HEAP_DEFAULT_ALIGNMENT, 0 );
125 }
126 
127 
128 /*---------------------------------------------------------------------------*
129   Name:         MEMGetMemBlockSizeForUnitHeap
130 
131   Description:  Gets the memory block size for the unit heap.
132 
133   Arguments:    heap:  Unit heap handle.
134 
135   Returns:      Returns the memory block size for the unit heap.
136  *---------------------------------------------------------------------------*/
137 static inline u32
MEMGetMemBlockSizeForUnitHeap(MEMHeapHandle heap)138 MEMGetMemBlockSizeForUnitHeap( MEMHeapHandle heap )
139 {
140     return ( ( (const MEMiUntHeapHead*)( (const u8*)heap + sizeof(MEMiHeapHead) ) )->mBlkSize );
141 }
142 
143 
144 #ifdef __cplusplus
145 } /* extern "C" */
146 #endif
147 
148 /* MEM_UNITHEAP_H__ */
149 #endif
150 
151