Skip to content

Instantly share code, notes, and snippets.

@jaames
Last active November 27, 2023 03:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaames/050f16be3865670ef56d697938d4d2eb to your computer and use it in GitHub Desktop.
Save jaames/050f16be3865670ef56d697938d4d2eb to your computer and use it in GitHub Desktop.
Playdate C memory management functions (and extras!) as macros
// usage example in your C entry file...
#include "pd_api.h"
#include "platform.h"
PlaydateAPI *pd = NULL;
int eventHandler(PlaydateAPI *playdate, PDSystemEvent event, uint32_t arg)
{
if (event == kEventInitLua) {
pd = playdate;
// your code here...
}
return 0;
}
#pragma once
#include "pd_api.h"
extern PlaydateAPI* pd;
// memory management
#ifndef pd_alloc
#define pd_alloc(s) pd->system->realloc(NULL, (s))
#endif
#ifndef pd_malloc
#define pd_malloc(s) pd->system->realloc(NULL, (s))
#endif
#ifndef pd_calloc
#define pd_calloc(numEls, elSize) memset(pd->system->realloc(NULL, ((numEls) * (elSize))), 0, ((numEls) * (elSize)))
#endif
#ifndef pd_realloc
#define pd_realloc pd->system->realloc
#endif
#ifndef pd_free
#define pd_free(ptr) pd->system->realloc((ptr), 0)
#endif
// loging
#ifndef pd_log
#define pd_log(s, ...) pd->system->logToConsole((s), ##__VA_ARGS__)
#endif
#ifndef pd_error
#define pd_error(s, ...) pd->system->error((s), ##__VA_ARGS__)
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment