emscripten: Make sure SDL_malloc and friends are marked KEEPALIVE.

Reference PR #9937.
This commit is contained in:
Ryan C. Gordon
2024-08-19 23:49:07 -04:00
parent e75175129f
commit 423d6ec15a
3 changed files with 16 additions and 5 deletions

View File

@@ -5196,6 +5196,17 @@ static void SDLCALL real_free(void *p) { free(p); }
#define real_free dlfree
#endif
// mark the allocator entry points as KEEPALIVE so we can call these from JavaScript.
// otherwise they could could get so aggressively inlined that their symbols
// don't exist at all in the final binary!
#ifdef SDL_PLATFORM_EMSCRIPTEN
#include <emscripten/emscripten.h>
extern SDL_DECLSPEC SDL_MALLOC EMSCRIPTEN_KEEPALIVE void * SDLCALL SDL_malloc(size_t size);
extern SDL_DECLSPEC SDL_MALLOC SDL_ALLOC_SIZE2(1, 2) EMSCRIPTEN_KEEPALIVE void * SDLCALL SDL_calloc(size_t nmemb, size_t size);
extern SDL_DECLSPEC SDL_ALLOC_SIZE(2) EMSCRIPTEN_KEEPALIVE void * SDLCALL SDL_realloc(void *mem, size_t size);
extern SDL_DECLSPEC EMSCRIPTEN_KEEPALIVE void SDLCALL SDL_free(void *mem);
#endif
/* Memory functions used by SDL that can be replaced by the application */
static struct
{

View File

@@ -740,7 +740,7 @@ static void Emscripten_set_pointer_event_callbacks(SDL_WindowData *data)
var makePointerEventCStruct = function(event) {
var ptr = 0;
if (event.pointerType == "pen") {
ptr = _malloc($2);
ptr = _SDL_malloc($2);
if (ptr != 0) {
var rect = target.getBoundingClientRect();
var idx = ptr >> 2;
@@ -762,18 +762,18 @@ static void Emscripten_set_pointer_event_callbacks(SDL_WindowData *data)
};
SDL3.eventHandlerPointerEnter = function(event) {
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerEnter(data, d); _free(d); }
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerEnter(data, d); _SDL_free(d); }
};
target.addEventListener("pointerenter", SDL3.eventHandlerPointerEnter);
SDL3.eventHandlerPointerLeave = function(event) {
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerLeave(data, d); _free(d); }
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerLeave(data, d); _SDL_free(d); }
};
target.addEventListener("pointerleave", SDL3.eventHandlerPointerLeave);
target.addEventListener("pointercancel", SDL3.eventHandlerPointerLeave); /* catch this, just in case. */
SDL3.eventHandlerPointerGeneric = function(event) {
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerGeneric(data, d); _free(d); }
var d = makePointerEventCStruct(event); if (d != 0) { _Emscripten_HandlePointerGeneric(data, d); _SDL_free(d); }
};
target.addEventListener("pointerdown", SDL3.eventHandlerPointerGeneric);
target.addEventListener("pointerup", SDL3.eventHandlerPointerGeneric);

View File

@@ -104,7 +104,7 @@ static SDL_Cursor *Emscripten_CreateCursor(SDL_Surface *surface, int hot_x, int
? "url(" + canvas.toDataURL() + "), auto"
: "url(" + canvas.toDataURL() + ") " + hot_x + " " + hot_y + ", auto";
var urlBuf = _malloc(url.length + 1);
var urlBuf = _SDL_malloc(url.length + 1);
stringToUTF8(url, urlBuf, url.length + 1);
return urlBuf;