1 /*---------------------------------------------------------------------------*
2   Project:  NET Initialize demo
3   File:     heap.c
4 
5   Copyright 2006 Nintendo. All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12 
13   $Log: heap.c,v $
14   Revision 1.7  2007/06/23 12:21:28  seiki_masashi
15   Added REXDEMOCreateMEM1Heap, and so on.
16 
17   Revision 1.6  2007/05/28 08:40:57  hirose_kazuki
18   Small fix.
19 
20   Revision 1.5  2007/05/28 08:36:34  hirose_kazuki
21   Added REXDEMODestroyHeapForSocket()
22 
23   Revision 1.4  2006/10/30 10:01:55  yasu
24   Add comment for heap for socket.
25 
26   Revision 1.3  2006/08/29 07:19:20  adachi_hiroaki
27   Changed prefix. Reorganized others.
28 
29   Revision 1.2  2006/08/25 02:14:11  adachi_hiroaki
30   Made changes so that failure in heap creation will be displayed.
31 
32   Revision 1.1  2006/08/10 12:10:04  adachi_hiroaki
33   Added features
34 
35 
36   $NoKeywords: $
37  *---------------------------------------------------------------------------*/
38 
39 #include <revolution/os.h>
40 #include <revolution/mem.h>
41 #include "rexdemo/heap.h"
42 
43 /*---------------------------------------------------------------------------*/
44 
45 static MEMHeapHandle    heapHandleSocket = NULL;
46 static MEMHeapHandle    heapHandleMEM1   = NULL;
47 
48 
49 void*
REXDEMOAllocForSocket(u32 name,s32 size)50 REXDEMOAllocForSocket( u32 name, s32 size )
51 {
52 #pragma unused( name )
53     void* ptr;
54 
55     if (0 < size)
56     {
57         // This is thread-safe, because the MEM_HEAP_OPT_THREAD_SAFE
58         // option is specified in MEMCreateExpHeapEx().
59         ptr = MEMAllocFromExpHeapEx( heapHandleSocket, (u32) size, 32 );
60         return ptr;
61     }
62     else
63     {
64         return NULL;
65     }
66 }
67 
68 void
REXDEMOFreeForSocket(u32 name,void * ptr,s32 size)69 REXDEMOFreeForSocket( u32 name, void* ptr, s32 size )
70 {
71 #pragma unused( name )
72 
73     if (ptr && 0 < size)
74     {
75         // This is thread-safe, because
76         // the MEM_HEAP_OPT_THREAD_SAFE option is specified.
77         MEMFreeToExpHeap( heapHandleSocket, ptr );
78     }
79 }
80 
81 BOOL
REXDEMOCreateHeapForSocket(u32 size)82 REXDEMOCreateHeapForSocket( u32 size )
83 {
84     void*            arenaLo;
85     void*            arenaHi;
86 
87     //
88     // Initialize the heap for Alloc() and Free().
89     //
90     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
91     // !!! Heap area for socket library must be in MEM2 !!!
92     // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
93     arenaLo = OSGetMEM2ArenaLo();
94     arenaHi = OSGetMEM2ArenaHi();
95 
96     if ((u32) arenaHi - (u32) arenaLo < size)
97     {
98         return FALSE;
99     }
100     heapHandleSocket = MEMCreateExpHeapEx( arenaLo, size, MEM_HEAP_OPT_THREAD_SAFE );
101     if (heapHandleSocket == MEM_HEAP_INVALID_HANDLE)
102     {
103         OSReport("MEMCreateExpHeapEx failed.\n");
104         return FALSE;
105     }
106 
107     OSSetMEM2ArenaLo((u8*)arenaLo + size);
108     return TRUE;
109 }
110 
111 void
REXDEMODestroyHeapForSocket(void)112 REXDEMODestroyHeapForSocket( void )
113 {
114     s32   heapSize;
115     void* heapPtr;
116 
117     if ( heapHandleSocket == NULL )
118         return;
119 
120     heapSize = MEMGetHeapTotalSize(heapHandleSocket);
121     heapPtr  = MEMDestroyExpHeap(heapHandleSocket);
122 
123     if ( heapPtr != NULL )
124     {
125         if ( (u32)OSGetMEM2ArenaLo() == (u32)heapPtr + heapSize )
126         {
127             OSSetMEM2ArenaLo(heapPtr);
128         }
129     }
130     heapHandleSocket = NULL;
131 
132     return;
133 }
134 
135 
136 SOLibraryConfig*
REXDEMOGetSOLibConfig(void)137 REXDEMOGetSOLibConfig( void )
138 {
139     static SOLibraryConfig libConfig = { REXDEMOAllocForSocket, REXDEMOFreeForSocket };
140     if (heapHandleSocket)
141     {
142         return &libConfig;
143     }
144     else
145     {
146         return NULL;
147     }
148 }
149 
REXDEMOCreateMEM1Heap(u32 size)150 BOOL REXDEMOCreateMEM1Heap( u32 size )
151 {
152     void*            arenaLo;
153     void*            arenaHi;
154 
155     //
156     // Initialize the heap for Alloc() and Free().
157     //
158     arenaLo = OSGetMEM1ArenaLo();
159     arenaHi = OSGetMEM1ArenaHi();
160 
161     if ((u32) arenaHi - (u32) arenaLo < size)
162     {
163         return FALSE;
164     }
165     heapHandleMEM1 = MEMCreateExpHeapEx( arenaLo, size, MEM_HEAP_OPT_THREAD_SAFE );
166     if (heapHandleMEM1 == MEM_HEAP_INVALID_HANDLE)
167     {
168         OSReport("MEMCreateExpHeapEx failed.\n");
169         return FALSE;
170     }
171 
172     OSSetMEM1ArenaLo((u8*)arenaLo + size);
173     return TRUE;
174 }
175 
REXDEMODestroyMEM1Heap(void)176 void REXDEMODestroyMEM1Heap( void )
177 {
178     s32   heapSize;
179     void* heapPtr;
180 
181     if ( heapHandleMEM1 == NULL )
182         return;
183 
184     heapSize = MEMGetHeapTotalSize(heapHandleMEM1);
185     heapPtr  = MEMDestroyExpHeap(heapHandleMEM1);
186 
187     if ( heapPtr != NULL )
188     {
189         if ( (u32)OSGetMEM1ArenaLo() == (u32)heapPtr + heapSize )
190         {
191             OSSetMEM1ArenaLo(heapPtr);
192         }
193     }
194     heapHandleMEM1 = NULL;
195 
196     return;
197 }
198 
REXDEMOMEM1Alloc(s32 size)199 void* REXDEMOMEM1Alloc( s32 size )
200 {
201     void* ptr;
202 
203     if (0 < size)
204     {
205         // This is thread-safe, because the MEM_HEAP_OPT_THREAD_SAFE
206         // option is specified in MEMCreateExpHeapEx().
207         ptr = MEMAllocFromExpHeapEx( heapHandleMEM1, (u32) size, 32 );
208         return ptr;
209     }
210     else
211     {
212         return NULL;
213     }
214 }
215 
REXDEMOMEM1Free(void * ptr)216 void REXDEMOMEM1Free( void* ptr )
217 {
218     if (ptr)
219     {
220         // This is thread-safe, because
221         // the MEM_HEAP_OPT_THREAD_SAFE option is specified.
222         MEMFreeToExpHeap( heapHandleMEM1, ptr );
223     }
224 }
225 
226 
227 
228 /*---------------------------------------------------------------------------*
229   End of file
230  *---------------------------------------------------------------------------*/
231