From d2ae33680e5e58ec3ee9ef80eb9dfab5e03d513a Mon Sep 17 00:00:00 2001 From: Maschell Date: Tue, 14 Apr 2026 17:24:15 +0200 Subject: [PATCH] Use non-newlib function to allocate memory for reent structs to make things simpler --- include/wups/reent_internal.h | 16 ++-------- libraries/libwups/wups_reent.cpp | 54 +++++++++++--------------------- 2 files changed, 22 insertions(+), 48 deletions(-) diff --git a/include/wups/reent_internal.h b/include/wups/reent_internal.h index 8535b1f..d59b97e 100644 --- a/include/wups/reent_internal.h +++ b/include/wups/reent_internal.h @@ -5,26 +5,16 @@ extern "C" { #endif -typedef enum wups_loader_init_reent_errors_t_ { - WUPSReent_ERROR_NONE = 0, - WUPSReent_ERROR_GLOBAL_REENT_REQUESTED = 1, - WUPSReent_ERROR_NO_THREAD = 2, -} wups_loader_init_reent_errors_t_; - -typedef void *(*WUPSReent_GetReentContext)(const void *pluginId, wups_loader_init_reent_errors_t_ *outError); -typedef void *(*WUPSReent_SetSentinel)(); -typedef void (*WUPSReent_RestoreHead)(void *oldHead); -typedef bool (*WUPSReent_AddReentContext)(const void *pluginId, void *reentPtr, void (*cleanupFn)(void *), void *oldHead); +typedef bool (*WUPSReent_GetReentContext)(const void *pluginId, void **outPtr); +typedef bool (*WUPSReent_AddReentContext)(const void *pluginId, void *reentPtr, void (*cleanupFn)(void *)); typedef uint32_t WUPS_REENT_API_VERSION; -#define WUPS_REENT_CUR_API_VERSION 0x01 +#define WUPS_REENT_CUR_API_VERSION 0x02 typedef struct wups_loader_init_reent_args_t_ { WUPS_REENT_API_VERSION version; WUPSReent_GetReentContext get_context_ptr; - WUPSReent_SetSentinel set_sentinel_ptr; - WUPSReent_RestoreHead restore_head_ptr; WUPSReent_AddReentContext add_reent_context_ptr; } wups_loader_init_reent_args_t_; diff --git a/libraries/libwups/wups_reent.cpp b/libraries/libwups/wups_reent.cpp index cca6eff..40e23d0 100644 --- a/libraries/libwups/wups_reent.cpp +++ b/libraries/libwups/wups_reent.cpp @@ -4,13 +4,11 @@ #include "wups/reent_internal.h" #include "wups/wups_debug.h" #include +#include #include -#include struct wups_loader_init_reent_args_t { WUPSReent_GetReentContext get_context_ptr = nullptr; - WUPSReent_SetSentinel set_sentinel_ptr = nullptr; - WUPSReent_RestoreHead restore_head_ptr = nullptr; WUPSReent_AddReentContext add_reent_context_ptr = nullptr; }; @@ -24,28 +22,18 @@ void WUPSReentAPI_InitInternal(wups_loader_init_reent_args_t_ args) { WUPS_DEBUG_REPORT("WUPSReentAPI_InitInternal: Initializing reent plugin\n"); __internal_functions.get_context_ptr = args.get_context_ptr; - __internal_functions.set_sentinel_ptr = args.set_sentinel_ptr; __internal_functions.add_reent_context_ptr = args.add_reent_context_ptr; - __internal_functions.restore_head_ptr = args.restore_head_ptr; } // use variable in the .data section as unique plugin id static const int sReentPluginId = 0; -void *wups_backend_get_context(const void *id, wups_loader_init_reent_errors_t_ *outError) { - return __internal_functions.get_context_ptr(id, outError); +bool wups_backend_get_context(const void *id, void **outPtr) { + return __internal_functions.get_context_ptr(id, outPtr); } -void *wups_backend_set_sentinel() { - return __internal_functions.set_sentinel_ptr(); -} - -void wups_backend_restore_head(void *head) { - __internal_functions.restore_head_ptr(head); -} - -bool wups_backend_register_context(const void *pluginId, void *reentPtr, void (*cleanupFn)(void *), void *oldHead) { - return __internal_functions.add_reent_context_ptr(pluginId, reentPtr, cleanupFn, oldHead); +bool wups_backend_register_context(const void *pluginId, void *reentPtr, void (*cleanupFn)(void *)) { + return __internal_functions.add_reent_context_ptr(pluginId, reentPtr, cleanupFn); } static void reclaim_reent_trampoline(void *payload) { @@ -54,7 +42,8 @@ static void reclaim_reent_trampoline(void *payload) { if (payload) { auto *reentPtr = static_cast<_reent *>(payload); _reclaim_reent(reentPtr); - free(reentPtr); + // Make sure to use MEMFreeToDefaultHeap + MEMFreeToDefaultHeap(reentPtr); } } @@ -62,22 +51,18 @@ struct _reent *__wups_getreent() { if (!OSGetCurrentThread()) { return _GLOBAL_REENT; } - wups_loader_init_reent_errors_t_ error = WUPSReent_ERROR_NONE; - if (void *existingCtx = wups_backend_get_context(&sReentPluginId, &error)) { + void *existingCtx = nullptr; + if (!wups_backend_get_context(&sReentPluginId, &existingCtx)) { + return _GLOBAL_REENT; + } + // if non-null we can use it + if (existingCtx) { return static_cast<_reent *>(existingCtx); } - switch (error) { - case WUPSReent_ERROR_GLOBAL_REENT_REQUESTED: - case WUPSReent_ERROR_NO_THREAD: - return _GLOBAL_REENT; - case WUPSReent_ERROR_NONE: - break; - } - auto *oldHead = wups_backend_set_sentinel(); - auto *newReent = static_cast(malloc(sizeof(struct _reent))); + // Use `MEMAllocFromDefaultHeap` to avoid creating a new reent for allocating the reent + auto *newReent = static_cast(MEMAllocFromDefaultHeap(sizeof(struct _reent))); if (!newReent) { - wups_backend_restore_head(oldHead); return _GLOBAL_REENT; } @@ -85,17 +70,16 @@ struct _reent *__wups_getreent() { _REENT_INIT_PTR(newReent); - bool result = wups_backend_register_context( + const bool result = wups_backend_register_context( &sReentPluginId, newReent, - reclaim_reent_trampoline, - oldHead); + reclaim_reent_trampoline); if (!result) { WUPS_DEBUG_WARN("Failed to register context for thread %p\n", OSGetCurrentThread()); _reclaim_reent(newReent); - free(newReent); - wups_backend_restore_head(oldHead); + // Make sure to call the free function + MEMFreeToDefaultHeap(newReent); return _GLOBAL_REENT; }