From b65c3f5f4a4edbc953c405bfacd33f186f3a1931 Mon Sep 17 00:00:00 2001 From: Shauren Date: Fri, 6 Jan 2023 16:52:44 +0100 Subject: Core/Misc: Replaced boost::container::static_vector in packet classes with plain vector with custom static storage allocatlr --- dep/CMakeLists.txt | 1 + dep/PackageList.txt | 4 + dep/short_alloc/CMakeLists.txt | 15 +++ dep/short_alloc/short_alloc/short_alloc.h | 162 ++++++++++++++++++++++++++++++ 4 files changed, 182 insertions(+) create mode 100644 dep/short_alloc/CMakeLists.txt create mode 100644 dep/short_alloc/short_alloc/short_alloc.h (limited to 'dep') diff --git a/dep/CMakeLists.txt b/dep/CMakeLists.txt index b0b28c09bf1..0607f678338 100644 --- a/dep/CMakeLists.txt +++ b/dep/CMakeLists.txt @@ -32,6 +32,7 @@ if(SERVERS) add_subdirectory(rapidjson) add_subdirectory(efsw) add_subdirectory(protobuf) + add_subdirectory(short_alloc) endif() if(TOOLS) diff --git a/dep/PackageList.txt b/dep/PackageList.txt index bdcf4e72d0e..702b3963c4e 100644 --- a/dep/PackageList.txt +++ b/dep/PackageList.txt @@ -72,3 +72,7 @@ rapidjson (A fast JSON parser/generator for C++ with both SAX/DOM style API http protobuf (Protocol Buffers - Google's data interchange format https://developers.google.com/protocol-buffers/) https://github.com/google/protobuf Version: v2.6.1 + +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 +#include +#include + +namespace short_alloc +{ +template +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 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(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 +template +char* +arena::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(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(::operator new(n)); +} + +template +void +arena::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 short_alloc +{ +public: + using value_type = T; + static auto constexpr alignment = Align; + static auto constexpr size = N; + using arena_type = arena; + +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 + short_alloc(const short_alloc& a) noexcept + : a_(a.a_) {} + + template struct rebind {using other = short_alloc<_Up, N, alignment>;}; + + T* allocate(std::size_t n) + { + return reinterpret_cast(a_.template allocate(n*sizeof(T))); + } + void deallocate(T* p, std::size_t n) noexcept + { + a_.deallocate(reinterpret_cast(p), n*sizeof(T)); + } + + template + friend + bool + operator==(const short_alloc& x, const short_alloc& y) noexcept; + + template friend class short_alloc; +}; + +template +inline +bool +operator==(const short_alloc& x, const short_alloc& y) noexcept +{ + return N == M && A1 == A2 && &x.a_ == &y.a_; +} + +template +inline +bool +operator!=(const short_alloc& x, const short_alloc& y) noexcept +{ + return !(x == y); +} +} +#endif // SHORT_ALLOC_H -- cgit v1.2.3