diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-07-09 12:20:10 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-07-09 12:20:10 +0200 |
commit | 2c74626e66e5fd88683348f37f4b30ab397bf55b (patch) | |
tree | 50b4dee8256022d29c0146834ab604d16c5d9ddd | |
parent | c7f5696479bb85c8fc7ee0d9fdaeb92e2b32213f (diff) |
Core/Random: Use standard aligned operator new and remove RandomEngine singleton instance (it is trivially constructible)
-rw-r--r-- | src/common/Utilities/Containers.h | 2 | ||||
-rw-r--r-- | src/common/Utilities/Random.cpp | 17 | ||||
-rw-r--r-- | src/common/Utilities/Random.h | 4 | ||||
-rw-r--r-- | src/common/Utilities/SFMTRand.cpp | 77 | ||||
-rw-r--r-- | src/common/Utilities/SFMTRand.h | 16 |
5 files changed, 17 insertions, 99 deletions
diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h index 11928fa053d..551587a9eab 100644 --- a/src/common/Utilities/Containers.h +++ b/src/common/Utilities/Containers.h @@ -170,7 +170,7 @@ namespace Trinity template <std::random_access_iterator Iterator> inline void RandomShuffle(Iterator begin, Iterator end) { - std::ranges::shuffle(begin, end, RandomEngine::Instance()); + std::ranges::shuffle(begin, end, RandomEngine()); } /** diff --git a/src/common/Utilities/Random.cpp b/src/common/Utilities/Random.cpp index 973832a1cf3..1cf4da95bfe 100644 --- a/src/common/Utilities/Random.cpp +++ b/src/common/Utilities/Random.cpp @@ -21,16 +21,16 @@ #include <memory> #include <random> -static thread_local std::unique_ptr<SFMTRand> sfmtRand; -static RandomEngine engine; - -static SFMTRand* GetRng() +namespace { - if (!sfmtRand) - sfmtRand = std::make_unique<SFMTRand>(); +constexpr RandomEngine engine; +SFMTRand* GetRng() noexcept +{ + thread_local std::unique_ptr<SFMTRand> sfmtRand = std::make_unique<SFMTRand>(); return sfmtRand.get(); } +} int32 irand(int32 min, int32 max) { @@ -89,8 +89,3 @@ uint32 urandweighted(size_t count, double const* chances) std::discrete_distribution<uint32> dd(chances, chances + count); return dd(engine); } - -RandomEngine& RandomEngine::Instance() -{ - return engine; -} diff --git a/src/common/Utilities/Random.h b/src/common/Utilities/Random.h index c658c477ab1..2de685db456 100644 --- a/src/common/Utilities/Random.h +++ b/src/common/Utilities/Random.h @@ -64,7 +64,7 @@ inline bool roll_chance_i(int chance) /* * Wrapper satisfying UniformRandomNumberGenerator concept for use in <random> algorithms */ -class TC_COMMON_API RandomEngine +class RandomEngine { public: typedef uint32 result_type; @@ -72,8 +72,6 @@ public: static constexpr result_type min() { return std::numeric_limits<result_type>::min(); } static constexpr result_type max() { return std::numeric_limits<result_type>::max(); } result_type operator()() const { return rand32(); } - - static RandomEngine& Instance(); }; #endif // Random_h__ diff --git a/src/common/Utilities/SFMTRand.cpp b/src/common/Utilities/SFMTRand.cpp index 63918fce710..e39cfa1bc46 100644 --- a/src/common/Utilities/SFMTRand.cpp +++ b/src/common/Utilities/SFMTRand.cpp @@ -22,44 +22,13 @@ #include <random> #include <ctime> -#if __has_include(<mm_malloc.h>) -#include <mm_malloc.h> -#elif __has_include(<malloc.h>) && TRINITY_COMPILER == TRINITY_COMPILER_MICROSOFT -#include <malloc.h> -#else -static __inline__ void *__attribute__((__always_inline__, __nodebug__, __malloc__)) -_mm_malloc(size_t __size, size_t __align) -{ - if (__align == 1) - { - return malloc(__size); - } - - if (!(__align & (__align - 1)) && __align < sizeof(void *)) - __align = sizeof(void *); - - void *__mallocedMemory; - - if (posix_memalign(&__mallocedMemory, __align, __size)) - return NULL; - - return __mallocedMemory; -} - -static __inline__ void __attribute__((__always_inline__, __nodebug__)) -_mm_free(void *__p) -{ - free(__p); -} -#endif - -SFMTRand::SFMTRand() +SFMTRand::SFMTRand() noexcept { std::random_device dev; if (dev.entropy() > 0) { std::array<uint32, SFMT_N32> seed; - std::generate(seed.begin(), seed.end(), std::ref(dev)); + std::ranges::generate(seed, std::ref(dev)); sfmt_init_by_array(&_state, seed.data(), seed.size()); } @@ -67,47 +36,7 @@ SFMTRand::SFMTRand() sfmt_init_gen_rand(&_state, uint32(time(nullptr))); } -uint32 SFMTRand::RandomUInt32() // Output random bits +uint32 SFMTRand::RandomUInt32() noexcept // Output random bits { return sfmt_genrand_uint32(&_state); } - -void* SFMTRand::operator new(size_t size, std::nothrow_t const&) -{ - return _mm_malloc(size, 16); -} - -void SFMTRand::operator delete(void* ptr, std::nothrow_t const&) -{ - _mm_free(ptr); -} - -void* SFMTRand::operator new(size_t size) -{ - return _mm_malloc(size, 16); -} - -void SFMTRand::operator delete(void* ptr) -{ - _mm_free(ptr); -} - -void* SFMTRand::operator new[](size_t size, std::nothrow_t const&) -{ - return _mm_malloc(size, 16); -} - -void SFMTRand::operator delete[](void* ptr, std::nothrow_t const&) -{ - _mm_free(ptr); -} - -void* SFMTRand::operator new[](size_t size) -{ - return _mm_malloc(size, 16); -} - -void SFMTRand::operator delete[](void* ptr) -{ - _mm_free(ptr); -} diff --git a/src/common/Utilities/SFMTRand.h b/src/common/Utilities/SFMTRand.h index 7d9a820c487..26c1816f667 100644 --- a/src/common/Utilities/SFMTRand.h +++ b/src/common/Utilities/SFMTRand.h @@ -27,16 +27,12 @@ */ class SFMTRand { public: - SFMTRand(); - uint32 RandomUInt32(); // Output random bits - void* operator new(size_t size, std::nothrow_t const&); - void operator delete(void* ptr, std::nothrow_t const&); - void* operator new(size_t size); - void operator delete(void* ptr); - void* operator new[](size_t size, std::nothrow_t const&); - void operator delete[](void* ptr, std::nothrow_t const&); - void* operator new[](size_t size); - void operator delete[](void* ptr); + SFMTRand() noexcept; + uint32 RandomUInt32() noexcept; // Output random bits + void* operator new(size_t size) noexcept { return ::operator new (size, std::align_val_t(alignof(SFMTRand)), std::nothrow); } + void operator delete(void* ptr) noexcept { ::operator delete (ptr, std::align_val_t(alignof(SFMTRand)), std::nothrow); } + void* operator new[](size_t size) noexcept { return ::operator new[](size, std::align_val_t(alignof(SFMTRand)), std::nothrow); } + void operator delete[](void* ptr) noexcept { ::operator delete[](ptr, std::align_val_t(alignof(SFMTRand)), std::nothrow); } private: sfmt_t _state; }; |