mirror of
https://github.com/wiiu-env/WiiUModuleSystem.git
synced 2026-05-25 11:46:49 -05:00
Use non-newlib function to allocate memory for reent structs to make things simpler
Some checks failed
Publish Docker Image / build-and-push-image (push) Has been cancelled
Some checks failed
Publish Docker Image / build-and-push-image (push) Has been cancelled
This commit is contained in:
parent
ae8bf4a995
commit
cd66e5fc96
|
|
@ -5,26 +5,16 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef enum wums_loader_init_reent_errors_t_ {
|
typedef bool (*WUMSReent_GetReentContext)(const void *moduleId, void **outPtr);
|
||||||
WUMSReent_ERROR_NONE = 0,
|
typedef bool (*WUMSReent_AddReentContext)(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *));
|
||||||
WUMSReent_ERROR_GLOBAL_REENT_REQUESTED = 1,
|
|
||||||
WUMSReent_ERROR_NO_THREAD = 2,
|
|
||||||
} wums_loader_init_reent_errors_t_;
|
|
||||||
|
|
||||||
typedef void *(*WUMSReent_GetReentContext)(const void *moduleId, wums_loader_init_reent_errors_t_ *outError);
|
|
||||||
typedef void *(*WUMSReent_SetSentinel)();
|
|
||||||
typedef void (*WUMSReent_RestoreHead)(void *oldHead);
|
|
||||||
typedef bool (*WUMSReent_AddReentContext)(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *), void *oldHead);
|
|
||||||
|
|
||||||
typedef uint32_t WUMS_REENT_API_VERSION;
|
typedef uint32_t WUMS_REENT_API_VERSION;
|
||||||
|
|
||||||
#define WUMS_REENT_CUR_API_VERSION 0x01
|
#define WUMS_REENT_CUR_API_VERSION 0x02
|
||||||
|
|
||||||
typedef struct wums_loader_init_reent_args_t_ {
|
typedef struct wums_loader_init_reent_args_t_ {
|
||||||
WUMS_REENT_API_VERSION version;
|
WUMS_REENT_API_VERSION version;
|
||||||
WUMSReent_GetReentContext get_context_ptr;
|
WUMSReent_GetReentContext get_context_ptr;
|
||||||
WUMSReent_SetSentinel set_sentinel_ptr;
|
|
||||||
WUMSReent_RestoreHead restore_head_ptr;
|
|
||||||
WUMSReent_AddReentContext add_reent_context_ptr;
|
WUMSReent_AddReentContext add_reent_context_ptr;
|
||||||
} wums_loader_init_reent_args_t_;
|
} wums_loader_init_reent_args_t_;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
extern "C" void OSFatal(const char *);
|
|
||||||
extern "C" void OSFatal(const char *);
|
extern "C" void OSFatal(const char *);
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,22 @@
|
||||||
#include "wums/reent_internal.h"
|
#include "wums/reent_internal.h"
|
||||||
#include "wums/wums_debug.h"
|
#include "wums/wums_debug.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
extern "C" void OSFatal(const char *format, ...);
|
extern "C" void OSFatal(const char *format, ...);
|
||||||
extern "C" void *OSGetCurrentThread();
|
extern "C" void *OSGetCurrentThread();
|
||||||
|
|
||||||
#include <stdlib.h>
|
typedef void *(*MEMAllocFromDefaultHeapFn)(uint32_t size);
|
||||||
|
typedef void *(*MEMAllocFromDefaultHeapExFn)(uint32_t size, int32_t alignment);
|
||||||
|
typedef void (*MEMFreeToDefaultHeapFn)(void *ptr);
|
||||||
|
|
||||||
|
extern MEMAllocFromDefaultHeapFn MEMAllocFromDefaultHeap;
|
||||||
|
extern MEMAllocFromDefaultHeapExFn MEMAllocFromDefaultHeapEx;
|
||||||
|
extern MEMFreeToDefaultHeapFn MEMFreeToDefaultHeap;
|
||||||
|
|
||||||
|
|
||||||
struct wums_loader_init_reent_args_t {
|
struct wums_loader_init_reent_args_t {
|
||||||
WUMSReent_GetReentContext get_context_ptr = nullptr;
|
WUMSReent_GetReentContext get_context_ptr = nullptr;
|
||||||
WUMSReent_SetSentinel set_sentinel_ptr = nullptr;
|
|
||||||
WUMSReent_RestoreHead restore_head_ptr = nullptr;
|
|
||||||
WUMSReent_AddReentContext add_reent_context_ptr = nullptr;
|
WUMSReent_AddReentContext add_reent_context_ptr = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -26,28 +33,18 @@ void WUMSReentAPI_InitInternal(wums_loader_init_reent_args_t_ args) {
|
||||||
WUMS_DEBUG_REPORT("WUMSReentAPI_InitInternal: Initializing reent module\n");
|
WUMS_DEBUG_REPORT("WUMSReentAPI_InitInternal: Initializing reent module\n");
|
||||||
|
|
||||||
__internal_functions.get_context_ptr = args.get_context_ptr;
|
__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.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 module id
|
// use variable in the .data section as unique module id
|
||||||
static const int sReentModuleId = 0;
|
static const int sReentModuleId = 0;
|
||||||
|
|
||||||
void *wums_backend_get_context(const void *id, wums_loader_init_reent_errors_t_ *outError) {
|
bool wums_backend_get_context(const void *id, void **outPtr) {
|
||||||
return __internal_functions.get_context_ptr(id, outError);
|
return __internal_functions.get_context_ptr(id, outPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wums_backend_set_sentinel() {
|
bool wums_backend_register_context(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *)) {
|
||||||
return __internal_functions.set_sentinel_ptr();
|
return __internal_functions.add_reent_context_ptr(moduleId, reentPtr, cleanupFn);
|
||||||
}
|
|
||||||
|
|
||||||
void wums_backend_restore_head(void *head) {
|
|
||||||
__internal_functions.restore_head_ptr(head);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wums_backend_register_context(const void *moduleId, void *reentPtr, void (*cleanupFn)(void *), void *oldHead) {
|
|
||||||
return __internal_functions.add_reent_context_ptr(moduleId, reentPtr, cleanupFn, oldHead);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void reclaim_reent_trampoline(void *payload) {
|
static void reclaim_reent_trampoline(void *payload) {
|
||||||
|
|
@ -56,7 +53,9 @@ static void reclaim_reent_trampoline(void *payload) {
|
||||||
if (payload) {
|
if (payload) {
|
||||||
auto *reentPtr = static_cast<_reent *>(payload);
|
auto *reentPtr = static_cast<_reent *>(payload);
|
||||||
_reclaim_reent(reentPtr);
|
_reclaim_reent(reentPtr);
|
||||||
free(reentPtr);
|
|
||||||
|
// Make sure to use MEMFreeToDefaultHeap
|
||||||
|
MEMFreeToDefaultHeap(reentPtr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -64,22 +63,18 @@ struct _reent *__wums_getreent() {
|
||||||
if (!OSGetCurrentThread()) {
|
if (!OSGetCurrentThread()) {
|
||||||
return _GLOBAL_REENT;
|
return _GLOBAL_REENT;
|
||||||
}
|
}
|
||||||
wums_loader_init_reent_errors_t_ error = WUMSReent_ERROR_NONE;
|
void *existingCtx = nullptr;
|
||||||
if (void *existingCtx = wums_backend_get_context(&sReentModuleId, &error)) {
|
if (!wums_backend_get_context(&sReentModuleId, &existingCtx)) {
|
||||||
|
return _GLOBAL_REENT;
|
||||||
|
}
|
||||||
|
// if non-null we can use it
|
||||||
|
if (existingCtx) {
|
||||||
return static_cast<_reent *>(existingCtx);
|
return static_cast<_reent *>(existingCtx);
|
||||||
}
|
}
|
||||||
switch (error) {
|
|
||||||
case WUMSReent_ERROR_GLOBAL_REENT_REQUESTED:
|
|
||||||
case WUMSReent_ERROR_NO_THREAD:
|
|
||||||
return _GLOBAL_REENT;
|
|
||||||
case WUMSReent_ERROR_NONE:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto *oldHead = wums_backend_set_sentinel();
|
// Use `MEMAllocFromDefaultHeap` to avoid creating a new reent for allocating the reent
|
||||||
auto *newReent = static_cast<struct _reent *>(malloc(sizeof(struct _reent)));
|
auto *newReent = static_cast<struct _reent *>(MEMAllocFromDefaultHeap(sizeof(struct _reent)));
|
||||||
if (!newReent) {
|
if (!newReent) {
|
||||||
wums_backend_restore_head(oldHead);
|
|
||||||
return _GLOBAL_REENT;
|
return _GLOBAL_REENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -87,17 +82,16 @@ struct _reent *__wums_getreent() {
|
||||||
|
|
||||||
_REENT_INIT_PTR(newReent);
|
_REENT_INIT_PTR(newReent);
|
||||||
|
|
||||||
bool result = wums_backend_register_context(
|
const bool result = wums_backend_register_context(
|
||||||
&sReentModuleId,
|
&sReentModuleId,
|
||||||
newReent,
|
newReent,
|
||||||
reclaim_reent_trampoline,
|
reclaim_reent_trampoline);
|
||||||
oldHead);
|
|
||||||
|
|
||||||
if (!result) {
|
if (!result) {
|
||||||
WUMS_DEBUG_WARN("Failed to register context for thread %p\n", OSGetCurrentThread());
|
WUMS_DEBUG_WARN("Failed to register context for thread %p\n", OSGetCurrentThread());
|
||||||
_reclaim_reent(newReent);
|
_reclaim_reent(newReent);
|
||||||
free(newReent);
|
// Make sure to call the free function
|
||||||
wums_backend_restore_head(oldHead);
|
MEMFreeToDefaultHeap(newReent);
|
||||||
return _GLOBAL_REENT;
|
return _GLOBAL_REENT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user