Using the Region Allocation Routines

Previous examples were primarily concerned with static memory allocation; many applications may find it necessary to do some form of dynamic allocation. For situations where the allocation is always done in fixed size chunks, a family of region allocation routines are provided. These routines will carve up a larger buffer into fixed some memory regions that are managed by the library. The routines of interest are:

- osCreateRegion
  This function initializes an allocation arena given a memory address, size, and alignment.

- osMalloc
  This function allocates and returns the address to a single fixed sized and properly aligned buffer from a given region. This function will fail and return NULL is there is no available free buffer in the region.

- osFree
  This routine returns a previously allocated buffer to the given region pool.

- osGetRegionBufCount
  This function returns the total number of buffers in the region.

- osGetRegionBufSize
  This function returns the actual buffer size, after having been possibly padded to the given alignment.

The following code sample creates a region, allocates a buffer, and then frees it.

    void *region;
            char regionMemory[REGION_SIZE];
            u64 *buffer;

            region = osCreateRegion(regionMemory,
                                   sizeof(regionMemory),
                                   BUFFER_SIZE, OS_RG_ALIGN_16B);
            buffer = osMalloc(region);

            /* do some work that uses ‘buffer’ */

            osFree(region, buffer);

Incidentally, if the fixed size regions are intended to hold entire segments, the maxsize keyword of the makerom specification file may be of interest. See makerom(1P) for details.

Copyright © 1999 Nintendo of America Inc. All Rights Reserved Nintendo and N64 are registered trademarks of Nintendo Last Updated January, 1999