diff options
author | Shauren <shauren.trinity@gmail.com> | 2023-01-06 16:52:44 +0100 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2023-08-12 17:56:15 +0200 |
commit | f5a2e2ae13e3f7b505b069c1cd98e3157c31c83a (patch) | |
tree | 50567e305521694de10f09f038831ccf4d19552c | |
parent | 30adff16ae8806b7d5df824b6d3fdd331f7ff233 (diff) |
Core/Misc: Replaced boost::container::static_vector in packet classes with plain vector with custom static storage allocatlr
(cherry picked from commit b65c3f5f4a4edbc953c405bfacd33f186f3a1931)
-rw-r--r-- | dep/CMakeLists.txt | 1 | ||||
-rw-r--r-- | dep/PackageList.txt | 4 | ||||
-rw-r--r-- | dep/short_alloc/CMakeLists.txt | 15 | ||||
-rw-r--r-- | dep/short_alloc/short_alloc/short_alloc.h | 162 | ||||
-rw-r--r-- | src/common/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/server/game/Server/Packets/PacketUtilities.cpp | 12 | ||||
-rw-r--r-- | src/server/game/Server/Packets/PacketUtilities.h | 169 |
7 files changed, 244 insertions, 122 deletions
diff --git a/dep/CMakeLists.txt b/dep/CMakeLists.txt index a87f460ed95..71fa48710ce 100644 --- a/dep/CMakeLists.txt +++ b/dep/CMakeLists.txt @@ -29,6 +29,7 @@ if(SERVERS) add_subdirectory(readline) add_subdirectory(gsoap) add_subdirectory(efsw) + add_subdirectory(short_alloc) endif() if(TOOLS) diff --git a/dep/PackageList.txt b/dep/PackageList.txt index e4b2ece30b2..026578d952c 100644 --- a/dep/PackageList.txt +++ b/dep/PackageList.txt @@ -72,3 +72,7 @@ argon2 catch2 https://github.com/catchorg/Catch2 Version: v2.13.0 + +short_alloc (Stack based allocator) https://howardhinnant.github.io/stack_alloc.html + https://howardhinnant.github.io/short_alloc.h + Version: N/A diff --git a/dep/short_alloc/CMakeLists.txt b/dep/short_alloc/CMakeLists.txt new file mode 100644 index 00000000000..fc6ae3941b7 --- /dev/null +++ b/dep/short_alloc/CMakeLists.txt @@ -0,0 +1,15 @@ +# This file is part of the TrinityCore Project. See AUTHORS file for Copyright information +# +# This file is free software; as a special exception the author gives +# unlimited permission to copy and/or distribute it, with or without +# modifications, as long as this notice is preserved. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY, to the extent permitted by law; without even the +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + +add_library(short_alloc INTERFACE) + +target_include_directories(short_alloc + INTERFACE + ${CMAKE_CURRENT_SOURCE_DIR}) diff --git a/dep/short_alloc/short_alloc/short_alloc.h b/dep/short_alloc/short_alloc/short_alloc.h new file mode 100644 index 00000000000..eb8d02c7917 --- /dev/null +++ b/dep/short_alloc/short_alloc/short_alloc.h @@ -0,0 +1,162 @@ +#ifndef SHORT_ALLOC_H +#define SHORT_ALLOC_H + +// The MIT License (MIT) +// +// Copyright (c) 2015 Howard Hinnant +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all +// copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +#include <cassert> +#include <cstddef> +#include <cstdint> + +namespace short_alloc +{ +template <std::size_t N, std::size_t alignment = alignof(std::max_align_t)> +class arena +{ + alignas(alignment) char buf_[N]; + char* ptr_; + +public: + ~arena() {ptr_ = nullptr;} + arena() noexcept : ptr_(buf_) {} + arena(const arena&) = delete; + arena& operator=(const arena&) = delete; + + template <std::size_t ReqAlign> char* allocate(std::size_t n); + void deallocate(char* p, std::size_t n) noexcept; + + static constexpr std::size_t size() noexcept {return N;} + std::size_t used() const noexcept {return static_cast<std::size_t>(ptr_ - buf_);} + void reset() noexcept {ptr_ = buf_;} + +private: + static + std::size_t + align_up(std::size_t n) noexcept + {return (n + (alignment-1)) & ~(alignment-1);} + + bool + pointer_in_buffer(char* p) noexcept + { + return std::uintptr_t(buf_) <= std::uintptr_t(p) && + std::uintptr_t(p) <= std::uintptr_t(buf_) + N; + } +}; + +template <std::size_t N, std::size_t alignment> +template <std::size_t ReqAlign> +char* +arena<N, alignment>::allocate(std::size_t n) +{ + static_assert(ReqAlign <= alignment, "alignment is too small for this arena"); + assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena"); + auto const aligned_n = align_up(n); + if (static_cast<decltype(aligned_n)>(buf_ + N - ptr_) >= aligned_n) + { + char* r = ptr_; + ptr_ += aligned_n; + return r; + } + + static_assert(alignment <= alignof(std::max_align_t), "you've chosen an " + "alignment that is larger than alignof(std::max_align_t), and " + "cannot be guaranteed by normal operator new"); + return static_cast<char*>(::operator new(n)); +} + +template <std::size_t N, std::size_t alignment> +void +arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept +{ + assert(pointer_in_buffer(ptr_) && "short_alloc has outlived arena"); + if (pointer_in_buffer(p)) + { + n = align_up(n); + if (p + n == ptr_) + ptr_ = p; + } + else + ::operator delete(p); +} + +template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)> +class short_alloc +{ +public: + using value_type = T; + static auto constexpr alignment = Align; + static auto constexpr size = N; + using arena_type = arena<size, alignment>; + +private: + arena_type& a_; + +public: + short_alloc(const short_alloc&) = default; + short_alloc& operator=(const short_alloc&) = delete; + + short_alloc(arena_type& a) noexcept : a_(a) + { + static_assert(size % alignment == 0, + "size N needs to be a multiple of alignment Align"); + } + template <class U> + short_alloc(const short_alloc<U, N, alignment>& a) noexcept + : a_(a.a_) {} + + template <class _Up> struct rebind {using other = short_alloc<_Up, N, alignment>;}; + + T* allocate(std::size_t n) + { + return reinterpret_cast<T*>(a_.template allocate<alignof(T)>(n*sizeof(T))); + } + void deallocate(T* p, std::size_t n) noexcept + { + a_.deallocate(reinterpret_cast<char*>(p), n*sizeof(T)); + } + + template <class T1, std::size_t N1, std::size_t A1, + class U, std::size_t M, std::size_t A2> + friend + bool + operator==(const short_alloc<T1, N1, A1>& x, const short_alloc<U, M, A2>& y) noexcept; + + template <class U, std::size_t M, std::size_t A> friend class short_alloc; +}; + +template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2> +inline +bool +operator==(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept +{ + return N == M && A1 == A2 && &x.a_ == &y.a_; +} + +template <class T, std::size_t N, std::size_t A1, class U, std::size_t M, std::size_t A2> +inline +bool +operator!=(const short_alloc<T, N, A1>& x, const short_alloc<U, M, A2>& y) noexcept +{ + return !(x == y); +} +} +#endif // SHORT_ALLOC_H diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 5258258a7c6..67d0d663c73 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -72,7 +72,8 @@ target_link_libraries(common openssl valgrind threads - jemalloc) + jemalloc + short_alloc) add_dependencies(common revision_data.h) diff --git a/src/server/game/Server/Packets/PacketUtilities.cpp b/src/server/game/Server/Packets/PacketUtilities.cpp index 862c643013c..fe7487daaee 100644 --- a/src/server/game/Server/Packets/PacketUtilities.cpp +++ b/src/server/game/Server/Packets/PacketUtilities.cpp @@ -17,10 +17,7 @@ #include "PacketUtilities.h" #include "Hyperlinks.h" -#include "Errors.h" #include <utf8.h> -#include <sstream> -#include <array> WorldPackets::InvalidStringValueException::InvalidStringValueException(std::string const& value) : ByteBufferInvalidValueException("string", value.c_str()) { @@ -61,12 +58,5 @@ bool WorldPackets::Strings::NoHyperlinks::Validate(std::string const& value) WorldPackets::PacketArrayMaxCapacityException::PacketArrayMaxCapacityException(std::size_t requestedSize, std::size_t sizeLimit) { - std::ostringstream builder; - builder << "Attempted to read more array elements from packet " << requestedSize << " than allowed " << sizeLimit; - message().assign(builder.str()); -} - -void WorldPackets::CheckCompactArrayMaskOverflow(std::size_t index, std::size_t limit) -{ - ASSERT(index < limit, "Attempted to insert " SZFMTD " values into CompactArray but it can only hold " SZFMTD, index, limit); + message().assign("Attempted to read more array elements from packet " + Trinity::ToString(requestedSize) + " than allowed " + Trinity::ToString(sizeLimit)); } diff --git a/src/server/game/Server/Packets/PacketUtilities.h b/src/server/game/Server/Packets/PacketUtilities.h index 0c06be5ea71..8b05803bb2b 100644 --- a/src/server/game/Server/Packets/PacketUtilities.h +++ b/src/server/game/Server/Packets/PacketUtilities.h @@ -20,6 +20,7 @@ #include "ByteBuffer.h" #include "Tuples.h" +#include <short_alloc/short_alloc.h> #include <string_view> namespace WorldPackets @@ -114,21 +115,49 @@ namespace WorldPackets /** * Utility class for automated prevention of loop counter spoofing in client packets */ - template<typename T, std::size_t N = 1000 /*select a sane default limit*/> + template<typename T, std::size_t N> class Array { - typedef std::vector<T> storage_type; + public: + using allocator_type = short_alloc::short_alloc<T, (N * sizeof(T) + (alignof(std::max_align_t) - 1)) & ~(alignof(std::max_align_t) - 1)>; + using arena_type = typename allocator_type::arena_type; - typedef typename storage_type::value_type value_type; - typedef typename storage_type::size_type size_type; - typedef typename storage_type::reference reference; - typedef typename storage_type::const_reference const_reference; - typedef typename storage_type::iterator iterator; - typedef typename storage_type::const_iterator const_iterator; + using storage_type = std::vector<T, allocator_type>; - public: - Array() : _limit(N) { } - Array(size_type limit) : _limit(limit) { } + using max_capacity = std::integral_constant<std::size_t, N>; + + using value_type = typename storage_type::value_type; + using size_type = typename storage_type::size_type; + using pointer = typename storage_type::pointer; + using const_pointer = typename storage_type::const_pointer; + using reference = typename storage_type::reference; + using const_reference = typename storage_type::const_reference; + using iterator = typename storage_type::iterator; + using const_iterator = typename storage_type::const_iterator; + + Array() : _storage(_data) { } + + Array(Array const& other) : Array() + { + for (T const& element : other) + _storage.push_back(element); + } + + Array(Array&& other) noexcept = delete; + + Array& operator=(Array const& other) + { + if (this == &other) + return *this; + + _storage.clear(); + for (T const& element : other) + _storage.push_back(element); + + return *this; + } + + Array& operator=(Array&& other) noexcept = delete; iterator begin() { return _storage.begin(); } const_iterator begin() const { return _storage.begin(); } @@ -136,6 +165,9 @@ namespace WorldPackets iterator end() { return _storage.end(); } const_iterator end() const { return _storage.end(); } + pointer data() { return _storage.data(); } + const_pointer data() const { return _storage.data(); } + size_type size() const { return _storage.size(); } bool empty() const { return _storage.empty(); } @@ -144,132 +176,49 @@ namespace WorldPackets void resize(size_type newSize) { - if (newSize > _limit) - throw PacketArrayMaxCapacityException(newSize, _limit); + if (newSize > max_capacity::value) + throw PacketArrayMaxCapacityException(newSize, max_capacity::value); _storage.resize(newSize); } - void reserve(size_type newSize) - { - if (newSize > _limit) - throw PacketArrayMaxCapacityException(newSize, _limit); - - _storage.reserve(newSize); - } - void push_back(value_type const& value) { - if (_storage.size() >= _limit) - throw PacketArrayMaxCapacityException(_storage.size() + 1, _limit); + if (_storage.size() >= max_capacity::value) + throw PacketArrayMaxCapacityException(_storage.size() + 1, max_capacity::value); _storage.push_back(value); } void push_back(value_type&& value) { - if (_storage.size() >= _limit) - throw PacketArrayMaxCapacityException(_storage.size() + 1, _limit); + if (_storage.size() >= max_capacity::value) + throw PacketArrayMaxCapacityException(_storage.size() + 1, max_capacity::value); _storage.push_back(std::forward<value_type>(value)); } - private: - storage_type _storage; - size_type _limit; - }; - - void CheckCompactArrayMaskOverflow(std::size_t index, std::size_t limit); - - template <typename T> - class CompactArray - { - public: - CompactArray() : _mask(0) { } - - CompactArray(CompactArray const& right) - : _mask(right._mask), _contents(right._contents) { } - - CompactArray(CompactArray&& right) - : _mask(right._mask), _contents(std::move(right._contents)) - { - right._mask = 0; - } - - CompactArray& operator=(CompactArray const& right) - { - _mask = right._mask; - _contents = right._contents; - return *this; - } - - CompactArray& operator=(CompactArray&& right) + template<typename... Args> + T& emplace_back(Args&&... args) { - _mask = right._mask; - right._mask = 0; - _contents = std::move(right._contents); - return *this; + _storage.emplace_back(std::forward<Args>(args)...); + return _storage.back(); } - uint32 GetMask() const { return _mask; } - T const& operator[](std::size_t index) const { return _contents.at(index); } - std::size_t GetSize() const { return _contents.size(); } - - void Insert(std::size_t index, T const& value) + iterator erase(const_iterator first, const_iterator last) { - CheckCompactArrayMaskOverflow(index, sizeof(_mask) * 8); - - _mask |= 1 << index; - if (_contents.size() <= index) - _contents.resize(index + 1); - _contents[index] = value; + return _storage.erase(first, last); } - void Clear() + void clear() { - _mask = 0; - _contents.clear(); + _storage.clear(); } - bool operator==(CompactArray const& r) const - { - if (_mask != r._mask) - return false; - - return _contents == r._contents; - } - - bool operator!=(CompactArray const& r) const { return !(*this == r); } - private: - uint32 _mask; - std::vector<T> _contents; + arena_type _data; + storage_type _storage; }; - - template <typename T> - ByteBuffer& operator<<(ByteBuffer& data, CompactArray<T> const& v) - { - uint32 mask = v.GetMask(); - data << uint32(mask); - for (std::size_t i = 0; i < v.GetSize(); ++i) - if (mask & (1 << i)) - data << v[i]; - - return data; - } - - template <typename T> - ByteBuffer& operator>>(ByteBuffer& data, CompactArray<T>& v) - { - uint32 mask; - data >> mask; - - for (std::size_t index = 0; mask != 0; mask >>= 1, ++index) - if ((mask & 1) != 0) - v.Insert(index, data.read<T>()); - - return data; - } } #endif // PacketUtilities_h__ |