Home > Support > Documentation

Memory Allocation Macros and Functions


Detailed Description

Memory allocation functions and macros handle memory management for the XBinder C run-time. Special algorithms are used for allocation and deallocation of memory to improve the run-time performance.


Defines

#define OSRTALLOCTYPE(pctxt, type)   (type*) rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type))
#define OSRTALLOCTYPEZ(pctxt, type)   (type*) rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type))
#define OSRTREALLOCARRAY(pctxt, pseqof, type)
#define OSCRTMALLOC0(nbytes)   OSCRTLMALLOC(nbytes)
#define OSCRTFREE0(ptr)   OSCRTLFREE(ptr)
#define OSCRTMALLOC   rtxMemAlloc
#define OSCRTFREE   rtxMemFreePtr
#define OSCDECL
#define rtxIntlMemAlloc(pctxt, nbytes)   rtxMemHeapAlloc(&(pctxt)->pMemHeap,nbytes)
#define rtxIntlMemAllocZ(pctxt, nbytes)   rtxMemHeapAllocZ(&(pctxt)->pMemHeap,nbytes)
#define rtxIntlMemRealloc(pctxt, mem_p, nbytes)   rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, nbytes)
#define rtxIntlMemFreePtr(pctxt, mem_p)
#define rtxIntlMemFree(pctxt)   rtxMemHeapFreeAll(&(pctxt)->pMemHeap)
#define rtxIntlMemReset(pctxt)   rtxMemHeapReset(&(pctxt)->pMemHeap)
#define rtxIntlMemAllocType(pctxt, ctype)   (ctype*)rtxMemHeapAlloc(&(pctxt)->pMemHeap,sizeof(ctype))
#define rtxIntlMemAllocTypeZ(pctxt, ctype)   (ctype*)rtxMemHeapAllocZ(&(pctxt)->pMemHeap,sizeof(ctype))
#define rtxIntlMemFreeType(pctxt, mem_p)   rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
#define rtxIntlMemAllocArray(pctxt, n, type)   (type*)rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type)*n)
#define rtxIntlMemAllocArrayZ(pctxt, n, type)   (type*)rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type)*n)
#define rtxIntlMemFreeArray(pctxt, mem_p)   rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)
#define rtxIntlMemReallocArray(pctxt, mem_p, n, type)   (type*)rtxMemHeapRealloc(&(pctxt)->pMemHeap, (void*)mem_p, sizeof(type)*n)
#define rtxMemAllocType(pctxt, ctype)   (ctype*)rtxMemAlloc(pctxt,sizeof(ctype))
#define rtxMemAllocTypeZ(pctxt, ctype)   (ctype*)rtxMemAllocZ(pctxt,sizeof(ctype))
#define rtxMemFreeType(pctxt, mem_p)   rtxMemFreePtr(pctxt,mem_p)
#define rtxMemAllocArray(pctxt, n, type)   (type*)rtxMemAlloc(pctxt,n*sizeof(type))
#define rtxMemAllocArrayZ(pctxt, n, type)   (type*)rtxMemAllocZ(pctxt,n*sizeof(type))
#define rtxMemFreeArray(pctxt, mem_p)   rtxMemFreePtr(pctxt,mem_p)
#define rtxMemReallocArray(pctxt, mem, n, type)   (type*)rtxMemRealloc(pctxt,mem,n*sizeof(type))

Typedefs

typedef void *OSCDECL * OSMallocFunc (size_t size)
typedef void *OSCDECL * OSReallocFunc (void *ptr, size_t size)

Functions

typedef void (OSCDECL *OSFreeFunc)(void *ptr)
EXTERNRTX void rtxMemHeapAddRef (void **ppvMemHeap)
EXTERNRTX void * rtxMemHeapAlloc (void **ppvMemHeap, size_t nbytes)
EXTERNRTX void * rtxMemHeapAllocZ (void **ppvMemHeap, size_t nbytes)
EXTERNRTX int rtxMemHeapCheckPtr (void **ppvMemHeap, void *mem_p)
EXTERNRTX int rtxMemHeapCreate (void **ppvMemHeap)
EXTERNRTX void rtxMemHeapFreeAll (void **ppvMemHeap)
EXTERNRTX void rtxMemHeapFreePtr (void **ppvMemHeap, void *mem_p)
EXTERNRTX void * rtxMemHeapMarkSaved (void **ppvMemHeap, const void *mem_p, OSBOOL saved)
EXTERNRTX void * rtxMemHeapRealloc (void **ppvMemHeap, void *mem_p, size_t nbytes_)
EXTERNRTX void rtxMemHeapRelease (void **ppvMemHeap)
EXTERNRTX void rtxMemHeapReset (void **ppvMemHeap)
EXTERNRTX void rtxMemHeapSetProperty (void **ppvMemHeap, OSUINT32 propId, void *pProp)
EXTERNRTX void * rtxMemNewArray (size_t nbytes)
EXTERNRTX void * rtxMemNewArrayZ (size_t nbytes)
EXTERNRTX void rtxMemDeleteArray (void *mem_p)
EXTERNRTX void rtxMemSetAllocFuncs (OSMallocFunc malloc_func, OSReallocFunc realloc_func, OSFreeFunc free_func)
EXTERNRTX void rtxMemFreeOpenSeqExt (OSCTXT *pctxt, struct OSRTDList *pElemList)
EXTERNRTX void rtxMemHeapSetFlags (OSCTXT *pctxt, OSUINT32 flags)
EXTERNRTX void rtxMemHeapClearFlags (OSCTXT *pctxt, OSUINT32 flags)
EXTERNRTX void rtxMemHeapSetDefBlkSize (OSCTXT *pctxt, OSUINT32 blkSize)
EXTERNRTX OSUINT32 rtxMemHeapGetDefBlkSize (OSCTXT *pctxt)
EXTERNRTX void rtxMemSetDefBlkSize (OSUINT32 blkSize)
EXTERNRTX OSUINT32 rtxMemGetDefBlkSize ()
EXTERNRTX OSBOOL rtxMemIsZero (const void *pmem, size_t memsiz)
EXTERNRTX void * rtxMemAlloc (OSCTXT *pctxt, size_t nbytes)
EXTERNRTX void * rtxMemAllocZ (OSCTXT *pctxt, size_t nbytes)
EXTERNRTX void rtxMemFreePtr (OSCTXT *pctxt, void *mem_p)
EXTERNRTX void * rtxMemRealloc (OSCTXT *pctxt, void *mem_p, size_t nbytes)
EXTERNRTX void rtxMemReset (OSCTXT *pctxt)
EXTERNRTX void rtxMemFree (OSCTXT *pctxt)

