6 Commits
9 changed files with 519 additions and 11 deletions
+2 -5
View File
@@ -77,10 +77,7 @@ arena<N, alignment>::allocate(std::size_t 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));
return static_cast<char*>(::operator new(n, std::align_val_t(alignment)));
}
template <std::size_t N, std::size_t alignment>
@@ -95,7 +92,7 @@ arena<N, alignment>::deallocate(char* p, std::size_t n) noexcept
ptr_ = p;
}
else
::operator delete(p);
::operator delete(p, std::align_val_t(alignment));
}
template <class T, std::size_t N, std::size_t Align = alignof(std::max_align_t)>
@@ -0,0 +1,237 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITYCORE_INTRUSIVE_FORWARD_LIST_H
#define TRINITYCORE_INTRUSIVE_FORWARD_LIST_H
#include <atomic>
#include <concepts>
#include <type_traits>
#include <utility>
namespace Trinity::Containers
{
namespace Impl
{
template <typename ListLink>
struct IntrusiveForwardListLinkTraits;
template <typename ListLink>
struct IntrusiveForwardListLinkTraits<ListLink*>
{
using value_type = ListLink;
inline static ListLink* GetValuePtr(ListLink* link) { return link; }
inline static ListLink const* GetValuePtr(ListLink const* link) { return link; }
};
template <typename ListLink>
struct IntrusiveForwardListLinkTraits<std::atomic<ListLink*>>
{
using value_type = ListLink;
inline static ListLink* GetValuePtr(std::atomic<ListLink*>& link) { return link.load(); }
inline static ListLink const* GetValuePtr(std::atomic<ListLink*> const& link) { return link.load(); }
};
template <typename ListLink>
struct IntrusiveForwardListTraits;
template <typename Object, typename Member>
struct IntrusiveForwardListTraits<Member Object::*>
{
using link_type = Member;
using link_traits = IntrusiveForwardListLinkTraits<link_type>;
using value_type = typename link_traits::value_type;
};
template <typename ListLink>
concept IntrusiveForwardListLink = std::is_member_object_pointer_v<ListLink>
&& std::same_as<typename IntrusiveForwardListTraits<ListLink>::value_type, typename IntrusiveForwardListTraits<ListLink>::link_traits::value_type>;
}
template <auto ListLink> requires (Impl::IntrusiveForwardListLink<decltype(ListLink)>)
class IntrusiveForwardList
{
using queue_traits = Impl::IntrusiveForwardListTraits<decltype(ListLink)>;
using link_traits = typename queue_traits::link_traits;
using value_type = typename queue_traits::value_type;
using link_type = typename queue_traits::link_type;
public:
IntrusiveForwardList() = default;
IntrusiveForwardList(IntrusiveForwardList const&) = delete;
IntrusiveForwardList(IntrusiveForwardList&& other) noexcept :
_head(link_traits::GetValuePtr(other._head)),
_tail(empty() ? std::addressof(_head) : std::exchange(other._tail, &other._head))
{
other._head = nullptr;
}
IntrusiveForwardList& operator=(IntrusiveForwardList const&) = delete;
IntrusiveForwardList& operator=(IntrusiveForwardList&& other) noexcept
{
if (this != &other)
{
_head = link_traits::GetValuePtr(other._head);
other._head = nullptr;
_tail = empty() ? std::addressof(_head) : std::exchange(other._tail, std::addressof(other._head));
}
return *this;
}
~IntrusiveForwardList()
{
value_type* node = link_traits::GetValuePtr(_head);
while (node)
{
value_type* next = node->*ListLink;
node->*ListLink = nullptr;
node = next;
}
}
bool empty() const
{
return !link_traits::GetValuePtr(_head);
}
value_type& front()
{
return *link_traits::GetValuePtr(_head);
}
value_type const& front() const
{
return *link_traits::GetValuePtr(_head);
}
void push_front(value_type& value)
{
value.*ListLink = link_traits::GetValuePtr(_head);
_head = std::addressof(value);
}
void push_back(value_type& value)
{
*_tail = std::addressof(value);
_tail = std::addressof(value.*ListLink);
}
void pop_front()
{
value_type* head = link_traits::GetValuePtr(_head);
_head = link_traits::GetValuePtr(head->*ListLink);
head->*ListLink = nullptr;
if (empty())
_tail = std::addressof(_head);
}
class const_iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = std::add_const_t<IntrusiveForwardList::value_type>;
using difference_type = ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
reference operator*() const { return static_cast<reference>(*link_traits::GetValuePtr(*_link)); }
pointer operator->() const { return static_cast<pointer>(link_traits::GetValuePtr(*_link)); }
const_iterator& operator++()
{
value_type* current = link_traits::GetValuePtr(*_link);
_link = std::addressof(current->*ListLink);
return *this;
}
const_iterator operator++(int)
{
const_iterator tmp = *this;
++*this;
return tmp;
}
bool operator==(const_iterator const&) const = default;
bool operator==(std::default_sentinel_t const&) const { return link_traits::GetValuePtr(*_link) == nullptr; }
protected:
friend class IntrusiveForwardList;
explicit const_iterator(link_type const* link) : _link(link) { }
link_type const* _link;
};
class iterator : public const_iterator
{
using const_iterator::const_iterator;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = IntrusiveForwardList::value_type;
using difference_type = ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
reference operator*() const { return const_cast<reference>(const_iterator::operator*()); }
pointer operator->() const { return const_cast<pointer>(const_iterator::operator->()); }
iterator& operator++()
{
const_iterator::operator++();
return *this;
}
const_iterator operator++(int)
{
iterator tmp = *this;
const_iterator::operator++();
return tmp;
}
};
iterator begin() { return iterator(std::addressof(_head)); }
const_iterator begin() const { return const_iterator(std::addressof(_head)); }
std::default_sentinel_t end() const { return std::default_sentinel; }
iterator erase(const_iterator itr)
{
link_type* link = const_cast<link_type*>(itr._link);
value_type* elem = link_traits::GetValuePtr(*link);
value_type* next = link_traits::GetValuePtr(elem->*ListLink);
elem->*ListLink = nullptr;
*link = next;
if (!next)
_tail = link;
return iterator(link);
}
private:
link_type _head = nullptr; ///< First item in queue
link_type* _tail = std::addressof(_head); ///< Pointer to intrusive link that will receive next inserted item
};
}
#endif // TRINITYCORE_INTRUSIVE_FORWARD_LIST_H
@@ -9559,6 +9559,7 @@ void Player::SendInitWorldStates(uint32 zoneId, uint32 areaId) const
// Realm-global content, but the client needs it before it can put a countdown on any widget the
// zone it just entered shows.
WorldStateMgr::SendActiveScheduledWorldStateInfo(this);
SendDirectMessage(WorldPackets::Misc::StartLightningStorm(0).Write());
//WowCommunity
}
+1 -1
View File
@@ -390,7 +390,7 @@ void WorldSession::HandleRequestCemeteryList(WorldPackets::Misc::RequestCemetery
if (!it->second.Conditions.Meets(conditionSource))
continue;
graveyardIds.push_back(it->first);
graveyardIds.push_back(it->second.safeLocId);
}
if (graveyardIds.empty())
@@ -803,6 +803,12 @@ WorldPacket const* SplashScreenShowLatest::Write()
return &_worldPacket;
}
// Placed here to suppress false positive GCC warning
// warning: 'void operator delete(void*)' called on unallocated object 'displayToast' [-Wfree-nonheap-object]
DisplayToast::DisplayToast() : ServerPacket(SMSG_DISPLAY_TOAST)
{
}
WorldPacket const* DisplayToast::Write()
{
_worldPacket << uint64(Quantity);
@@ -1117,6 +1123,19 @@ WorldPacket const* DisplayWorldText::Write()
return &_worldPacket;
}
WorldPacket const* StartLightningStorm::Write()
{
_worldPacket << uint32(LightningStormId);
return &_worldPacket;
}
WorldPacket const* EndLightningStorm::Write()
{
_worldPacket << uint32(LightningStormId);
return &_worldPacket;
}
}
+28 -2
View File
@@ -1028,7 +1028,7 @@ namespace WorldPackets
class DisplayToast final : public ServerPacket
{
public:
explicit DisplayToast() : ServerPacket(SMSG_DISPLAY_TOAST) { }
explicit DisplayToast();
WorldPacket const* Write() override;
@@ -1543,7 +1543,7 @@ namespace WorldPackets
};
// Floats a server-authored, already-formatted string in the 3D world (the
// engine behind the Lua AddWorldText / AddCustomWorldText bindings) — not a
// engine behind the Lua AddWorldText / AddCustomWorldText bindings) ? not a
// chat line or combat-text entry. The client formats the string tokens with
// Arg1/Arg2 and raises no Lua event, so the display string is entirely the
// server's to compose. Retail uses it for "+XP", "+Gold" and "+Neighborly"
@@ -1567,6 +1567,32 @@ namespace WorldPackets
uint32 Arg2 = 0;
std::string Text;
};
class StartLightningStorm final : public ServerPacket
{
public:
explicit StartLightningStorm() : ServerPacket(SMSG_START_LIGHTNING_STORM, 4) {}
explicit StartLightningStorm(uint32 lightningStormId) : ServerPacket(SMSG_START_LIGHTNING_STORM, 4),
LightningStormId(lightningStormId) {
}
WorldPacket const* Write() override;
uint32 LightningStormId = 0;
};
class EndLightningStorm final : public ServerPacket
{
public:
explicit EndLightningStorm() : ServerPacket(SMSG_END_LIGHTNING_STORM, 4) {}
explicit EndLightningStorm(uint32 lightningStormId) : ServerPacket(SMSG_END_LIGHTNING_STORM, 4),
LightningStormId(lightningStormId) {
}
WorldPacket const* Write() override;
uint32 LightningStormId = 0;
};
}
}
@@ -150,7 +150,26 @@ namespace WorldPackets
template <typename T, std::size_t N, bool IsLarge>
struct ArrayAllocatorTraits
{
using allocator_type = short_alloc::short_alloc<T, (N * sizeof(T) + (alignof(std::max_align_t) - 1)) & ~(alignof(std::max_align_t) - 1)>;
static constexpr std::size_t Align = alignof(std::max_align_t);
static constexpr std::size_t Bytes = (N * sizeof(T) + (Align - 1)) & ~(Align - 1);
struct allocator_type : public short_alloc::short_alloc<T, Bytes, Align>
{
using short_alloc::short_alloc<T, Bytes, Align>::short_alloc;
template <typename Other>
struct rebind
{
// this is intended for debug mode msvc container proxy
struct rebound_allocator : public std::allocator<Other>
{
using std::allocator<Other>::allocator;
rebound_allocator(allocator_type const&) { }
};
using other = std::conditional_t<std::is_same_v<T, Other>, allocator_type, rebound_allocator>;
};
};
using resource_type = typename allocator_type::arena_type;
using reserve_max_capacity = std::true_type;
};
+2 -2
View File
@@ -1539,7 +1539,7 @@ void OpcodeTable::InitializeServerOpcodes()
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENCHANTMENT_LOG, STATUS_NEVER, CONNECTION_TYPE_INSTANCE);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENCOUNTER_END, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENCOUNTER_START, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_END_LIGHTNING_STORM, STATUS_UNHANDLED, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_END_LIGHTNING_STORM, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENTER_ENCRYPTED_MODE, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENUM_CHARACTERS_RESULT, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_ENUM_VAS_PURCHASE_STATES_RESPONSE, STATUS_NEVER, CONNECTION_TYPE_REALM);
@@ -2463,7 +2463,7 @@ void OpcodeTable::InitializeServerOpcodes()
DEFINE_SERVER_OPCODE_HANDLER(SMSG_STARTER_BUILD_ACTIVATE_FAILED, STATUS_UNHANDLED, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_ELAPSED_TIMER, STATUS_NEVER, CONNECTION_TYPE_INSTANCE);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_ELAPSED_TIMERS, STATUS_NEVER, CONNECTION_TYPE_INSTANCE);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_LIGHTNING_STORM, STATUS_UNHANDLED, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_LIGHTNING_STORM, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_LOOT_ROLL, STATUS_NEVER, CONNECTION_TYPE_INSTANCE);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_MIRROR_TIMER, STATUS_NEVER, CONNECTION_TYPE_REALM);
DEFINE_SERVER_OPCODE_HANDLER(SMSG_START_TIMER, STATUS_NEVER, CONNECTION_TYPE_REALM);
+209
View File
@@ -0,0 +1,209 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tc_catch2.h"
#include "IntrusiveForwardList.h"
struct ListItem;
struct ListItemLinkBase
{
ListItem* Link = nullptr; // inheritance intrusive link
};
struct ListItem : ListItemLinkBase
{
std::size_t Id = 0;
std::atomic<ListItem*> AtomicLink = nullptr; // direct member intrusive link
};
static std::array<ListItem, 5> GenerateData()
{
return {{ { .Id = 1 }, { .Id = 2 }, { .Id = 3 }, { .Id = 4 }, { .Id = 5 } }};
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList default constructed is empty", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
STATIC_REQUIRE(std::ranges::range<Trinity::Containers::IntrusiveForwardList<ListLink>> == true);
Trinity::Containers::IntrusiveForwardList<ListLink> list;
REQUIRE(list.empty() == true);
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList links and unlinks items", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
std::array<ListItem, 5> items = GenerateData();
Trinity::Containers::IntrusiveForwardList<ListLink> list;
for (ListItem& item : items)
list.push_back(item);
SECTION("Insertion succeeds")
{
REQUIRE(list.empty() == false);
for (std::size_t i = 0; i < items.size() - 1; ++i)
REQUIRE(items[i].*ListLink != nullptr);
// last item should have no "next"
REQUIRE(items.back().*ListLink == nullptr);
}
SECTION("Move transferts all items")
{
Trinity::Containers::IntrusiveForwardList<ListLink> list2 = std::move(list);
REQUIRE(list.empty() == true);
REQUIRE(list2.empty() == false);
}
SECTION("Items are retrieved in the same order as they were inserted")
{
for (std::size_t i = 0; i < items.size(); ++i)
{
REQUIRE(list.empty() == false);
REQUIRE(list.front().Id == (i + 1));
list.pop_front();
REQUIRE(items[i].*ListLink == nullptr);
}
REQUIRE(list.empty() == true);
}
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList allows insertion at front", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
std::array<ListItem, 5> items = GenerateData();
Trinity::Containers::IntrusiveForwardList<ListLink> list;
for (ListItem& item : items)
list.push_front(item);
for (std::size_t i = items.size(); i > 0; --i)
{
REQUIRE(list.empty() == false);
REQUIRE(list.front().Id == i);
list.pop_front();
REQUIRE(items[i - 1].*ListLink == nullptr);
}
REQUIRE(list.empty() == true);
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList destruction unlinks all items", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
std::array<ListItem, 5> items = GenerateData();
{
Trinity::Containers::IntrusiveForwardList<ListLink> list;
for (ListItem& item : items)
list.push_back(item);
}
for (ListItem& item : items)
REQUIRE(item.*ListLink == nullptr);
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList iterators empty begin == end", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
Trinity::Containers::IntrusiveForwardList<ListLink> list;
REQUIRE(list.begin() == list.end());
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList iterators allow looping over entire list", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
std::array<ListItem, 5> items = GenerateData();
Trinity::Containers::IntrusiveForwardList<ListLink> list;
for (ListItem& item : items)
list.push_back(item);
auto itemsItr = items.begin(), itemsEnd = items.end();
auto listItr = list.begin();
auto listEnd = list.end();
while (listItr != listEnd)
{
REQUIRE(listItr->Id == itemsItr->Id);
++itemsItr;
++listItr;
}
REQUIRE(itemsItr == itemsEnd);
}
TEMPLATE_TEST_CASE_SIG("IntrusiveForwardList erase links item adjacent to removed item", "[IntrusiveForwardList]", ((auto ListLink), ListLink),
&ListItemLinkBase::Link, &ListItem::AtomicLink)
{
std::array<ListItem, 5> items = GenerateData();
Trinity::Containers::IntrusiveForwardList<ListLink> list;
for (std::size_t i = 0; i < 4; ++i)
list.push_back(items[i]);
auto listItr = list.begin();
// erase in the middle
std::advance(listItr, 1);
REQUIRE(listItr->Id == 2);
auto newItr = list.erase(listItr);
REQUIRE(newItr->Id == 3);
REQUIRE(std::ranges::distance(list) == 3);
// erase first
listItr = list.begin();
REQUIRE(listItr->Id == 1);
newItr = list.erase(listItr);
REQUIRE(newItr->Id == 3);
REQUIRE(list.front().Id == 3);
REQUIRE(std::ranges::distance(list) == 2);
// erase last
listItr = list.begin();
std::advance(listItr, 1);
REQUIRE(listItr->Id == 4);
newItr = list.erase(listItr);
REQUIRE(newItr == list.end());
REQUIRE(std::ranges::distance(list) == 1);
// erase only remaining element
listItr = list.begin();
REQUIRE(listItr->Id == 3);
newItr = list.erase(listItr);
REQUIRE(newItr == list.end());
REQUIRE(list.empty());
for (ListItem& item : items)
REQUIRE(item.*ListLink == nullptr);
}