ei_calloc()

void *ei_calloc(size_t nitems, size_t size);

Brief: Wrapper around calloc

Location: edge-impulse-sdk/porting/ei_classifier_porting.h

Blocking: Depends on user implementation

Description:
ei_calloc() is declared internally in the Edge Impulse SDK library, and the function must be defined by the user.

This function should allocate nitems * size bytes and initialize all bytes in this allocated memory to 0. It should return a pointer to the allocated memory. In bare-metal implementations, it can simply be a wrapper for calloc(). For example:

__attribute__((weak)) void *ei_calloc(size_t nitems, size_t size) {
    return calloc(nitems, size);
}

If you intend to run your impulse in a multi-threaded environment, you will need to ensure that your implementation of ei_calloc() is thread-safe. For example, if you are using FreeRTOS, here is one possible implementation:

__attribute__((weak)) void *ei_calloc(size_t nitems, size_t size) {
    void *ptr = NULL;
    if (size > 0) {
        ptr = pvPortMalloc(nitems * size);
        if(ptr)
           memset(ptr, 0, (nitems * size));
    }
    return ptr;
}

Parameters:

  • nitems - [input] Number of blocks to allocate and clear
  • size - [input] Size (in bytes) of each block

Returns:
Pointer to the allocated memory. Should return NULL if there was an error allocating memory.

Example:
The following examples demonstrate possible implementations of this function for various platforms. Note the __attribute__((weak)) in most of the definitions, which means that a user could override the implementation elsewhere in the program: