1*6777b538SAndroid Build Coastguard Worker // Copyright 2022 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker #ifndef BASE_ALLOCATOR_DISPATCHER_DISPATCHER_H_ 6*6777b538SAndroid Build Coastguard Worker #define BASE_ALLOCATOR_DISPATCHER_DISPATCHER_H_ 7*6777b538SAndroid Build Coastguard Worker 8*6777b538SAndroid Build Coastguard Worker #include "base/allocator/dispatcher/internal/dispatcher_internal.h" 9*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 10*6777b538SAndroid Build Coastguard Worker 11*6777b538SAndroid Build Coastguard Worker #include <memory> 12*6777b538SAndroid Build Coastguard Worker 13*6777b538SAndroid Build Coastguard Worker namespace base::allocator::dispatcher { 14*6777b538SAndroid Build Coastguard Worker 15*6777b538SAndroid Build Coastguard Worker namespace internal { 16*6777b538SAndroid Build Coastguard Worker struct DispatchData; 17*6777b538SAndroid Build Coastguard Worker } 18*6777b538SAndroid Build Coastguard Worker 19*6777b538SAndroid Build Coastguard Worker // Dispatcher serves as the top level instance for managing the dispatch 20*6777b538SAndroid Build Coastguard Worker // mechanism. The class instance manages connections to the various memory 21*6777b538SAndroid Build Coastguard Worker // subsystems such as PartitionAlloc. To keep the public interface as lean as 22*6777b538SAndroid Build Coastguard Worker // possible it uses a pimpl pattern. 23*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT Dispatcher { 24*6777b538SAndroid Build Coastguard Worker public: 25*6777b538SAndroid Build Coastguard Worker static Dispatcher& GetInstance(); 26*6777b538SAndroid Build Coastguard Worker 27*6777b538SAndroid Build Coastguard Worker Dispatcher(); 28*6777b538SAndroid Build Coastguard Worker 29*6777b538SAndroid Build Coastguard Worker // Initialize the dispatch mechanism with the given tuple of observers. The 30*6777b538SAndroid Build Coastguard Worker // observers must be valid (it is only DCHECKed internally at initialization, 31*6777b538SAndroid Build Coastguard Worker // but not verified further) 32*6777b538SAndroid Build Coastguard Worker // If Initialize is called multiple times, the first one wins. All later 33*6777b538SAndroid Build Coastguard Worker // invocations are silently ignored. Initialization is protected from 34*6777b538SAndroid Build Coastguard Worker // concurrent invocations. In case of concurrent accesses, the first one to 35*6777b538SAndroid Build Coastguard Worker // get the lock wins. 36*6777b538SAndroid Build Coastguard Worker // The dispatcher invokes following functions on the observers: 37*6777b538SAndroid Build Coastguard Worker // void OnAllocation(void* address, 38*6777b538SAndroid Build Coastguard Worker // size_t size, 39*6777b538SAndroid Build Coastguard Worker // AllocationSubsystem sub_system, 40*6777b538SAndroid Build Coastguard Worker // const char* type_name); 41*6777b538SAndroid Build Coastguard Worker // void OnFree(void* address); 42*6777b538SAndroid Build Coastguard Worker // 43*6777b538SAndroid Build Coastguard Worker // Note: The dispatcher mechanism does NOT bring systematic protection against 44*6777b538SAndroid Build Coastguard Worker // recursive invocations. That is, observers which allocate memory on the 45*6777b538SAndroid Build Coastguard Worker // heap, i.e. through dynamically allocated containers or by using the 46*6777b538SAndroid Build Coastguard Worker // CHECK-macro, are responsible to break these recursions! 47*6777b538SAndroid Build Coastguard Worker template <typename... ObserverTypes> Initialize(const std::tuple<ObserverTypes...> & observers)48*6777b538SAndroid Build Coastguard Worker void Initialize(const std::tuple<ObserverTypes...>& observers) { 49*6777b538SAndroid Build Coastguard Worker // Get the hooks for running these observers and pass them to further 50*6777b538SAndroid Build Coastguard Worker // initialization 51*6777b538SAndroid Build Coastguard Worker Initialize(internal::GetNotificationHooks(observers)); 52*6777b538SAndroid Build Coastguard Worker } 53*6777b538SAndroid Build Coastguard Worker 54*6777b538SAndroid Build Coastguard Worker // The following functions provide an interface to setup and tear down the 55*6777b538SAndroid Build Coastguard Worker // dispatcher when testing. This must NOT be used from production code since 56*6777b538SAndroid Build Coastguard Worker // the hooks cannot be removed reliably under all circumstances. 57*6777b538SAndroid Build Coastguard Worker template <typename ObserverType> InitializeForTesting(ObserverType * observer)58*6777b538SAndroid Build Coastguard Worker void InitializeForTesting(ObserverType* observer) { 59*6777b538SAndroid Build Coastguard Worker Initialize(std::make_tuple(observer)); 60*6777b538SAndroid Build Coastguard Worker } 61*6777b538SAndroid Build Coastguard Worker 62*6777b538SAndroid Build Coastguard Worker void ResetForTesting(); 63*6777b538SAndroid Build Coastguard Worker 64*6777b538SAndroid Build Coastguard Worker private: 65*6777b538SAndroid Build Coastguard Worker // structure and pointer to the private implementation. 66*6777b538SAndroid Build Coastguard Worker struct Impl; 67*6777b538SAndroid Build Coastguard Worker std::unique_ptr<Impl> const impl_; 68*6777b538SAndroid Build Coastguard Worker 69*6777b538SAndroid Build Coastguard Worker ~Dispatcher(); 70*6777b538SAndroid Build Coastguard Worker 71*6777b538SAndroid Build Coastguard Worker void Initialize(const internal::DispatchData& dispatch_data); 72*6777b538SAndroid Build Coastguard Worker }; 73*6777b538SAndroid Build Coastguard Worker } // namespace base::allocator::dispatcher 74*6777b538SAndroid Build Coastguard Worker 75*6777b538SAndroid Build Coastguard Worker #endif // BASE_ALLOCATOR_DISPATCHER_DISPATCHER_H_ 76