iostream: Add optional free_func pointer property to memory streams

Fixes https://github.com/libsdl-org/SDL/issues/13368

Signed-off-by: Marcin Serwin <marcin@serwin.dev>
This commit is contained in:
Marcin Serwin
2025-07-16 20:21:41 +02:00
committed by Sam Lantinga
parent ee6d8f78f4
commit 8451ce86c1
3 changed files with 76 additions and 5 deletions

View File

@@ -716,6 +716,7 @@ typedef struct IOStreamMemData
Uint8 *base;
Uint8 *here;
Uint8 *stop;
SDL_PropertiesID props;
} IOStreamMemData;
static Sint64 SDLCALL mem_size(void *userdata)
@@ -779,6 +780,11 @@ static size_t SDLCALL mem_write(void *userdata, const void *ptr, size_t size, SD
static bool SDLCALL mem_close(void *userdata)
{
IOStreamMemData *iodata = (IOStreamMemData *) userdata;
SDL_free_func free_func = SDL_GetPointerProperty(iodata->props, SDL_PROP_IOSTREAM_MEMORY_FREE_FUNC, NULL);
if (free_func) {
free_func(iodata->base);
}
SDL_free(userdata);
return true;
}
@@ -950,6 +956,7 @@ SDL_IOStream *SDL_IOFromMem(void *mem, size_t size)
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
iodata->props = props;
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}
@@ -990,6 +997,7 @@ SDL_IOStream *SDL_IOFromConstMem(const void *mem, size_t size)
} else {
const SDL_PropertiesID props = SDL_GetIOProperties(iostr);
if (props) {
iodata->props = props;
SDL_SetPointerProperty(props, SDL_PROP_IOSTREAM_MEMORY_POINTER, (void *)mem);
SDL_SetNumberProperty(props, SDL_PROP_IOSTREAM_MEMORY_SIZE_NUMBER, size);
}