BRender Technical Reference Manual:3 Functions (Structured Reference):Memory Management
Next|Prev|Up
Dynamic Memory
BrBlockFill()
BrBlockCopy()
BrMemInquire()
BrMemAllocate()
BrMemCalloc()
BrMemStrDup()
BrMemFree()

Memory Management


Dynamic Memory

Simple Memory Services

While C compilers typically provide dynamic memory allocation services via the standard C library, to assist platform independence, BRender indirects all its requirements via an allocator data structure, which defines allocation, deallocation, and inquiry functions. By default these will use the standard C library functions (malloc and free). Note that not all development systems provide any inquiry functions, so in some cases the corresponding, default BRender function will return zero. Given the vagaries of what is meant by `available memory', the application programmer is expected to implement this function themselves if its behaviour is critical.

The following two functions are provided for efficient initialisation and copying of word aligned memory.


BrBlockFill()

Description:
Fast fill a block of memory.

Declaration:
void BrBlockFill(void* dest_ptr, int value, int dwords)

Arguments:
void * dest_ptr

A pointer to the block to be filled.

int value

Value to fill the block with.

int dwords

Size of block (number of 32-bit words).

See Also:
BrBlockCopy()
53


BrBlockCopy()

Description:
Copy a block of memory. The source and destination blocks must not overlap.

Declaration:
void BrBlockCopy(void* dest_ptr, const void* src_ptr, int dwords)

Arguments:
void * dest_ptr

Destination pointer.

const void * src_ptr

Source pointer.

int dwords

Size of block to copy (number of 32-bit words).

See Also:
BrBlockFill()
53


Memory Classes

For more precise control over its memory usage, BRender distinguishes between its various uses of dynamic memory. Memory classes also map to resource classes, given that a resource class restricts itself to a single memory class. So identifier's of each are the same.

Memory classes are defined by name and number. The following symbols define the number of each BRender memory class:

BR_MEMORY_SCRATCH

BR_MEMORY_PIXELMAP

BR_MEMORY_PIXELS

BR_MEMORY_VERTICES

BR_MEMORY_FACES

BR_MEMORY_GROUPS

BR_MEMORY_MODEL

BR_MEMORY_MATERIAL

BR_MEMORY_MATERIAL_INDEX

BR_MEMORY_ACTOR

BR_MEMORY_PREPARED_VERTICES

BR_MEMORY_PREPARED_FACES

BR_MEMORY_LIGHT

BR_MEMORY_CAMERA

BR_MEMORY_BOUNDS

BR_MEMORY_CLIP_PLANE

BR_MEMORY_STRING

BR_MEMORY_REGISTRY

BR_MEMORY_TRANSFORM

BR_MEMORY_RESOURCE_CLASS

BR_MEMORY_FILE

BR_MEMORY_ANCHOR

BR_MEMORY_POOL

BR_MEMORY_RENDER_MATERIAL

BR_MEMORY_DATAFILE

The class names are the same bar the BR_MEMORY prefix, thus BR_MEMORY_LIGHT has an identifier of "LIGHT".

The applications programmer may also indirectly request memory using classes between BR_MEMORY_APPLICATION and BR_MEMORY_MAX-1 (inclusive). BR_MEMORY_APPLICATION should be used for general heap requirements (like malloc()). Classes BR_MEMORY_APPLICATION+1 and onwards are reserved for user defined classes (see br_resource_class324 and BrResClassAdd()325).

Dynamic Memory Services

When allocating memory or making an inquiry, the memory class needs to be specified. For general purposes use BR_MEMORY_APPLICATION.


BrMemInquire()

Description:
Find the amount of memory available of a given type.

Declaration:
br_size_t BrMemInquire(br_uint_8 type)

Arguments:
br_uint_8 type

Memory type.

Result:
br_size_t

Returns memory available in bytes.


BrMemAllocate()

Description:
Allocate memory.

Declaration:
void* BrMemAllocate(br_size_t size, br_uint_8 type)

Arguments:
br_size_t size

Size of memory block to be allocated.

br_uint_8 type

Memory type.

Result:
void *

Returns a pointer to the allocated memory, or NULL is unsuccessful.


BrMemCalloc()

Description:
Allocate and clear memory.

Declaration:
void* BrMemCalloc(int nelems, br_size_t size, br_uint_8 type)

Arguments:
int nelems

Number of elements to allocate.

br_size_t size

Size of each element.

br_uint_8 type

Memory type.

Result:
void *

Returns a pointer to the allocated memory, or NULL is unsuccessful.


BrMemStrDup()

Description:
Duplicate a string.

Declaration:
char* BrMemStrDup(const char* str)

Arguments:
const char * str

A pointer to the source string.

Result:
char *

Returns a pointer to the new copy of the string.


BrMemFree()

Description:
Deallocate memory.

Declaration:
void BrMemFree(void* block)

Arguments:
void * block

A pointer to the block of memory to deallocate.


Memory Allocation Handler

BRender provides the facility for the application to specify their own memory allocation functions, through which all BRender's memory allocation is performed. A static br_allocator99 structure, containing pointers to an allocator, deallocator and inquiry function, is defined and engaged using BrAllocatorSet()100.

The application will typically need to do this for platforms that don't have the standard C library functions (malloc() etc.), and in cases where simple memory allocation is not appropriate due to limited memory space or performance.

Memory Pools

Pools are a flexible way of allocating memory from a class, whereas resource classes permit structured allocation. See br_pool299 for more information.


Generated with CERN WebMaker