Are dynamic
memory allocation and the freeing of memory inefficient on your small device?
The OSS’ Memory Handle
API can provide you
with more efficient allocation and freeing of memory. You can create a
"memory handle" which is a linked list of large memory blocks, and
allow the memory allocation function to allocate small segments of these memory
blocks as needed, rather than inefficiently calling dynamic memory allocation
functions (such as malloc() and free()) each time you need to allocate or
release memory. This is especially useful when the message you are
decoding contains a large number of output memory fragments. Rather than
calling malloc() for each fragment, each decoder request for memory will be
satisfied by a small segment of memory from the large blocks in the
"memory handle", thus avoiding costly calls to malloc() for dynamic
memory allocation.
When it
is time to free the memory allocated for the complex structure that you
decoded, you can either "release" those pieces of memory individually
so that they can be reused by future requests for memory, or you can release or
free all of blocks of memory for a particular "memory handle" in a
single function call, thereby not needing to traverse the complex message
structure to make sure each individual memory fragment is released. This
means that when you have several decode operations, you can simply release all
memory between decode operations rather than freeing it, thus being able to
reuse this memory for your next decode.
Using OSS’ Memory Handle
API, you can also create different "pools" of memory which
can be used for different purposes. A simple function call can set which
memory pool is currently active. This can be particularly useful in a
multithreaded environment where you may have a different memory pool for
messages which will only be handled by the current thread vs. a memory pool for
messages which could be passed to other threads.
If you
need further control over memory allocation and freeing, you can even replace
the default system functions (malloc and free) with your own versions of these
functions.
If
your platform does not allow dynamic memory allocation, you can still use OSS’
Memory Handle API to use static memory as the "pool" from which
fragments of memory are given to the encoder or decoder.
No comments:
Post a Comment