blob: b298815ee85979a18fea5ab67cc7ca9ce7a784e5 [file] [log] [blame]
André Kempe0e4df752022-08-05 09:36:571// Copyright (c) 2022 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef BASE_ALLOCATOR_DISPATCHER_INITIALIZER_H_
6#define BASE_ALLOCATOR_DISPATCHER_INITIALIZER_H_
7
8#include "base/allocator/dispatcher/configuration.h"
9#include "base/allocator/dispatcher/dispatcher.h"
10#include "base/allocator/dispatcher/internal/tools.h"
11
12#include <tuple>
13#include <utility>
14
15namespace base::allocator::dispatcher {
16namespace internal {
17
18// Filter the passed observers and perform initialization of the passed
19// dispatcher.
20template <size_t CurrentIndex,
21 typename DispatcherType,
22 typename CheckObserverPredicate,
23 typename VerifiedObservers,
24 typename UnverifiedObservers,
25 size_t... IndicesToSelect>
26inline void DoInitialize(DispatcherType& dispatcher,
27 CheckObserverPredicate check_observer,
28 const VerifiedObservers& verified_observers,
29 const UnverifiedObservers& unverified_observers,
30 std::index_sequence<IndicesToSelect...> indices) {
31 if constexpr (CurrentIndex < std::tuple_size<UnverifiedObservers>::value) {
32 // We still have some items left to handle.
33 if (check_observer(std::get<CurrentIndex>(unverified_observers))) {
34 // The current observer is valid. Hence, append the index of the current
35 // item to the set of indices and head on to the next item.
36 DoInitialize<CurrentIndex + 1>(
37 dispatcher, check_observer, verified_observers, unverified_observers,
38 std::index_sequence<IndicesToSelect..., CurrentIndex>{});
39 } else {
40 // The current observer is not valid. Hence, head on to the next item with
41 // an unaltered list of indices.
42 DoInitialize<CurrentIndex + 1>(dispatcher, check_observer,
43 verified_observers, unverified_observers,
44 indices);
45 }
46 } else if constexpr (CurrentIndex ==
47 std::tuple_size<UnverifiedObservers>::value) {
48 // So we have met the end of the tuple of observers to verify.
49 // Hence, we extract the additional valid observers, append to the tuple of
50 // already verified observers and hand over to the dispatcher.
51 auto observers = std::tuple_cat(
52 verified_observers,
53 std::make_tuple(std::get<IndicesToSelect>(unverified_observers)...));
54
55 // Do a final check that neither the maximum total number of observers nor
56 // the maximum number of optional observers is exceeded.
57 static_assert(std::tuple_size<decltype(observers)>::value <=
58 configuration::kMaximumNumberOfObservers);
59 static_assert(sizeof...(IndicesToSelect) <=
60 configuration::kMaximumNumberOfOptionalObservers);
61
62 dispatcher.Initialize(std::move(observers));
63 }
64}
65
66} // namespace internal
67
68// The result of concatenating two tuple-types.
69template <typename... tuples>
70using TupleCat = decltype(std::tuple_cat(std::declval<tuples>()...));
71
72// Initializer collects mandatory and optional observers and initializes the
73// passed Dispatcher with only the enabled observers.
74//
75// In some situations, presence of observers depends on runtime. i.e. command
76// line parameters or CPU features. With 3 optional observers we already have 8
77// different combinations. Initializer takes the job of dealing with all
78// combinations from the user. It allows users to pass all observers (including
79// nullptr for disabled optional observers) and initializes the Dispatcher with
80// only the enabled observers.
81//
82// Since this process results in a combinatoric explosion, Initializer
83// distinguishes between optional and mandatory observers. Mandatory observers
84// are not included in the filtering process and must always be enabled (not
85// nullptr).
86//
87// To allow the Initializer to track the number and exact type of observers, it
88// is implemented as a templated class which holds information on the types in
89// the std::tuples passed as template parameters. Therefore, whenever any type
90// observer it set, the initializer changes its type to reflect this.
91template <typename MandatoryObservers = std::tuple<>,
92 typename OptionalObservers = std::tuple<>>
93struct BASE_EXPORT Initializer {
94 Initializer() = default;
95 Initializer(MandatoryObservers mandatory_observers,
96 OptionalObservers optional_observers)
97 : mandatory_observers_(std::move(mandatory_observers)),
98 optional_observers_(std::move(optional_observers)) {}
99
100 // Set the mandatory observers. The number of observers that can be set is
101 // limited by configuration::maximum_number_of_observers.
102 template <typename... NewMandatoryObservers,
103 std::enable_if_t<
104 internal::LessEqual((sizeof...(NewMandatoryObservers) +
105 std::tuple_size<OptionalObservers>::value),
106 configuration::kMaximumNumberOfObservers),
107 bool> = true>
108 Initializer<std::tuple<NewMandatoryObservers*...>, OptionalObservers>
109 SetMandatoryObservers(NewMandatoryObservers*... mandatory_observers) const {
110 return {std::make_tuple(mandatory_observers...), GetOptionalObservers()};
111 }
112
113 // Add mandatory observers. The number of observers that can be added is
114 // limited by the current number of observers, see
115 // configuration::maximum_number_of_observers.
116 template <typename... AdditionalMandatoryObservers,
117 std::enable_if_t<internal::LessEqual(
118 std::tuple_size<MandatoryObservers>::value +
119 sizeof...(AdditionalMandatoryObservers) +
120 std::tuple_size<OptionalObservers>::value,
121 configuration::kMaximumNumberOfObservers),
122 bool> = true>
123 Initializer<TupleCat<MandatoryObservers,
124 std::tuple<AdditionalMandatoryObservers*...>>,
125 OptionalObservers>
126 AddMandatoryObservers(
127 AdditionalMandatoryObservers*... additional_mandatory_observers) const {
128 return {std::tuple_cat(GetMandatoryObservers(),
129 std::make_tuple(additional_mandatory_observers...)),
130 GetOptionalObservers()};
131 }
132
133 // Set the optional observers. The number of observers that can be set is
134 // limited by configuration::maximum_number_of_optional_observers as well as
135 // configuration::maximum_number_of_observers.
136 template <
137 typename... NewOptionalObservers,
138 std::enable_if_t<
139 internal::LessEqual(
140 sizeof...(NewOptionalObservers),
141 configuration::kMaximumNumberOfOptionalObservers) &&
142 internal::LessEqual((sizeof...(NewOptionalObservers) +
143 std::tuple_size<MandatoryObservers>::value),
144 configuration::kMaximumNumberOfObservers),
145 bool> = true>
146 Initializer<MandatoryObservers, std::tuple<NewOptionalObservers*...>>
147 SetOptionalObservers(NewOptionalObservers*... optional_observers) const {
148 return {GetMandatoryObservers(), std::make_tuple(optional_observers...)};
149 }
150
151 // Add optional observers. The number of observers that can be added is
152 // limited by the current number of optional observers,
153 // configuration::maximum_number_of_optional_observers as well as
154 // configuration::maximum_number_of_observers.
155 template <
156 typename... AdditionalOptionalObservers,
157 std::enable_if_t<
158 internal::LessEqual(
159 std::tuple_size<OptionalObservers>::value +
160 sizeof...(AdditionalOptionalObservers),
161 configuration::kMaximumNumberOfOptionalObservers) &&
162 internal::LessEqual((std::tuple_size<OptionalObservers>::value +
163 sizeof...(AdditionalOptionalObservers) +
164 std::tuple_size<MandatoryObservers>::value),
165 configuration::kMaximumNumberOfObservers),
166 bool> = true>
167 Initializer<
168 MandatoryObservers,
169 TupleCat<OptionalObservers, std::tuple<AdditionalOptionalObservers*...>>>
170 AddOptionalObservers(
171 AdditionalOptionalObservers*... additional_optional_observers) const {
172 return {GetMandatoryObservers(),
173 std::tuple_cat(GetOptionalObservers(),
174 std::make_tuple(additional_optional_observers...))};
175 }
176
177 // Perform the actual initialization on the passed dispatcher.
178 // The dispatcher is passed as a template only to provide better testability.
179 template <typename DispatcherType>
180 void DoInitialize(DispatcherType& dispatcher) const {
181 internal::DoInitialize<0>(dispatcher, internal::IsValidObserver{},
182 GetMandatoryObservers(), GetOptionalObservers(),
183 {});
184 }
185
186 const MandatoryObservers& GetMandatoryObservers() const {
187 return mandatory_observers_;
188 }
189
190 const OptionalObservers& GetOptionalObservers() const {
191 return optional_observers_;
192 }
193
194 private:
195 MandatoryObservers mandatory_observers_;
196 OptionalObservers optional_observers_;
197};
198
199// Convenience function for creating an empty Initializer.
200inline Initializer<> CreateInitializer() {
201 return {};
202}
203
204} // namespace base::allocator::dispatcher
205
206#endif // BASE_ALLOCATOR_DISPATCHER_INITIALIZER_H_