Allocating Memory


The following sections explain the easy way to allocate memory: by asking the kernel to do it. To learn how tasks can allocate their own memory from private memory lists, see Using Private Memory Lists.

Allocating a Memory Block

The normal way to allocate memory is with the AllocMem() macro:

void *AllocMem( int32 s, uint32 t )
The s argument specifies the size of the memory block to allocate, in bytes. The t argument contains memory allocation flags that specify the type of memory to allocate. You can include optional flags in the t argument that specify other characteristics of the block. The macro returns a pointer to the allocated block or NULL if the block couldn't be allocated.

Memory Block Flags

In the t argument, you must include one of the following flags to specify the type of memory to allocate:

If a block of VRAM must come from a specific VRAM bank, you must include the following flag to specify that bank: You must also include one of the following two VRAM bank selection flags: The following flags are for compatibility with future hardware. You can set them in addition to the preceding flags.

You can use the following optional flags to specify alignment (where the block is in relation to page boundaries), fill (the initial value of all memory locations in the block), and other allocation characteristics:

If there is insufficient memory in a task's free memory pool to allocate the requested memory, the kernel automatically transfers the necessary pages of additional memory from the system-wide free memory pool to the task's free memory pool. The only exceptions are when there is not enough memory in both pools together to satisfy the request, or when the MEMTYPE_MYPOOL memory flag-which specifies that the memory block must be allocated only from the task's current free memory pool-is set.

Freeing a Memory Block

To free a memory block allocated with AllocMem(), use FreeMem():

void FreeMem( void *p, int32 size )
The p argument points to the memory block to free. The size argument specifies the size of the block to free, in bytes. The size you specify must always be the same as the size specified when you allocated the block.

Allocating Memory in Programs Ported From Other Platforms

If you're porting a program from another platform that uses the malloc() function from the standard C library to allocate memory, you can continue to use malloc():

void *malloc( int32 size )
The size argument specifies the size of block to allocate, in bytes. It returns a pointer to the block that was allocated, or NULL if the memory couldn't be allocated.

Because malloc() does not accept memory allocation flags, you cannot use it to specify a particular kind of memory or any other memory characteristics. If you are writing programs specifically for 3DO systems, you should use AllocMem() in place of malloc().

You must only use free() to free memory that you've allocated with malloc(). Each memory allocation function has a corresponding deallocation function; if you use a different memory allocation function, you must use its corresponding deallocation function. (See .)

Freeing Memory in Programs Ported From Other Platforms

To free a memory block you have allocated with malloc(), use free():

void free( void *p )
The p argument points to the memory block to free. The entire block is freed automatically; unlike FreeMem(), you do not need to specify the amount of memory to free.