SameBoy | Accurate GB/GBC emulator |
| download: https://git.y1.nz/archives/sameboy.tar.gz | |
| README | Files | Log | Refs | LICENSE |
commit 9fcac76a034c7df2a6efece7d80d40bceb37ad84 parent 58750424c7564659ba60a9840c1f12839293c1e6 Author: Lior Halphon <LIJI32@gmail.com> Date: Sat, 20 Aug 2022 21:45:14 +0300 New memory management APIs Diffstat:
| M | Core/gb.c | 25 | +++++++++++++++++++++++-- |
| M | Core/gb.h | 27 | +++++++++++++++++++++++++-- |
2 files changed, 48 insertions(+), 4 deletions(-)
diff --git a/Core/gb.c b/Core/gb.c @@ -146,7 +146,19 @@ static void load_default_border(GB_gameboy_t *gb) } } -void GB_init(GB_gameboy_t *gb, GB_model_t model) +size_t GB_allocation_size(void) +{ + return sizeof(GB_gameboy_t); +} + +GB_gameboy_t *GB_alloc(void) +{ + GB_gameboy_t *ret = malloc(sizeof(*ret)); + ret->magic = 0; + return ret; +} + +GB_gameboy_t *GB_init(GB_gameboy_t *gb, GB_model_t model) { memset(gb, 0, sizeof(*gb)); gb->model = model; @@ -173,6 +185,7 @@ void GB_init(GB_gameboy_t *gb, GB_model_t model) GB_reset(gb); load_default_border(gb); + return gb; } GB_model_t GB_get_model(GB_gameboy_t *gb) @@ -217,7 +230,15 @@ void GB_free(GB_gameboy_t *gb) } #endif GB_stop_audio_recording(gb); - memset(gb, 0, sizeof(*gb)); + memset(gb, 0, sizeof(*gb)); +} + +void GB_dealloc(GB_gameboy_t *gb) +{ + if (GB_is_inited(gb)) { + GB_free(gb); + } + free(gb); } int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) diff --git a/Core/gb.h b/Core/gb.h @@ -834,14 +834,37 @@ struct GB_gameboy_s { __attribute__((__format__ (__printf__, fmtarg, firstvararg))) #endif -void GB_init(GB_gameboy_t *gb, GB_model_t model); +/* + There are two instance allocation styles – one where you manage your + own instance allocation, and one where you use provided allocators. + + Managing allocations yourself: + GB_gameboy_t gb; + GB_init(&gb, model); + ... + GB_free(&gb); + + Using the provided allocators: + GB_gameboy_t *gb = GB_init(GB_alloc(), model); + ... + GB_free(gb); // optional + GB_dealloc(gb); + +*/ +GB_gameboy_t *GB_init(GB_gameboy_t *gb, GB_model_t model); +void GB_free(GB_gameboy_t *gb); +GB_gameboy_t *GB_alloc(void); +void GB_dealloc(GB_gameboy_t *gb); + +// For when you want to use your own malloc implementation without having to rely on the header struct +size_t GB_allocation_size(void); + bool GB_is_inited(GB_gameboy_t *gb); bool GB_is_cgb(const GB_gameboy_t *gb); bool GB_is_cgb_in_cgb_mode(GB_gameboy_t *gb); bool GB_is_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2 bool GB_is_hle_sgb(GB_gameboy_t *gb); // Returns true if the model is SGB or SGB2 and the SFC/SNES side is HLE'd GB_model_t GB_get_model(GB_gameboy_t *gb); -void GB_free(GB_gameboy_t *gb); void GB_reset(GB_gameboy_t *gb); void GB_switch_model_and_reset(GB_gameboy_t *gb, GB_model_t model);
This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.