SDL_SetMemoryFunctions(3)

πŸ’– SDL_SetMemoryFunctions πŸ’–

okay bestie, so this is a page from the SDL3 FUNCTIONS manual β€” yes, that SDL, the Simple Directmedia Layer one β€” and it's all about SDL_SetMemoryFunctions, the function that lets you swap out SDL's whole memory-allocation situation for your own custom set. glow-up for your allocator, basically. ✨


🌸 NAME 🌸

SDL_SetMemoryFunctions β€” replace SDL's memory allocation functions with a custom set. like, out with the old, in with your little guys. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

#include <SDL3/SDL_stdinc.h>

bool SDL_SetMemoryFunctions(SDL_malloc_func malloc_func,
                                SDL_calloc_func calloc_func,
                                SDL_realloc_func realloc_func,
                                SDL_free_func free_func);

✨ DESCRIPTION ✨

okay so here's the tea, and it is serious tea: it is not safe to call this function once any allocations have been made, because future calls to SDL_free will use the new allocator β€” even if they came from an SDL_malloc made with the old one! 😭 that's a mismatch, that's a disaster, that's the kind of thing that makes a program simply evaporate.

no cap: allocate first, swap later, and you get frees going to the wrong allocator. do not do this. πŸ™ˆ

so if you're gonna use it, usually this needs to be the first call made into the SDL library β€” if not the very first thing done at program startup time. main character energy for the very first line of your main, fr fr. πŸ‘‘


πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • malloc_func β€” your custom malloc function. the one that goes and gets the memory. πŸ“
  • calloc_func β€” your custom calloc function. same vibe, but zeroed out. ✨
  • realloc_func β€” your custom realloc function. for when the memory needs to ✨change size✨. πŸ¦‹
  • free_func β€” your custom free function. the one that lets it all go. πŸ’œ

🌈 RETURN VALUE 🌈

Returns true on success or false on failure. and if it fails? call SDL_GetError() for more information β€” she will tell you exactly what went wrong, iykyk. πŸ’…


πŸ”₯ THREAD SAFETY πŸ”₯

It is safe to call this function from any thread β€” slay, versatile queen β€” but one should not replace the memory functions once any allocations are made! so yes she's thread-safe, and no you still can't just swap her out mid-flight. both things are true, periodt. ⭐


⭐ AVAILABILITY ⭐

This function is available since SDL 3.2.0. so if you're on anything older, bestie, it's simply not giving. 🧚


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page, bestie!! this absolute icon of a manpage comes to you from Simple Directmedia Layer, version SDL 3.4.18, and yes it really is titled SDL_SetMemoryFunctions(3) β€” go forth, set your memory functions first thing, and stay safe out there πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…πŸ«Ά

πŸ“¦ where this page comes from