Skip to content

Instantly share code, notes, and snippets.

@cibomahto
Created August 19, 2021 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cibomahto/a29b6662847e13c61b47a194fad4ab11 to your computer and use it in GitHub Desktop.
Save cibomahto/a29b6662847e13c61b47a194fad4ab11 to your computer and use it in GitHub Desktop.
cJSON SPIRAM allocator
//! @brief For cjson: Memory allocator to use SPIRAM
//!
//! @param[in] sz Size of the memory to allocate
//! @return Pointer to the allocated memory if successful, or NULL if not successful
void* cjson_caps_malloc(size_t sz)
{
return heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
}
//! @brief For cjson: Memory allocator to use SPIRAM
//!
//! @param[in] ptr Pointer to the allocated memory that is to be freed
void CJSON_CDECL cjson_caps_free(void* ptr)
{
heap_caps_free(ptr);
}
//! @brief Install function handlers so that cJSON will use SPIRAM
static void cjson_mem_init()
{
// Configure cJSON to use SPIRAM
cJSON_Hooks cjson_hooks = {
.malloc_fn = &cjson_caps_malloc,
.free_fn = &cjson_caps_free,
};
cJSON_InitHooks(&cjson_hooks);
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment