Here is a set of helpers I found useful while working in C, you need to initialize asap and not use until you have initialized or you will get crashes. Doing it first thing in the event handler should be fine.
pd_memory_tools.h
#ifndef PD_MEMORY_TOOLS_H
#define PD_MEMORY_TOOLS_H
#include "pd_api.h"
void initializeMemoryTools(PlaydateAPI *playdate);
static void* pd_malloc(size_t size);
static void* pd_calloc(size_t count, size_t size);
static void* pd_realloc(void* ptr, size_t size);
static void pd_free(void* ptr);
#endif
pd_memory_tools.c
#include "pd_memory_tools.h"
static void* pd_realloc(void* ptr, size_t size) {
return pd->system->realloc(ptr, size);
}
static void* pd_malloc(size_t size) {
return pd_realloc(NULL, size);
}
static void* pd_calloc(size_t count, size_t size) {
return memset(pd_malloc(count * size), 0, count * size);
}
static void pd_free(void* ptr) {
pd_realloc(ptr, 0);
}
static PlaydateAPI *pd = NULL;
void initializeMemoryTools(PlaydateAPI *playdate) {
pd = playdate;
}