1 /*---------------------------------------------------------------------------*
2 Project: TwlSDK - OS - demos - heap-1
3 File: main.c
4
5 Copyright 2003-2008 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 $Date:: 2008-10-03#$
14 $Rev: 8857 $
15 $Author: yada $
16 *---------------------------------------------------------------------------*/
17 #include <nitro.h>
18
19 //----------------------------------------------------------------
20 #define MAIN_HEAP_SIZE1 0x10000
21 #define MAIN_HEAP_SIZE2 0x20000
22 #define MAIN_HEAP_SIZE3 0x8000
23
24 #define ALLOC_SIZE1 0x10
25 #define ALLOC_SIZE2 0x100
26 #define ALLOC_SIZE3 0x1000
27 #define ALLOC_SIZE4 0x2000
28 #define ALLOC_SIZE5 0x10
29
30 #define HEAP_NUM 2
31 #define DISPLAY_HEAP1 (1<<0)
32 #define DISPLAY_HEAP2 (1<<1)
33
34 OSHeapHandle handle1;
35 OSHeapHandle handle2;
36
37 void DisplayHeap(u32 heap);
38
39 //================================================================================
40 // Comprehensive heap sample
41 // Create a heap for the main memory arena, and attempt region allocation/deallocation.
42 //
43 /*---------------------------------------------------------------------------*
44 Name: NitroMain
45
46 Description: Main
47
48 Arguments: None.
49
50 Returns: None.
51 *---------------------------------------------------------------------------*/
NitroMain(void)52 void NitroMain(void)
53 {
54 void *heapArea1;
55 void *heapArea2;
56 void *heapArea3;
57 void *nstart;
58 void *area1;
59 void *area2;
60 void *area3;
61 void *area4;
62
63 OS_Init();
64 //OS_InitArena(); /*Called in OS_Init()*/
65
66
67 OS_Printf("================================ after OS_Init() ================\n", OS_GetMainArenaLo());
68 OS_Printf("arena lo = %x\n", OS_GetMainArenaLo());
69 OS_Printf("arena hi = %x\n", OS_GetMainArenaHi());
70
71 //---- Initialize memory allocation system for MainRAM arena
72 nstart = OS_InitAlloc(OS_ARENA_MAIN, OS_GetMainArenaLo(), OS_GetMainArenaHi(), HEAP_NUM);
73 OS_SetMainArenaLo(nstart);
74
75
76 //---- Allocate a heap from the arena and create the heap
77 OS_Printf("\n================================ create heap ================\n" );
78 heapArea1 = OS_AllocFromMainArenaLo(MAIN_HEAP_SIZE1, 32);
79 heapArea2 = OS_AllocFromMainArenaLo(MAIN_HEAP_SIZE2, 32);
80 handle1 = OS_CreateHeap(OS_ARENA_MAIN, heapArea1, (void *)((u32)heapArea1 + MAIN_HEAP_SIZE1));
81 handle2 = OS_CreateHeap(OS_ARENA_MAIN, heapArea2, (void *)((u32)heapArea2 + MAIN_HEAP_SIZE2));
82
83 OS_Printf("heap1 handle=%d area1 = %x-%x\n", handle1, heapArea1, (u32)heapArea1 + MAIN_HEAP_SIZE1);
84 OS_Printf("heap2 handle=%d area2 = %x-%x\n", handle2, heapArea2, (u32)heapArea2 + MAIN_HEAP_SIZE2);
85
86 //---- Current heap settings
87 (void)OS_SetCurrentHeap(OS_ARENA_MAIN, handle1);
88
89 DisplayHeap( DISPLAY_HEAP1 | DISPLAY_HEAP2 );
90
91 //---- Allocate the areas
92 OS_Printf("\n================================ allocate ================\n" );
93 area1 = OS_Alloc(ALLOC_SIZE1);
94 area2 = OS_Alloc(ALLOC_SIZE2);
95 area3 = OS_AllocFromHeap(OS_ARENA_MAIN, handle1, ALLOC_SIZE3);
96 area4 = OS_AllocFromHeap(OS_ARENA_MAIN, handle2, ALLOC_SIZE4);
97
98 OS_Printf("area1: heap1 size=%5x addr=%x\n", ALLOC_SIZE1, area1);
99 OS_Printf("area2: heap1 size=%5x addr=%x\n", ALLOC_SIZE2, area2);
100 OS_Printf("area3: heap1 size=%5x addr=%x\n", ALLOC_SIZE3, area3);
101 OS_Printf("area4: heap2 size=%5x addr=%x\n", ALLOC_SIZE4, area4);
102
103 DisplayHeap( DISPLAY_HEAP1 | DISPLAY_HEAP2 );
104
105
106 //---- Deallocate the areas
107 OS_Printf("\n================================ Free ================\n" );
108 OS_Free(area2);
109 OS_Printf("free area2.\n");
110
111 OS_FreeToHeap(OS_ARENA_MAIN, handle2, area4);
112 OS_Printf("free area4.\n");
113
114 DisplayHeap( DISPLAY_HEAP1 | DISPLAY_HEAP2 );
115
116
117 //---- Expand the heap
118 OS_Printf("\n================================ expand heap1 ================\n" );
119 heapArea3 = OS_AllocFromMainArenaLo(MAIN_HEAP_SIZE3, 32);
120 OS_Printf("expand area %x-%x\n", heapArea3, (u32)heapArea3 + MAIN_HEAP_SIZE3);
121
122 OS_AddToHeap(OS_ARENA_MAIN, handle1, heapArea3, (void *)((u32)heapArea3 + MAIN_HEAP_SIZE3));
123
124 DisplayHeap( DISPLAY_HEAP1 | DISPLAY_HEAP2 );
125
126
127 //---- Destroy heap
128 OS_Printf("\n================================ destroy heap2 ================\n" );
129 OS_DestroyHeap(OS_ARENA_MAIN, handle2);
130
131
132 OS_Printf("\n");
133 OS_Printf("==== Finish sample.\n");
134 OS_Terminate();
135 }
136
137 //----------------------------------------------------------------
138 // DisplayHeap
139 //
DisplayHeap(u32 heap)140 void DisplayHeap( u32 heap )
141 {
142 if ( heap & DISPLAY_HEAP1 )
143 {
144 OS_Printf( "+-------------------HEAP 1-------------------+\n" );
145 OS_Printf( "| |\n" );
146 OS_DumpHeap(OS_ARENA_MAIN, handle1);
147 OS_Printf( "| |\n" );
148 OS_Printf( "+--------------------------------------------+\n" );
149 }
150
151 if ( heap & DISPLAY_HEAP2 )
152 {
153 OS_Printf( "+-------------------HEAP 2-------------------+\n" );
154 OS_Printf( "| |\n" );
155 OS_DumpHeap(OS_ARENA_MAIN, handle2);
156 OS_Printf( "| |\n" );
157 OS_Printf( "+--------------------------------------------+\n" );
158 }
159 }
160
161 /*====== End of main.c ======*/
162