/*---------------------------------------------------------------------------* Project: MEM library File: mem_allocator.c Programmers: Makoto Takano 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 releases memory to the expansion heap Arguments: pAllocator pointer to the allocator memBlock pointer to the memory block to be released 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 releases memory to the frame heap Arguments: pAllocator pointer to the allocator memBlock pointer to the memory block to be released Returns: None *---------------------------------------------------------------------------*/ static void AllocatorFreeForFrmHeap_( MEMAllocator* pAllocator, void* memBlock ) { #pragma unused( pAllocator, memBlock ) /* In the frame heap it is not possible to free memory in memory block units. Therefore, in this implementation this will do nothing. */ } /* ------------------------------------------------------------------------ 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. returns NULL. */ MEMHeapHandle const heap = (MEMHeapHandle)pAllocator->pHeap; if ( size > MEMGetMemBlockSizeForUnitHeap(heap) ) { return NULL; } return MEMAllocFromUnitHeap(heap); } /*---------------------------------------------------------------------------* Name: AllocatorFreeForUnitHeap_ Description: function that releases memory to the unit heap Arguments: pAllocator pointer to the allocator memBlock pointer to the memory block to be released 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 releases memory to the OS heap Arguments: pAllocator pointer to the allocator memBlock pointer to the memory block to be released 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 allocation will depend on the allocator and the implementation of the memory manager that is associated with it. Arguments: pAllocator: address of the Allocator structure size : Memory block size (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: Frees a memory block and returns it to allocator. Actual return will depend on the allocator and the implementation of the memory manager that is associated with it. Arguments: pAllocator: address of the Allocator structure memBlock: Pointer to the memory block to be freed 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 free 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 free 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 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 free 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 for allocating and freeing memory from the heap created with dolphin-SDK's OSCreateHeap() function. Initializes the allocator so it can allocate and free memory. Arguments: pAllocator: address of the MEMAllocator structure id : Arena ID the arena that the heap is located in heap : 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 }