Using Private Memory Pools


In addition to using private memory lists, a task can also use private memory pools. Each task gets a free memory pool when it is created. Tasks can also create additional free memory pools, which, like the pools created by the kernel, are simply lists of free memory lists. When a task creates a free memory pool containing its private memory lists, it can use a single function call to allocate or deallocate any of the types of memory in those lists. The functions for doing this are described in the following sections.

As with memory lists, tasks can only use memory pools that they own. This means that the system-wide free memory pool is off-limits to tasks, as are memory pools belonging to other tasks.

Creating a Memory Pool

To create a memory pool, you must prepare a List structure using the InitList() function:

void InitList(List *l, const char *name)
Once the List structure is initialized, you can then allocate some MemList structures using the technique described in the previous section. Every memory list you create can then be added to the memory pool by calling the AddHead() function:

void AddHead(List *l, Node *n)

Allocating Memory From a Specific Memory Pool

To allocate memory from a specific free memory pool, you use the AllocMemFromMemLists() function:

void *AllocMemFromMemLists( List *l, int32 size, uint32 memflags )
The l argument is the free memory pool from which to allocate a memory block. As noted earlier, this pool must be owned by the calling task. The size argument specifies the size of the block to allocate, in bytes.

The memflags argument specifies the type of memory to allocate, the alignment of the block with respect to the page, and so on; these flags can include MEMTYPE_ANY, MEMTYPE_VRAM, MEMTYPE_DRAM, MEMTYPE_BANKSELECT, MEMTYPE_BANK1, MEMTYPE_BANK2, MEMTYPE_DMA, MEMTYPE_CEL, MEMTYPE_AUDIO, MEMTYPE_DSP, MEMTYPE_FILL, MEMTYPE_INPAGE, MEMTYPE_SYSTEMPAGESIZE, MEMTYPE_STARTPAGE, and MEMTYPE_MYPOOL. For complete descriptions of these flags, see Allocating a Memory Block. The function returns a pointer to the block that was allocated, or NULL if there was not enough memory in the list to satisfy the request.

To free memory that you've allocated with AllocMemFromMemLists(), you can only use FreeMemToMemLists() (described in the next section). Each memory allocation function has a corresponding deallocation function; if you use another memory allocation function, you must use its corresponding deallocation function.

Freeing Memory to a Specific Memory Pool

To free a block of memory allocated with AllocMemFromMemLists(), you use the FreeMemToMemLists() function:

void FreeMemToMemLists( List *l, void *p, int32 size )
The l argument is a pointer to the memory pool from which the block was allocated. The p argument is a pointer to the block to free. The size argument is the size of the block to free, in bytes.