From f01793ad4ac35f7d0bb2566f2c17981cd3db2f5d Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 11 Sep 2023 18:48:21 +0200 Subject: [PATCH] util/Intrusive*: move `constant_time_size` to an options struct This makes it easier to add more options later. --- src/Partition.hxx | 2 +- src/RemoteTagCache.hxx | 18 ++++++++---------- src/client/List.hxx | 11 +++++------ src/output/plugins/httpd/HttpdInternal.hxx | 10 ++++------ src/util/IntrusiveForwardList.hxx | 10 ++++++++-- src/util/IntrusiveHashSet.hxx | 8 +++++++- src/util/IntrusiveList.hxx | 10 ++++++++-- src/util/IntrusiveSortedList.hxx | 6 +++--- 8 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/Partition.hxx b/src/Partition.hxx index 237232810..59235c370 100644 --- a/src/Partition.hxx +++ b/src/Partition.hxx @@ -49,7 +49,7 @@ struct Partition final : QueueListener, PlayerListener, MixerListener { std::unique_ptr listener; - IntrusiveList clients; + IntrusiveList clients; /** * Monitor for idle events local to this partition. diff --git a/src/RemoteTagCache.hxx b/src/RemoteTagCache.hxx index 96e59c0f2..fdaff2770 100644 --- a/src/RemoteTagCache.hxx +++ b/src/RemoteTagCache.hxx @@ -1,8 +1,7 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef MPD_REMOTE_TAG_CACHE_HXX -#define MPD_REMOTE_TAG_CACHE_HXX +#pragma once #include "input/RemoteTagScanner.hxx" #include "tag/Tag.hxx" @@ -84,12 +83,13 @@ class RemoteTagCache final { */ ItemList invoke_list; - IntrusiveHashSet, - std::equal_to, - Item::GetUri>, - IntrusiveHashSetBaseHookTraits, - true> map; + IntrusiveHashSet< + Item, 127, + IntrusiveHashSetOperators, + std::equal_to, + Item::GetUri>, + IntrusiveHashSetBaseHookTraits, + IntrusiveHashSetOptions{.constant_time_size = true}> map; public: RemoteTagCache(EventLoop &event_loop, @@ -107,5 +107,3 @@ private: void ItemResolved(Item &item) noexcept; }; - -#endif diff --git a/src/client/List.hxx b/src/client/List.hxx index f5af653b6..6ee636035 100644 --- a/src/client/List.hxx +++ b/src/client/List.hxx @@ -1,15 +1,16 @@ // SPDX-License-Identifier: GPL-2.0-or-later // Copyright The Music Player Daemon Project -#ifndef MPD_CLIENT_LIST_HXX -#define MPD_CLIENT_LIST_HXX +#pragma once #include "Client.hxx" #include "util/IntrusiveList.hxx" class ClientList { - using List = - IntrusiveList, true>; + using List = IntrusiveList< + Client, + IntrusiveListMemberHookTraits<&Client::list_siblings>, + IntrusiveListOptions{.constant_time_size = true}>; const unsigned max_size; @@ -39,5 +40,3 @@ public: void Remove(Client &client) noexcept; }; - -#endif diff --git a/src/output/plugins/httpd/HttpdInternal.hxx b/src/output/plugins/httpd/HttpdInternal.hxx index 67eefe079..476508546 100644 --- a/src/output/plugins/httpd/HttpdInternal.hxx +++ b/src/output/plugins/httpd/HttpdInternal.hxx @@ -6,8 +6,7 @@ * Internal declarations for the "httpd" audio output plugin. */ -#ifndef MPD_OUTPUT_HTTPD_INTERNAL_H -#define MPD_OUTPUT_HTTPD_INTERNAL_H +#pragma once #include "HttpdClient.hxx" #include "output/Interface.hxx" @@ -121,8 +120,9 @@ private: * A linked list containing all clients which are currently * connected. */ - IntrusiveList, - true> clients; + IntrusiveList< + HttpdClient, IntrusiveListBaseHookTraits, + IntrusiveListOptions{.constant_time_size = true}> clients; /** * The maximum number of clients connected at the same time. @@ -255,5 +255,3 @@ private: }; extern const class Domain httpd_output_domain; - -#endif diff --git a/src/util/IntrusiveForwardList.hxx b/src/util/IntrusiveForwardList.hxx index 09e852dbe..2f3302064 100644 --- a/src/util/IntrusiveForwardList.hxx +++ b/src/util/IntrusiveForwardList.hxx @@ -14,6 +14,10 @@ #include #include +struct IntrusiveForwardListOptions { + bool constant_time_size = false; +}; + struct IntrusiveForwardListNode { IntrusiveForwardListNode *next; }; @@ -93,8 +97,10 @@ struct IntrusiveForwardListMemberHookTraits { */ template, - bool constant_time_size=false> + IntrusiveForwardListOptions options=IntrusiveForwardListOptions{}> class IntrusiveForwardList { + static constexpr bool constant_time_size = options.constant_time_size; + IntrusiveForwardListNode head{nullptr}; [[no_unique_address]] @@ -145,7 +151,7 @@ public: :head(src.head) { // shallow copies mess with the counter - static_assert(!constant_time_size); + static_assert(!options.constant_time_size); } IntrusiveForwardList &operator=(IntrusiveForwardList &&src) noexcept { diff --git a/src/util/IntrusiveHashSet.hxx b/src/util/IntrusiveHashSet.hxx index a1a252352..9bb535f80 100644 --- a/src/util/IntrusiveHashSet.hxx +++ b/src/util/IntrusiveHashSet.hxx @@ -10,6 +10,10 @@ #include #include // for std::accumulate() +struct IntrusiveHashSetOptions { + bool constant_time_size = false; +}; + template struct IntrusiveHashSetHook { using SiblingsHook = IntrusiveListHook; @@ -106,8 +110,10 @@ struct IntrusiveHashSetOperators { template, - bool constant_time_size=false> + IntrusiveHashSetOptions options=IntrusiveHashSetOptions{}> class IntrusiveHashSet { + static constexpr bool constant_time_size = options.constant_time_size; + [[no_unique_address]] OptionalCounter counter; diff --git a/src/util/IntrusiveList.hxx b/src/util/IntrusiveList.hxx index aa1c67e55..54cab6933 100644 --- a/src/util/IntrusiveList.hxx +++ b/src/util/IntrusiveList.hxx @@ -13,6 +13,10 @@ #include #include +struct IntrusiveListOptions { + bool constant_time_size = false; +}; + struct IntrusiveListNode { IntrusiveListNode *next, *prev; @@ -27,7 +31,7 @@ template class IntrusiveListHook { template friend struct IntrusiveListBaseHookTraits; template friend struct IntrusiveListMemberHookTraits; - template friend class IntrusiveList; + template friend class IntrusiveList; protected: IntrusiveListNode siblings; @@ -135,8 +139,10 @@ struct IntrusiveListMemberHookTraits { */ template, - bool constant_time_size=false> + IntrusiveListOptions options=IntrusiveListOptions{}> class IntrusiveList { + static constexpr bool constant_time_size = options.constant_time_size; + IntrusiveListNode head{&head, &head}; [[no_unique_address]] diff --git a/src/util/IntrusiveSortedList.hxx b/src/util/IntrusiveSortedList.hxx index cd029aa52..4cef8c099 100644 --- a/src/util/IntrusiveSortedList.hxx +++ b/src/util/IntrusiveSortedList.hxx @@ -14,11 +14,11 @@ */ template, - bool constant_time_size=false> + IntrusiveListOptions options=IntrusiveListOptions{}> class IntrusiveSortedList - : public IntrusiveList + : public IntrusiveList { - using Base = IntrusiveList; + using Base = IntrusiveList; [[no_unique_address]] Compare compare;