From 946d1ae9b7027dc88c2f2824d946cc882f2fc92c Mon Sep 17 00:00:00 2001 From: PJB3005 Date: Mon, 23 Mar 2026 13:34:08 +0100 Subject: [PATCH] Threading cleanup: remove MallocAllocator stuff Not necessary anymore now that JKRHeap is isolated --- src/dusk/OSMutex.cpp | 53 +++++-------------------------------------- src/dusk/OSThread.cpp | 51 +++++------------------------------------ src/dusk/stubs.cpp | 43 ++++------------------------------- 3 files changed, 16 insertions(+), 131 deletions(-) diff --git a/src/dusk/OSMutex.cpp b/src/dusk/OSMutex.cpp index 55d8ccb52f..8e26402e0d 100644 --- a/src/dusk/OSMutex.cpp +++ b/src/dusk/OSMutex.cpp @@ -14,43 +14,6 @@ #include "JSystem/JKernel/JKRHeap.h" -// ============================================================================ -// Malloc-based allocator to bypass JKRHeap operator new/delete -// Without this, side-table allocations call operator new -> JKRHeap::alloc -// -> OSLockMutex -> GetMutexData -> operator new ... infinite recursion. -// ============================================================================ - -template -struct MallocAllocator { - using value_type = T; - MallocAllocator() = default; - template MallocAllocator(const MallocAllocator&) noexcept {} - T* allocate(std::size_t n) { - void* p = std::malloc(n * sizeof(T)); - if (!p) throw std::bad_alloc(); - return static_cast(p); - } - void deallocate(T* p, std::size_t) noexcept { std::free(p); } - template bool operator==(const MallocAllocator&) const noexcept { return true; } - template bool operator!=(const MallocAllocator&) const noexcept { return false; } -}; - -template -struct MallocDeleter { - void operator()(T* p) const { - p->~T(); - std::free(p); - } -}; - -template -std::unique_ptr> make_malloc_unique(Args&&... args) { - void* mem = std::malloc(sizeof(T)); - if (!mem) throw std::bad_alloc(); - T* obj = JKR_NEW_ARGS (mem) T(std::forward(args)...); - return std::unique_ptr>(obj); -} - // ============================================================================ // Side-table: native mutex per OSMutex // ============================================================================ @@ -59,17 +22,13 @@ struct PCMutexData { std::recursive_mutex nativeMutex; }; -template -using MallocMap = std::unordered_map, std::equal_to, - MallocAllocator>>; - // Lazy-initialized to avoid DLL static init crashes static std::mutex& GetMutexMapMutex() { static std::mutex mtx; return mtx; } -static MallocMap>>& GetMutexMap() { - static MallocMap>> map; +static std::unordered_map>& GetMutexMap() { + static std::unordered_map> map; return map; } @@ -78,7 +37,7 @@ static PCMutexData& GetMutexData(OSMutex* mutex) { auto& map = GetMutexMap(); auto it = map.find(mutex); if (it == map.end()) { - auto result = map.emplace(mutex, make_malloc_unique()); + auto result = map.emplace(mutex, std::make_unique()); return *result.first->second; } return *it->second; @@ -97,8 +56,8 @@ static std::mutex& GetCondMapMutex() { static std::mutex mtx; return mtx; } -static MallocMap>>& GetCondMap() { - static MallocMap>> map; +static std::unordered_map>& GetCondMap() { + static std::unordered_map> map; return map; } @@ -107,7 +66,7 @@ static PCCondData& GetCondData(OSCond* cond) { auto& map = GetCondMap(); auto it = map.find(cond); if (it == map.end()) { - auto result = map.emplace(cond, make_malloc_unique()); + auto result = map.emplace(cond, std::make_unique()); return *result.first->second; } return *it->second; diff --git a/src/dusk/OSThread.cpp b/src/dusk/OSThread.cpp index 2d19139272..0be30f4aaf 100644 --- a/src/dusk/OSThread.cpp +++ b/src/dusk/OSThread.cpp @@ -18,45 +18,6 @@ #include "JSystem/JKernel/JKRHeap.h" -// ============================================================================ -// Malloc-based allocator to bypass JKRHeap operator new/delete -// ============================================================================ - -template -struct MallocAllocator { - using value_type = T; - MallocAllocator() = default; - template MallocAllocator(const MallocAllocator&) noexcept {} - T* allocate(std::size_t n) { - void* p = std::malloc(n * sizeof(T)); - if (!p) throw std::bad_alloc(); - return static_cast(p); - } - void deallocate(T* p, std::size_t) noexcept { std::free(p); } - template bool operator==(const MallocAllocator&) const noexcept { return true; } - template bool operator!=(const MallocAllocator&) const noexcept { return false; } -}; - -template -struct MallocDeleter { - void operator()(T* p) const { - p->~T(); - std::free(p); - } -}; - -template -std::unique_ptr> make_malloc_unique(Args&&... args) { - void* mem = std::malloc(sizeof(T)); - if (!mem) throw std::bad_alloc(); - T* obj = JKR_NEW_ARGS (mem) T(std::forward(args)...); - return std::unique_ptr>(obj); -} - -template -using MallocMap = std::unordered_map, std::equal_to, - MallocAllocator>>; - // ============================================================================ // Side-table: native thread data per OSThread // ============================================================================ @@ -76,8 +37,8 @@ static std::mutex& GetThreadDataMutex() { static std::mutex mtx; return mtx; } -static MallocMap>>& GetThreadDataMap() { - static MallocMap>> map; +static std::unordered_map>& GetThreadDataMap() { + static std::unordered_map> map; return map; } @@ -86,8 +47,8 @@ static std::mutex& GetQueueCvMutex() { static std::mutex mtx; return mtx; } -static MallocMap>>& GetQueueCvMap() { - static MallocMap>> map; +static std::unordered_map>& GetQueueCvMap() { + static std::unordered_map> map; return map; } @@ -96,7 +57,7 @@ static std::condition_variable& GetQueueCV(OSThreadQueue* queue) { auto& map = GetQueueCvMap(); auto it = map.find(queue); if (it == map.end()) { - auto result = map.emplace(queue, make_malloc_unique()); + auto result = map.emplace(queue, std::make_unique()); return *result.first->second; } return *it->second; @@ -295,7 +256,7 @@ int OSCreateThread(OSThread* thread, void* (*func)(void*), void* param, // Create side-table entry (but don't start the thread yet) { - auto data = make_malloc_unique(); + auto data = std::make_unique(); data->func = func; data->param = param; diff --git a/src/dusk/stubs.cpp b/src/dusk/stubs.cpp index 73f4e93d80..f4d1b2688b 100644 --- a/src/dusk/stubs.cpp +++ b/src/dusk/stubs.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -55,42 +56,6 @@ u32 OSGetSoundMode() { // Message Queue (thread-safe implementation) // ========================================================================== -// Malloc-based allocator to bypass JKRHeap operator new/delete -template -struct MallocAllocator { - using value_type = T; - MallocAllocator() = default; - template MallocAllocator(const MallocAllocator&) noexcept {} - T* allocate(std::size_t n) { - void* p = std::malloc(n * sizeof(T)); - if (!p) throw std::bad_alloc(); - return static_cast(p); - } - void deallocate(T* p, std::size_t) noexcept { std::free(p); } - template bool operator==(const MallocAllocator&) const noexcept { return true; } - template bool operator!=(const MallocAllocator&) const noexcept { return false; } -}; - -template -struct MallocDeleter { - void operator()(T* p) const { - p->~T(); - std::free(p); - } -}; - -template -std::unique_ptr> make_malloc_unique(Args&&... args) { - void* mem = std::malloc(sizeof(T)); - if (!mem) throw std::bad_alloc(); - T* obj = new (mem) T(std::forward(args)...); - return std::unique_ptr>(obj); -} - -template -using MallocMap = std::unordered_map, std::equal_to, - MallocAllocator>>; - // Side-table for native synchronization per OSMessageQueue struct PCMessageQueueData { std::mutex mtx; @@ -103,8 +68,8 @@ static std::mutex& GetMsgQueueMapMutex() { static std::mutex mtx; return mtx; } -static MallocMap>>& GetMsgQueueMap() { - static MallocMap>> map; +static std::unordered_map>& GetMsgQueueMap() { + static std::unordered_map> map; return map; } @@ -113,7 +78,7 @@ static PCMessageQueueData& GetMsgQueueData(OSMessageQueue* mq) { auto& map = GetMsgQueueMap(); auto it = map.find(mq); if (it == map.end()) { - auto result = map.emplace(mq, make_malloc_unique()); + auto result = map.emplace(mq, std::make_unique()); return *result.first->second; } return *it->second;