/*---------------------------------------------------------------------------* Project: MEM library File: allocator.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: a demo for using the expansion heap 1. Creates extended heap, frame heap, unit heap, and OS heap from the arena. 2. Creates allocators from these heaps, and performs a memory allocation test using the AllocateTest function. *---------------------------------------------------------------------------*/ #include #define HEAP_SIZE 4096 #define UNIT_BLOCK_SIZE 64 /*---------------------------------------------------------------------------* Name: AllocateTest Description: Allocates a memory block having the specified size from the allocator. Arguments: allocator Specifies the allocator to allocate memory. size Specifies the memory size to allocate. Returns: None. *---------------------------------------------------------------------------*/ static void AllocateTest( MEMAllocator* allocator, u32 size ) { void* addr; // Allocates memory. // If a size larger than the block size is specified for the unit heap, allocation fails, and NULL is returned. // addr = MEMAllocFromAllocator( allocator, size ); if ( addr ) { OSReport("Success: allocate %ld byte\n", size); // Release memory. // Nothing is released in the frame heap because it is not possible to free memory in memory block units. // MEMFreeToAllocator( allocator, addr ); } else { OSReport("Error: can't allocate %ld bytes\n", size ); } } /*---------------------------------------------------------------------------* Name: main *---------------------------------------------------------------------------*/ void main( void ) { u8* arenaLo; u8* arenaHi; OSInit(); arenaLo = OSGetArenaLo(); arenaHi = OSGetArenaHi(); arenaLo = OSInitAlloc( arenaLo, arenaHi, 1 ); OSSetArenaLo( arenaLo ); { // When creating the heap, the provided region is aligned internally as necessary. // Creation of the expanded heap MEMHeapHandle hExpHeap = MEMCreateExpHeap ( arenaLo, HEAP_SIZE ); // Creation of the frame heap MEMHeapHandle hFrmHeap = MEMCreateFrmHeap ( arenaLo + HEAP_SIZE, HEAP_SIZE ); // Creation of the unit heap MEMHeapHandle hUnitHeap = MEMCreateUnitHeap( arenaLo + HEAP_SIZE * 2, HEAP_SIZE, UNIT_BLOCK_SIZE ); // Creation of the OS heap OSHeapHandle hOSHeap = OSCreateHeap( arenaLo + HEAP_SIZE * 3, arenaLo + HEAP_SIZE * 4 ); // Extended heap test. { MEMAllocator allocator; MEMInitAllocatorForExpHeap( &allocator, hExpHeap, 4 ); OSReport(" Exp Heap ---------------------------\n"); AllocateTest( &allocator, 50 ); AllocateTest( &allocator, 100 ); } // Frame heap test { MEMAllocator allocator; MEMInitAllocatorForFrmHeap( &allocator, hFrmHeap, 4 ); OSReport(" Frm Heap ---------------------------\n"); AllocateTest( &allocator, 50 ); AllocateTest( &allocator, 100 ); } // Unit heap test { MEMAllocator allocator; MEMInitAllocatorForUnitHeap( &allocator, hUnitHeap ); OSReport(" Unit Heap ---------------------------\n"); AllocateTest( &allocator, 50 ); AllocateTest( &allocator, 100 ); } // OS heap test { MEMAllocator allocator; MEMInitAllocatorForOSHeap( &allocator, hOSHeap ); OSReport(" OS Heap ---------------------------\n"); AllocateTest( &allocator, 50 ); AllocateTest( &allocator, 100 ); } } while(1) {} }