/*---------------------------------------------------------------------------* Project: MEM library File: main.c Programmers: Takano Makoto Copyright 2005 Nintendo. All rights reserved. These coded instructions, statements, and computer programs contain proprietary information of Nintendo of America Inc. and/or Nintendo Company Ltd., and are protected by Federal copyright law. They may not be disclosed to third parties or copied or duplicated in any form, in whole or in part, without the prior written consent of Nintendo. *---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------* Note: Unit heap usage demo 1. Using the region allocated on the stack, creates a unit heap having unit blocks of 64 bytes. Creates unit heap 2. Allocates four memory blocks from the heap. 3. Release the memory block allocated third. 4. Displays the remaining number of blocks that can be allocated. 5. Destroys the unit heap and terminates. *---------------------------------------------------------------------------*/ #include // Outputs the heap status for non-debug builds. static void ReportUnitHeap( MEMHeapHandle heap ); #define HeapBufElementNum (4096 / sizeof(u32)) static u32 sHeapBuf[ HeapBufElementNum ]; /*---------------------------------------------------------------------------* Name: SampleUnitHeap Description: A sample of the unit heap. Arguments: heapAddress: The starting address of the memory allocated to the heap. heapSize: The size of the memory allocated to the heap. Returns: None. *---------------------------------------------------------------------------*/ static void SampleUnitHeap( void* heapAddress, u32 heapSize) { void* pMBlocks[4]; // Creation of the unit heap MEMHeapHandle hUnitHeap = MEMCreateUnitHeapEx( heapAddress, heapSize, 64, // The size of the memory block 16, // The alignment 0 ); // Options // Display the number of allocatable memory blocks OSReport("[demo unit heap] free block num %d.\n", MEMCountFreeBlockForUnitHeap(hUnitHeap)); // Memory block allocation pMBlocks[0] = MEMAllocFromUnitHeap( hUnitHeap ); ASSERT( pMBlocks[0] != NULL ); pMBlocks[1] = MEMAllocFromUnitHeap( hUnitHeap ); ASSERT( pMBlocks[1] != NULL ); pMBlocks[2] = MEMAllocFromUnitHeap( hUnitHeap ); ASSERT( pMBlocks[2] != NULL ); pMBlocks[3] = MEMAllocFromUnitHeap( hUnitHeap ); ASSERT( pMBlocks[3] != NULL ); // Free the third memory block MEMFreeToUnitHeap( hUnitHeap, pMBlocks[2] ); // Display the number of allocatable memory blocks OSReport("[demo unit heap] free block num %d.\n", MEMCountFreeBlockForUnitHeap(hUnitHeap)); #ifdef _DEBUG MEMDumpHeap( hUnitHeap ); #else ReportUnitHeap( hUnitHeap ); #endif // Get rid of the unit heap (void)MEMDestroyUnitHeap( hUnitHeap ); } /*---------------------------------------------------------------------------* Name: main *---------------------------------------------------------------------------*/ void main( void ) { OSInit(); SampleUnitHeap( sHeapBuf, sizeof sHeapBuf ); while(1) {} } /* ------------------------------------------------------------------------ * This is the code for outputting the heap state as a sample for non-debug build. * * Normally, more detailed output can be derived during a debug build by using the MEMDumpHeap function. */ #ifndef _DEBUG static void ReportUnitHeap( MEMHeapHandle heap ) { u32 freeSize = MEMCountFreeBlockForUnitHeap( heap ) * MEMGetMemBlockSizeForUnitHeap( heap ); u32 totalSize = (u32)MEMGetHeapTotalUsableSize( heap ); u32 usedSize = totalSize - freeSize; OSReport("[OS Foundation Unit Heap]\n"); OSReport(" whole [%p - %p)\n", MEMGetHeapStartAddress( heap ), MEMGetHeapEndAddress( heap ) ); OSReport(" %d / %d bytes (%6.2f%%) used\n", usedSize, totalSize, 100.0 * usedSize / totalSize ); OSReport("\n"); } #endif