1.4.2.4 Memory Operations

This section describes the memory operation using the OSAL Library. The OSAL Library provides an interface to a memory allocation mechanism. The memory required for dynamic instantiation of variables is normally provided by allocating it from the heap. However the standard C library implementation of malloc and free are not considered thread safe and so OSAL specific functions must be used if MPLAB Harmony or the application requires dynamic memory during operation.

When operating without an underlying RTOS the OSAL memory allocators default to using standard malloc and free functions. However, when operating with an RTOS the calls will defer to the specific scheme used by the RTOS. This may involve multiple memory pools or it may simply involve adding a critical section around calls to malloc and free. It is left to the implementation to define the most appropriate scheme.

/* allocate a large buffer */
uint8_t* buffer;

buffer = OSAL_Malloc(8000);
if (buffer != NULL)
{
    ... manipulate the buffer
    /* free the buffer */
    OSAL_Free(buffer);
    buffer = NULL;
}