1 /*---------------------------------------------------------------------------*
2 Project: MEM library
3 File: main.c
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 /*---------------------------------------------------------------------------*
16 Note: Unit heap usage demo
17
18 1. Using the region allocated on the stack, creates a unit heap having unit blocks of 64 bytes.
19 Creates unit heap
20 2. Allocates four memory blocks from the heap.
21 3. Release the memory block allocated third.
22 4. Displays the remaining number of blocks that can be allocated.
23 5. Destroys the unit heap and terminates.
24 *---------------------------------------------------------------------------*/
25
26 #include <revolution/mem.h>
27
28 // Outputs the heap status for non-debug builds.
29 static void ReportUnitHeap( MEMHeapHandle heap );
30
31
32 #define HeapBufElementNum (4096 / sizeof(u32))
33
34 static u32 sHeapBuf[ HeapBufElementNum ];
35
36
37 /*---------------------------------------------------------------------------*
38 Name: SampleUnitHeap
39
40 Description: A sample of the unit heap.
41
42 Arguments: heapAddress: The starting address of the memory allocated to the heap.
43 heapSize: The size of the memory allocated to the heap.
44
45 Returns: None.
46 *---------------------------------------------------------------------------*/
47 static void
SampleUnitHeap(void * heapAddress,u32 heapSize)48 SampleUnitHeap(
49 void* heapAddress,
50 u32 heapSize)
51 {
52 void* pMBlocks[4];
53
54 // Creation of the unit heap
55 MEMHeapHandle hUnitHeap = MEMCreateUnitHeapEx(
56 heapAddress,
57 heapSize,
58 64, // The size of the memory block
59 16, // The alignment
60 0 ); // Options
61
62 // Display the number of allocatable memory blocks
63 OSReport("[demo unit heap] free block num %d.\n", MEMCountFreeBlockForUnitHeap(hUnitHeap));
64
65 // Memory block allocation
66 pMBlocks[0] = MEMAllocFromUnitHeap( hUnitHeap );
67 ASSERT( pMBlocks[0] != NULL );
68 pMBlocks[1] = MEMAllocFromUnitHeap( hUnitHeap );
69 ASSERT( pMBlocks[1] != NULL );
70 pMBlocks[2] = MEMAllocFromUnitHeap( hUnitHeap );
71 ASSERT( pMBlocks[2] != NULL );
72 pMBlocks[3] = MEMAllocFromUnitHeap( hUnitHeap );
73 ASSERT( pMBlocks[3] != NULL );
74
75 // Free the third memory block
76 MEMFreeToUnitHeap( hUnitHeap, pMBlocks[2] );
77
78 // Display the number of allocatable memory blocks
79 OSReport("[demo unit heap] free block num %d.\n", MEMCountFreeBlockForUnitHeap(hUnitHeap));
80
81 #ifdef _DEBUG
82 MEMDumpHeap( hUnitHeap );
83 #else
84 ReportUnitHeap( hUnitHeap );
85 #endif
86 // Get rid of the unit heap
87 (void)MEMDestroyUnitHeap( hUnitHeap );
88 }
89
90
91 /*---------------------------------------------------------------------------*
92 Name: main
93 *---------------------------------------------------------------------------*/
94 void
main(void)95 main( void )
96 {
97 OSInit();
98
99 SampleUnitHeap( sHeapBuf, sizeof sHeapBuf );
100
101 while(1) {}
102 }
103
104
105
106
107 /* ------------------------------------------------------------------------
108 * This is the code for outputting the heap state as a sample for non-debug build.
109 *
110 * Normally, more detailed output can be derived during a debug build by using the MEMDumpHeap function.
111 */
112 #ifndef _DEBUG
113 static void
ReportUnitHeap(MEMHeapHandle heap)114 ReportUnitHeap( MEMHeapHandle heap )
115 {
116 u32 freeSize = MEMCountFreeBlockForUnitHeap( heap ) * MEMGetMemBlockSizeForUnitHeap( heap );
117 u32 totalSize = (u32)MEMGetHeapTotalUsableSize( heap );
118 u32 usedSize = totalSize - freeSize;
119 OSReport("[OS Foundation Unit Heap]\n");
120 OSReport(" whole [%p - %p)\n", MEMGetHeapStartAddress( heap ), MEMGetHeapEndAddress( heap ) );
121 OSReport(" %d / %d bytes (%6.2f%%) used\n", usedSize, totalSize, 100.0 * usedSize / totalSize );
122 OSReport("\n");
123 }
124 #endif
125
126