/*---------------------------------------------------------------------------* Project: MEM library File: mem_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. *---------------------------------------------------------------------------*/ #include #include #include #include #include /* ======================================================================== static functions ======================================================================== */ /* ------------------------------------------------------------------------ For Exp Heap ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: AllocatorAllocForExpHeap_ Description: Function that gets memory from the expansion heap. Arguments: pAllocator: pointer to the allocator size: size of memory to be gotten Returns: A pointer to the memory region gotten from the expansion heap. *---------------------------------------------------------------------------*/ static void* AllocatorAllocForExpHeap_( MEMAllocator* pAllocator, u32 size ) { MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; int const alignment = (int)pAllocator->heapParam1; return MEMAllocFromExpHeapEx( heap, size, alignment ); } /*---------------------------------------------------------------------------* Name: AllocatorFreeForExpHeap_ Description: Function that deallocates memory to the expansion heap. Arguments: pAllocator: pointer to the allocator memBlock: pointer to the memory block to be deallocated Returns: None *---------------------------------------------------------------------------*/ static void AllocatorFreeForExpHeap_( MEMAllocator* pAllocator, void* memBlock ) { MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; MEMFreeToExpHeap( heap, memBlock ); } /* ------------------------------------------------------------------------ For Frame Heap ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: AllocatorAllocForFrmHeap_ Description: Function that gets memory from the frame heap. Arguments: pAllocator: pointer to the allocator size: size of memory to be gotten Returns: A pointer to the memory region gotten from the frame heap. *---------------------------------------------------------------------------*/ static void* AllocatorAllocForFrmHeap_( MEMAllocator* pAllocator, u32 size ) { MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; int const alignment = (int)pAllocator->heapParam1; return MEMAllocFromFrmHeapEx( heap, size, alignment ); } /*---------------------------------------------------------------------------* Name: AllocatorFreeForFrmHeap_ Description: Function that deallocates memory to the frame heap. Arguments: pAllocator: pointer to the allocator memBlock: pointer to the memory block to be deallocated Returns: None *---------------------------------------------------------------------------*/ static void AllocatorFreeForFrmHeap_( MEMAllocator* pAllocator, void* memBlock ) { #pragma unused( pAllocator, memBlock ) /* Since it is not possible in the frame heap to deallocate memory in memory block units, nothing is to occur in this implementation. */ } /* ------------------------------------------------------------------------ For Unit Heap ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: AllocatorAllocForUnitHeap_ Description: Function that gets memory from the unit heap. Arguments: pAllocator: pointer to the allocator size: size of memory to be gotten Returns: A pointer to the memory region gotten from the unit heap. *---------------------------------------------------------------------------*/ static void* AllocatorAllocForUnitHeap_( MEMAllocator* pAllocator, u32 size ) { /* Null is returned because a block larger than the unit heap's memory block size cannot be allocated. */ MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; if ( size > MEMGetMemBlockSizeForUnitHeap(heap) ) { return NULL; } return MEMAllocFromUnitHeap(heap); } /*---------------------------------------------------------------------------* Name: AllocatorFreeForUnitHeap_ Description: Function that deallocates memory to the unit heap. Arguments: pAllocator: pointer to the allocator memBlock: pointer to the memory block to be deallocated Returns: None *---------------------------------------------------------------------------*/ static void AllocatorFreeForUnitHeap_( MEMAllocator* pAllocator, void* memBlock ) { MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; MEMFreeToUnitHeap( heap, memBlock ); } /* ------------------------------------------------------------------------ for the OS heap ------------------------------------------------------------------------ */ /*---------------------------------------------------------------------------* Name: AllocatorAllocForOSHeap_ Description: Function that gets memory from the OS heap. Arguments: pAllocator: pointer to the allocator size: size of memory to be gotten Returns: A pointer to the memory region gotten from the OS heap. *---------------------------------------------------------------------------*/ static void* AllocatorAllocForOSHeap_( MEMAllocator* pAllocator, u32 size ) { OSHeapHandle const heap = (int)pAllocator->pHeap; return OSAllocFromHeap( heap, size ); } /*---------------------------------------------------------------------------* Name: AllocatorFreeForOSHeap_ Description: Function that deallocates memory to the OS heap. Arguments: pAllocator: pointer to the allocator memBlock: pointer to the memory block to be deallocated Returns: None *---------------------------------------------------------------------------*/ static void AllocatorFreeForOSHeap_( MEMAllocator* pAllocator, void* memBlock ) { OSHeapHandle const heap = (int)pAllocator->pHeap; OSFreeToHeap( heap, memBlock ); } /* ======================================================================== External functions ======================================================================== */ /*---------------------------------------------------------------------------* Name: MEMAllocFromAllocator Description: Allocates memory blocks from the allocator. Actual memory allocation will depend on the allocator and the implementation of the associated memory manager. Arguments: pAllocator: address of the Allocator structure size: memory block size (in bytes) Returns: If memory block allocation succeeds, returns the beginning address of the block. Returns NULL if allocation fails. *---------------------------------------------------------------------------*/ void* MEMAllocFromAllocator( MEMAllocator* pAllocator, u32 size ) { ASSERT(pAllocator); return (*pAllocator->pFunc->pfAlloc)(pAllocator, size); } /*---------------------------------------------------------------------------* Name: MEMFreeToAllocator Description: Deallocates a memory block and returns it to allocator. How the memory is actually returned will depend on the allocator and the implementation of the associated memory manager. Arguments: pAllocator: address of the Allocator structure memBlock: pointer to the memory block to be deallocated Returns: None. *---------------------------------------------------------------------------*/ void MEMFreeToAllocator( MEMAllocator* pAllocator, void* memBlock ) { ASSERT(pAllocator && memBlock); (*pAllocator->pFunc->pfFree)(pAllocator, memBlock); } /*---------------------------------------------------------------------------* Name: MEMInitAllocatorForExpHeap Description: Initializes the allocator so it can allocate and deallocate memory from the extended heap. The alignment argument specifies alignment values for all memory blocks that are allocated via the allocator. Arguments: pAllocator: address of the MEMAllocator structure heap: Handle for expanded heap alignment: alignment value to apply to each memory block that is allocated Returns: None. *---------------------------------------------------------------------------*/ void MEMInitAllocatorForExpHeap( MEMAllocator* pAllocator, MEMHeapHandle heap, int alignment ) { static const MEMAllocatorFunc sAllocatorFunc = { AllocatorAllocForExpHeap_, AllocatorFreeForExpHeap_, }; pAllocator->pFunc = &sAllocatorFunc; pAllocator->pHeap = heap; pAllocator->heapParam1 = (u32)alignment; pAllocator->heapParam2 = 0; // no use } /*---------------------------------------------------------------------------* Name: MEMInitAllocatorForFrmHeap Description: Initializes the allocator so it can allocate and deallocate memory from the frame heap. The alignment argument specifies alignment values for all memory blocks that are allocated via the allocator. Arguments: pAllocator: address of the MEMAllocator structure heap: Handle for the frame heap alignment: alignment value to apply to each memory block that is allocated Returns: None. *---------------------------------------------------------------------------*/ void MEMInitAllocatorForFrmHeap( MEMAllocator* pAllocator, MEMHeapHandle heap, int alignment ) { static const MEMAllocatorFunc sAllocatorFunc = { AllocatorAllocForFrmHeap_, AllocatorFreeForFrmHeap_, }; pAllocator->pFunc = &sAllocatorFunc; pAllocator->pHeap = heap; pAllocator->heapParam1 = (u32)alignment; pAllocator->heapParam2 = 0; // no use } /*---------------------------------------------------------------------------* Name: MEMInitAllocatorForUnitHeap Description: Initializes the allocator so it can allocate and deallocate memory from the unit heap. It is not possible to allocated a memory block that is larger than the unit heap memory block size. In such a case, the AllocFromAllocator() function will return NULL. Arguments: pAllocator: address of the MEMAllocator structure heap: unit heap handle Returns: None. *---------------------------------------------------------------------------*/ void MEMInitAllocatorForUnitHeap( MEMAllocator* pAllocator, MEMHeapHandle heap ) { static const MEMAllocatorFunc sAllocatorFunc = { AllocatorAllocForUnitHeap_, AllocatorFreeForUnitHeap_, }; pAllocator->pFunc = &sAllocatorFunc; pAllocator->pHeap = heap; pAllocator->heapParam1 = 0; // no use pAllocator->heapParam2 = 0; // no use } /*---------------------------------------------------------------------------* Name: MEMInitAllocatorForOSHeap Description: Initializes the allocator to perform allocation and deallocation of memory from the heap that is created using the dolphin-SDK's OSCreateHeap() function. Arguments: pAllocator: address of the MEMAllocator structure id: arena ID the arena that the heap is located in heap: the heap handle Returns: None. *---------------------------------------------------------------------------*/ void MEMInitAllocatorForOSHeap( MEMAllocator* pAllocator, OSHeapHandle heap ) { static const MEMAllocatorFunc sAllocatorFunc = { AllocatorAllocForOSHeap_, AllocatorFreeForOSHeap_, }; pAllocator->pFunc = &sAllocatorFunc; pAllocator->pHeap = (void*)heap; pAllocator->heapParam1 = 0; // no use pAllocator->heapParam2 = 0; // no use }