N64® Functions Menu

al - Audio Library Functions
gDP - DP GBI Macros
gdSP - General GBI Macros
gSP - SP GBI Macros
gt - Turbo Microcode RDP
gu - Graphics Utilities
Math - Math Functions
nuSys - NuSystem
os - N64 Operating System
sp - Sprite Library Functions
uh - Host to Target IO
64DD - N64 Disk Drive

Nintendo® Confidential

   

osMalloc
osFree

Format
#include <ultra64.h>
void *osMalloc(void *region);
void osFree(void *region, void *addr);
What These Functions Do
The osCreateRegion, osMalloc, osFree, osGetRegionBufCount, and osGetRegionBufSize functions are arbitrary region memory allocation routines.

These routines provide an easy and fast way to allocate and free fixed-size buffers from an arbitrary memory area. A region is a user-defined, physically contiguous block of memory, divided into a set of equal-sized buffers.

You can create any number of memory regions at run-time by calling osCreateRegion. You must supply a block of memory large enough for the specific buffer size, including a certain overhead for the control structure. This memory allocation scheme takes a small portion at the beginning of the input memory area to use as the region control header. The rest of the region is organized simply as a pool of equal-sized buffers. The buffer size is aligned to the number of bytes defined in the alignSize argument. Upon creation, each region is given a unique region identifier. You can use this id to reference the region. because the id is actually the address of the control header for the region, any reference leads directly to it, without any searching.

Two easy calls, osMalloc and osFree, provide fast, dynamic allocation and de-allocation of buffers. No searching is needed - osMalloc takes the buffer at the head of the region's free list, and osFree puts the buffer back at the head of the free list.

osMalloc returns a pointer to a buffer from the region that is aligned according to the byte boundary specified by the osCreateRegion function's alignSize argument. The addr argument in osFree is the pointer to a buffer previously allocated by osMalloc. The osFree routine returns this buffer to the region's free list after it performs a sanity check on the buffer address.

Diagnostics
osMalloc returns a NULL if there is no available free buffer in the region.

Notes
Currently, a region can have up to a maximum of 32,768 buffers. Upon initialization, the region's starting address is automatically aligned to the boundary specified by the alignSize argument. Its buffer size (in bytes) is rounded up to be alignSize aligned.

You need to be extremely careful that you do not create multiple regions pointing to the same memory address. This will cause unexpected behavior because this scheme does not perform any memory check during region creation.

See Also
osCreateRegion
osGetRegionBufCount
osGetRegionBufSize



Nintendo® Confidential

Warning: all information in this document is confidential and covered by a non-disclosure agreement. You are responsible for keeping this information confidential and protected. Nintendo will vigorously enforce this responsibility.


Copyright © 1998
Nintendo of America Inc. All rights reserved
Nintendo and N64 are registered trademarks of Nintendo
Last updated January 1998