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