The Socrates Gameboy Advance Development Engine
The SGADE is a development library for the Nintendo Gameboy
Advance. It's free for all uses and is distributed without guarantees.
For more information visit
the SGADE page.
Files | |
| file | SoMemManager.c |
| file | SoMemManager.h |
Defines | |
| #define | SO_MEM_MANAGER_VERIFY |
| Configuration -- If this symbol is defined, the library will verify arguments at RUN TIME. | |
| #define | SO_MEM_MANAGER_SINGLE_POOL_BASE |
| Configuration - starting address of the managed memory pool. | |
| #define | SO_MEM_MANAGER_SINGLE_POOL_SIZE |
| Configuration - the size of the managed memory pool in bytes. | |
| #define | SO_MEM_MANAGER_BLOCK_BITS |
| Configuration - the number of address bits in each allocation block (Allocation block size is equal to 1<<SO_MEM_MANAGER_BLOCK_BITS). | |
Functions | |
| void | SoMemManagerInit (void) |
| void * | SoMemManagerAlloc (u16 iSize) |
| void | SoMemManagerFree (void *pBuf) |
Variables | |
| u32 | g_u32MemPool [] |
| The single managed memory pool (must be provided by library user). | |
Singleton
This file contains an implementation of a simple general purpose memory manager suitable for use in embedded applications. It is based loosely on the GDC 2001 talk "AGB Resource Management" by Rafael Baptista. Credit goes to him for the basic algorithm used herein.
These functions were designed with the intention of balancing simplicity with speed and space efficiency. Features that would protect against improper use but which would add a significant computational cost were not included. Here is a list of known limitations: You must call free with the same pointer as was used to alloc (there is an ifdef'ed check to ensure this is done). If you do not, free may only release a portion of the originally allocated buffer. The basic allocation block size must be a power of two. Other sizes are truncated to the nearest power of two *less than* the requested size. The minimum allowable block size is 8 bytes (in order to fit the free tree node). The number of blocks in the free pool is the largest multiple of 8 that will fit within the buffer.
This is my first implementation of a memory manager. There are undoubtedly areas that could be improved. If you have suggestions for improvements -- preferably with code, or you find anything that is obviously wrong or grossly inefficient drop me a line.
Configuration
This implementation supports allocation block sizes equal to any power of two. The default (and minimum) block size is the size of the free node structure (8 bytes). To allow varying block sizes, uncomment the SO_MEM_MANAGER_BLOCK_BITS define in SoMemManager.h
This implementation supports multiple active memory pools. To force a single memory pool (and simplify the API slightly) uncomment the SO_MEM_MANAGER_SINGLE_POOL_* defines in SoMemManager.h
|
|
Configuration - the number of address bits in each allocation block (Allocation block size is equal to 1<<SO_MEM_MANAGER_BLOCK_BITS). If this symbol is not defined, then different sizes will be allowed for each free pool.
|
|
|
Configuration - starting address of the managed memory pool. If this symbol is not defined, then multiple free pools will be supported and each call to every SoMemManager function must include the pool to be used. |
|
|
Configuration - the size of the managed memory pool in bytes. If SO_MEM_MANAGER_SINGLE_POOL_BASE is defined, then this symbol must be defined as well. |
|
|
Configuration -- If this symbol is defined, the library will verify arguments at RUN TIME. This is only done for debug builds. |
|
|
The single managed memory pool (must be provided by library user). Only used if SO_MEM_MANAGER_SINGLE_POOL_BASE is defined |