1 // Copyright 2023 The Chromium Authors 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_NOTIFICATION_DATA_H_ 6 #define BASE_ALLOCATOR_DISPATCHER_NOTIFICATION_DATA_H_ 7 8 #include <cstdint> 9 10 #include "base/allocator/dispatcher/memory_tagging.h" 11 #include "base/allocator/dispatcher/subsystem.h" 12 #include "base/base_export.h" 13 #include "partition_alloc/partition_alloc_buildflags.h" 14 15 namespace base::allocator::dispatcher { 16 17 // Definitions of the parameter structures passed to the observer hooks. They 18 // are similar to the structures defined by PartitionAllocator but provide 19 // further information. 20 21 // The notification data for the allocation path. 22 class BASE_EXPORT AllocationNotificationData { 23 public: AllocationNotificationData(void * address,size_t size,const char * type_name,AllocationSubsystem allocation_subsystem)24 constexpr AllocationNotificationData(void* address, 25 size_t size, 26 const char* type_name, 27 AllocationSubsystem allocation_subsystem) 28 : address_(address), 29 size_(size), 30 type_name_(type_name), 31 allocation_subsystem_(allocation_subsystem) {} 32 address()33 constexpr void* address() const { return address_; } 34 size()35 constexpr size_t size() const { return size_; } 36 type_name()37 constexpr const char* type_name() const { return type_name_; } 38 allocation_subsystem()39 constexpr AllocationSubsystem allocation_subsystem() const { 40 return allocation_subsystem_; 41 } 42 43 // In the allocation observer path, it's interesting which reporting mode is 44 // enabled. 45 #if BUILDFLAG(HAS_MEMORY_TAGGING) SetMteReportingMode(MTEMode mode)46 constexpr AllocationNotificationData& SetMteReportingMode(MTEMode mode) { 47 mte_reporting_mode_ = mode; 48 return *this; 49 } 50 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 51 mte_reporting_mode()52 constexpr MTEMode mte_reporting_mode() const { 53 #if BUILDFLAG(HAS_MEMORY_TAGGING) 54 return mte_reporting_mode_; 55 #else 56 return MTEMode::kUndefined; 57 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 58 } 59 60 private: 61 void* address_ = nullptr; 62 size_t size_ = 0; 63 const char* type_name_ = nullptr; 64 #if BUILDFLAG(HAS_MEMORY_TAGGING) 65 MTEMode mte_reporting_mode_ = MTEMode::kUndefined; 66 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 67 AllocationSubsystem allocation_subsystem_; 68 }; 69 70 // The notification data for the free path. 71 class BASE_EXPORT FreeNotificationData { 72 public: FreeNotificationData(void * address,AllocationSubsystem allocation_subsystem)73 constexpr explicit FreeNotificationData( 74 void* address, 75 AllocationSubsystem allocation_subsystem) 76 : address_(address), allocation_subsystem_(allocation_subsystem) {} 77 address()78 constexpr void* address() const { return address_; } 79 allocation_subsystem()80 constexpr AllocationSubsystem allocation_subsystem() const { 81 return allocation_subsystem_; 82 } 83 84 // In the free observer path, it's interesting which reporting mode is 85 // enabled. 86 #if BUILDFLAG(HAS_MEMORY_TAGGING) SetMteReportingMode(MTEMode mode)87 constexpr FreeNotificationData& SetMteReportingMode(MTEMode mode) { 88 mte_reporting_mode_ = mode; 89 return *this; 90 } 91 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 92 mte_reporting_mode()93 constexpr MTEMode mte_reporting_mode() const { 94 #if BUILDFLAG(HAS_MEMORY_TAGGING) 95 return mte_reporting_mode_; 96 #else 97 return MTEMode::kUndefined; 98 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 99 } 100 101 private: 102 void* address_ = nullptr; 103 #if BUILDFLAG(HAS_MEMORY_TAGGING) 104 MTEMode mte_reporting_mode_ = MTEMode::kUndefined; 105 #endif // BUILDFLAG(HAS_MEMORY_TAGGING) 106 AllocationSubsystem allocation_subsystem_; 107 }; 108 109 } // namespace base::allocator::dispatcher 110 #endif // BASE_ALLOCATOR_DISPATCHER_NOTIFICATION_DATA_H_ 111