diff --git a/rpcs3/Emu/RSX/Common/simple_array.hpp b/rpcs3/Emu/RSX/Common/simple_array.hpp index 090c51d674..50f8874992 100644 --- a/rpcs3/Emu/RSX/Common/simple_array.hpp +++ b/rpcs3/Emu/RSX/Common/simple_array.hpp @@ -3,11 +3,46 @@ #include #include #include +#include #include "reverse_ptr.hpp" namespace rsx { + namespace aligned_allocator + { + template + void* malloc(size_t size) + { +#ifdef _MSC_VER + return _aligned_malloc(size, Align); +#else + return std::aligned_alloc(Align, size); +#endif + } + + template + void* realloc(void* prev_ptr, [[maybe_unused]] size_t prev_size, size_t new_size) + { +#ifdef _MSC_VER + return _aligned_realloc(prev_ptr, new_size, Align); +#else + void* ret = std::aligned_alloc(Align, new_size); + std::memcpy(ret, prev_ptr, std::min(prev_size, new_size)); + return ret; +#endif + } + + static inline void free(void* ptr) + { +#ifdef _MSC_VER + _aligned_free(ptr); +#else + std::free(ptr); +#endif + } + } + template concept span_like = requires(C& c) { @@ -15,7 +50,7 @@ namespace rsx { c.size() } -> std::integral; }; - template + template requires std::is_trivially_destructible_v && std::is_trivially_copyable_v struct simple_array { @@ -28,7 +63,7 @@ namespace rsx private: static constexpr u32 _local_capacity = std::max(64u / sizeof(Ty), 1u); - char _local_storage[_local_capacity * sizeof(Ty)]; + alignas(Align) char _local_storage[_local_capacity * sizeof(Ty)]; u32 _capacity = _local_capacity; Ty* _data = _local_capacity ? reinterpret_cast(_local_storage) : nullptr; @@ -128,7 +163,7 @@ namespace rsx { if (!is_local_storage()) { - free(_data); + aligned_allocator::free(_data); } _data = nullptr; @@ -196,13 +231,13 @@ namespace rsx if (is_local_storage()) { // Switch to heap storage - ensure(_data = static_cast(std::malloc(sizeof(Ty) * size))); + ensure(_data = static_cast(aligned_allocator::malloc(sizeof(Ty) * size))); std::memcpy(static_cast(_data), _local_storage, size_bytes()); } else { // Extend heap storage - ensure(_data = static_cast(std::realloc(_data, sizeof(Ty) * size))); // "realloc() failed!" + ensure(_data = static_cast(aligned_allocator::realloc(_data, size_bytes(), sizeof(Ty) * size))); // "realloc() failed!" } _capacity = size;