From 4220023511a11c3459c6b2567b3a8f0dcdb29413 Mon Sep 17 00:00:00 2001 From: James Benton Date: Wed, 30 May 2018 13:14:01 +0100 Subject: [PATCH] wutstdc++: Use malloc / free instead of MEM functions. --- libraries/wutstdc++/wut_gthread_keys.cc | 24 +++++++++++++++++++---- libraries/wutstdc++/wut_gthread_thread.cc | 14 ++++++------- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/libraries/wutstdc++/wut_gthread_keys.cc b/libraries/wutstdc++/wut_gthread_keys.cc index 5db4642..f11e856 100644 --- a/libraries/wutstdc++/wut_gthread_keys.cc +++ b/libraries/wutstdc++/wut_gthread_keys.cc @@ -1,4 +1,6 @@ #include "wut_gthread.h" + +#include #include #include @@ -59,8 +61,11 @@ __wut_get_thread_keys() { const void **keys = (const void **)OSGetThreadSpecific(__WUT_KEY_THREAD_SPECIFIC_ID); if (!keys) { - MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - keys = (const void **)MEMAllocFromExpHeapEx(heap, sizeof(void *) * sizeof(__WUT_MAX_KEYS), 4); + keys = (const void **)malloc(sizeof(void *) * sizeof(__WUT_MAX_KEYS)); + if (!keys) { + return NULL; + } + memset(keys, 0, sizeof(void *) * sizeof(__WUT_MAX_KEYS)); OSSetThreadSpecific(__WUT_KEY_THREAD_SPECIFIC_ID, keys); } @@ -71,14 +76,24 @@ __wut_get_thread_keys() void * __wut_getspecific(__wut_key_t key) { - return (void *)__wut_get_thread_keys()[key]; + const void **keys = __wut_get_thread_keys(); + if (!keys) { + return NULL; + } + + return (void *)(keys[key]); } int __wut_setspecific(__wut_key_t key, const void *ptr) { - __wut_get_thread_keys()[key] = ptr; + const void **keys = __wut_get_thread_keys(); + if (!keys) { + return -1; + } + + keys[key] = ptr; return 0; } @@ -99,4 +114,5 @@ __wut_key_cleanup(OSThread *thread) } __wut_mutex_unlock(&key_mutex); + free(keys); } diff --git a/libraries/wutstdc++/wut_gthread_thread.cc b/libraries/wutstdc++/wut_gthread_thread.cc index 4bccdd8..93015b7 100644 --- a/libraries/wutstdc++/wut_gthread_thread.cc +++ b/libraries/wutstdc++/wut_gthread_thread.cc @@ -1,5 +1,6 @@ #include "wut_gthread.h" +#include #include #include @@ -7,9 +8,8 @@ static void __wut_thread_deallocator(OSThread *thread, void *stack) { - MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - MEMFreeToExpHeap(heap, thread); - MEMFreeToExpHeap(heap, stack); + free(thread); + free(stack); } static void @@ -23,9 +23,8 @@ __wut_thread_create(OSThread **outThread, void *(*entryPoint) (void*), void *entryArgs) { - MEMExpandedHeap *heap = (MEMExpandedHeap *)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2); - OSThread *thread = (OSThread *)MEMAllocFromExpHeapEx(heap, sizeof(OSThread), 8); - char *stack = (char *)MEMAllocFromExpHeapEx(heap, __WUT_STACK_SIZE, 8); + OSThread *thread = (OSThread *)memalign(16, sizeof(OSThread)); + char *stack = (char *)memalign(16, __WUT_STACK_SIZE); memset(thread, 0, sizeof(OSThread)); if (!OSCreateThread(thread, @@ -36,7 +35,8 @@ __wut_thread_create(OSThread **outThread, __WUT_STACK_SIZE, 16, OS_THREAD_ATTRIB_AFFINITY_ANY)) { - MEMFreeToExpHeap((MEMExpandedHeap*)MEMGetBaseHeapHandle(MEM_BASE_HEAP_MEM2), thread); + free(thread); + free(stack); return EINVAL; }