skip to main content
General Support API : Memory allocation functions
 

Memory allocation functions

Memory management functions are provided for allocating memory consistently and efficiently. Using these functions, rather than malloc() and free(), prevents memory fragmentation and simplifies memory management for the programmer. Freeing is simpler because it is not necessary to keep track of the allocated pointer. As long as the items (blocks of memory) are attached to a tree, a single call to xm_freeTree() frees them all. Using the memory allocation functions can also help to avoid memory leakage.
XM_Tree xm_allocTree(
XM_Tree * parentTree)
This function allocates a new tree. The value returned can be passed to xm_allocItem. Any items that are allocated on the same tree can easily be freed at one time. The argument is a pointer to another tree previously allocated by xm_allocTree()or NULL. If NULL is passed, the tree is a new tree. Otherwise the tree becomes a descendant of the tree passed in argument.
void xm_freeTree (XM_Tree *tree)
Free the tree given in the argument, and all of its items. All descendants of the tree are also freed.
void *xm_allocItem (XM_Tree *parentTree, size, flags)
This function is used to allocate an item of size "size" that resides on the tree parentTree.
If parentTree is NULL, the item does not reside on any tree and has to be freed individually. If the function succeeds, a pointer to a block of memory is returned. And, if the memory cannot be allocated, NULL is returned.
Note: It is recommended that you use the value:
"flags" = XM_NOFLAGS.
Optionally you can use "flags" = XM_CLEARMEM to allocate and ZERO initialize the allocated memory.
void xm_freeItem (void *item)
This function frees an item and removes it from its parent tree.
char *xm_strdup (XM_Tree *tree, char *string)
This macro is equivalent to the standard library strdup()function. It performs allocations using xm_allocItem() and attaches the new string to the tree given in the argument.