Define Documentation

#define OSRTALLOCTYPE ( pctxt,
type   )     (type*) rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type))

This macro allocates a single element of the given type.

Parameters:
pctxt - Pointer to a context block
type - Data type of record to allocate

#define OSRTALLOCTYPEZ ( pctxt,
type   )     (type*) rtxMemHeapAllocZ (&(pctxt)->pMemHeap, sizeof(type))

This macro allocates and zeros a single element of the given type.

Parameters:
pctxt - Pointer to a context block
type - Data type of record to allocate

#define OSRTREALLOCARRAY ( pctxt,
pseqof,
type   ) 

Value:

do {\
if (sizeof(type)*(pseqof)->n < (pseqof)->n) return RTERR_NOMEM; \
if (((pseqof)->elem = (type*) rtxMemHeapRealloc \
(&(pctxt)->pMemHeap, (pseqof)->elem, sizeof(type)*(pseqof)->n)) == 0) \
return RTERR_NOMEM; \
} while (0)

#define rtxIntlMemAllocArray ( pctxt,
n,
type   )     (type*)rtxMemHeapAlloc (&(pctxt)->pMemHeap, sizeof(type)*n)

Allocate a dynamic array. This macro allocates a dynamic array of records of the given type. The pointer to the allocated array is returned to the caller.

Parameters:
pctxt - Pointer to a context block
n - Number of records to allocate
type - Data type of an array record

#define rtxIntlMemAllocType ( pctxt,
ctype   )     (ctype*)rtxMemHeapAlloc(&(pctxt)->pMemHeap,sizeof(ctype))

Allocate type. This macro allocates memory to hold a variable of the given type.

Parameters:
pctxt - Pointer to a context block
ctype - Name of C typedef
Returns:
- Pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtxIntlMemAllocTypeZ ( pctxt,
ctype   )     (ctype*)rtxMemHeapAllocZ(&(pctxt)->pMemHeap,sizeof(ctype))

Allocate type and zero memory. This macro allocates memory to hold a variable of the given type and initializes the allocated memory to zero.

Parameters:
pctxt - Pointer to a context block
ctype - Name of C typedef
Returns:
- Pointer to allocated memory or NULL if insufficient memory was available to fulfill the request.

#define rtxIntlMemFreePtr ( pctxt,
mem_p   ) 

Value:

if (rtxMemHeapCheckPtr (&(pctxt)->pMemHeap, (void*)mem_p)) \
rtxMemHeapFreePtr(&(pctxt)->pMemHeap, (void*)mem_p)


Function Documentation

EXTERNRTX OSUINT32 rtxMemGetDefBlkSize (  ) 

This function returns the actual granularity of memory blocks.

Returns:
The currently used minimum size and the granularity of memory blocks.

EXTERNRTX OSBOOL rtxMemIsZero ( const void *  pmem,
size_t  memsiz 
)

This helper function determines if an arbitrarily sized block of memory is set to zero.

Parameters:
pmem Pointer to memory block to check
memsiz Size of the memory block
Returns:
Boolean result: true if memory is all zero

EXTERNRTX void rtxMemSetAllocFuncs ( OSMallocFunc  malloc_func,
OSReallocFunc  realloc_func,
OSFreeFunc  free_func 
)

This function sets the pointers to standard allocation functions. These functions are used to allocate/reallocate/free memory blocks. By default, standard C functions - 'malloc', 'realloc' and 'free' - are used. But if some platforms do not support these functions (or some other reasons exist) they can be overloaded. The functions being overloaded should have the same prototypes as the standard functions.

Parameters:
malloc_func Pointer to the memory allocation function ('malloc' by default).
realloc_func Pointer to the memory reallocation function ('realloc' by default).
free_func Pointer to the memory deallocation function ('free' by default).

EXTERNRTX void rtxMemSetDefBlkSize ( OSUINT32  blkSize  ) 

This function sets the minimum size and the granularity of memory blocks for newly created memory heaps.

Parameters:
blkSize The minimum size and the granularity of memory blocks.