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