Memory management functions handle the allocation and deallocation of dynamic memory. These functions form an abstraction layer above the standard C memory management functions malloc, free, and realloc. This block of functions can be replaced by the user with custom code to implement a different memory management scheme. For example, an embedded system application might want to use a fixed-sized static block from which to allocate.
The built-in memory management logic implements a nibble-allocation memory management algorithm that provides superior performance to calling malloc and free directly. This algorithm causes memory blocks to be allocated up front in larger sizes and then subsequently split up when future allocation requests are received. These blocks can be reset and reused in applications that are constantly allocating and freeing memory (for example, a decoder that constantly reads and decodes XML messages in a long running loop).
The key memory management functions that a user might use are the following:
rtxMemAlloc - This function allocates a block of memory in much the same way malloc would. The only difference from the user’s perspective is that a pointer to a context structure is required as an argument. The allocated memory is tracked within this context.
rtxMemFreePtr - This function releases the memory held by a pointer in much the same way the C free function would. The only difference from a user’s perspective is that a pointer to a context structure is required as an argument. This context must have been used in the call to rtxMemAlloc at the time the memory was allocated.
rtxMemFree - This function releases all memory held within a context.
rtxMemReset - This functions resets all memory held within a context. The difference between this and the rtxMemFree function is that this function does not actually free the blocks that were previously allocated. It only resets the pointers and indexes within those blocks to allow the memory to be reused.
rtxMemRealloc - This function works in the same way as the C realloc function. It reallocates an existing block of memory. As in the other cases above, a pointer to a context structure is a required argument.
Note that these memory management functions are only used in the generation of C code, not C++ (although a user can use them in a C++ application). For C++, the built-in new and delete operators are used to ensure constructors and destructors are properly executed.
For a full description of these and other memory management functions, see the XBinder C/C++ Runtime Reference Manual.