Last active
November 27, 2023 03:29
-
-
Save jaames/050f16be3865670ef56d697938d4d2eb to your computer and use it in GitHub Desktop.
Playdate C memory management functions (and extras!) as macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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