Getting Memory From the System-Wide Free Memory Pool


When there is insufficient memory in a task's free memory pool to allocate a block of memory, the kernel automatically provides additional memory pages from the system-wide free memory pool. Tasks can also get pages directly from the system-wide free memory pool by calling the AllocMemBlocks() function:

void *AllocMemBlocks( int32 size, uint32 typebits )
The size argument specifies the amount of memory to transfer, in bytes. If the specified size is not a multiple of the page size for the type of memory requested, the kernel transfers enough full pages to satisfy the request.

The first 4 bytes of the allocated block contain the actual number of bytes allocated. This isn't true when MEMTYPE_FILL is supplied.

The typebits argument specifies the type of memory to transfer. This argument can contain any of the following memory allocation flags: MEMTYPE_ANY, MEMTYPE_VRAM, MEMTYPE_DRAM, MEMTYPE_BANKSELECT, MEMTYPE_BANK1, MEMTYPE_BANK2, MEMTYPE_DMA, MEMTYPE_CEL, MEMTYPE_AUDIO, and MEMTYPE_DSP. For descriptions of these flags, see . You must always set the MEMTYPE_TASKMEM flag when using this function.

AllocMemBlocks() returns a pointer to the memory that was transferred. When you get the memory, you must call a function to free it, such as FreeMemToMemList() to move it to a specific memory list or FreeMemToMemLists() to move it to a specific memory pool. For either function, use the pointer returned by AllocMemBlocks() as the value of the p argument. Use the size value that is stored in the first 4 bytes in the memory returned by the AllocMemBlocks() for the size argument.