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_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_COUNTING_IMPL_FOR_TEST_H_ 6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_COUNTING_IMPL_FOR_TEST_H_ 7 8 #include <climits> 9 10 #include "partition_alloc/pointers/raw_ptr.h" 11 #include "partition_alloc/pointers/raw_ptr_noop_impl.h" 12 13 namespace base::test { 14 15 // Provides a raw_ptr/raw_ref implementation that performs accounting for test 16 // purposes. It performs extra bookkeeping, e.g. to track the number of times 17 // the raw_ptr is wrapped, unrwapped, etc. 18 // 19 // Test only. 20 struct RawPtrCountingImplForTest : public base::internal::RawPtrNoOpImpl { 21 using SuperImpl = base::internal::RawPtrNoOpImpl; 22 23 static constexpr bool kMustZeroOnConstruct = false; 24 static constexpr bool kMustZeroOnMove = false; 25 static constexpr bool kMustZeroOnDestruct = false; 26 27 template <typename T> WrapRawPtrRawPtrCountingImplForTest28 PA_ALWAYS_INLINE static T* WrapRawPtr(T* ptr) { 29 ++wrap_raw_ptr_cnt; 30 return SuperImpl::WrapRawPtr(ptr); 31 } 32 33 template <typename T> ReleaseWrappedPtrRawPtrCountingImplForTest34 PA_ALWAYS_INLINE static void ReleaseWrappedPtr(T* ptr) { 35 ++release_wrapped_ptr_cnt; 36 SuperImpl::ReleaseWrappedPtr(ptr); 37 } 38 39 template <typename T> SafelyUnwrapPtrForDereferenceRawPtrCountingImplForTest40 PA_ALWAYS_INLINE static T* SafelyUnwrapPtrForDereference(T* wrapped_ptr) { 41 ++get_for_dereference_cnt; 42 return SuperImpl::SafelyUnwrapPtrForDereference(wrapped_ptr); 43 } 44 45 template <typename T> SafelyUnwrapPtrForExtractionRawPtrCountingImplForTest46 PA_ALWAYS_INLINE static T* SafelyUnwrapPtrForExtraction(T* wrapped_ptr) { 47 ++get_for_extraction_cnt; 48 return SuperImpl::SafelyUnwrapPtrForExtraction(wrapped_ptr); 49 } 50 51 template <typename T> UnsafelyUnwrapPtrForComparisonRawPtrCountingImplForTest52 PA_ALWAYS_INLINE static T* UnsafelyUnwrapPtrForComparison(T* wrapped_ptr) { 53 ++get_for_comparison_cnt; 54 return SuperImpl::UnsafelyUnwrapPtrForComparison(wrapped_ptr); 55 } 56 IncrementSwapCountForTestRawPtrCountingImplForTest57 PA_ALWAYS_INLINE static void IncrementSwapCountForTest() { 58 ++wrapped_ptr_swap_cnt; 59 } 60 IncrementLessCountForTestRawPtrCountingImplForTest61 PA_ALWAYS_INLINE static void IncrementLessCountForTest() { 62 ++wrapped_ptr_less_cnt; 63 } 64 65 template <typename T> WrapRawPtrForDuplicationRawPtrCountingImplForTest66 PA_ALWAYS_INLINE static T* WrapRawPtrForDuplication(T* ptr) { 67 ++wrap_raw_ptr_for_dup_cnt; 68 return SuperImpl::WrapRawPtrForDuplication(ptr); 69 } 70 71 template <typename T> UnsafelyUnwrapPtrForDuplicationRawPtrCountingImplForTest72 PA_ALWAYS_INLINE static T* UnsafelyUnwrapPtrForDuplication(T* wrapped_ptr) { 73 ++get_for_duplication_cnt; 74 return SuperImpl::UnsafelyUnwrapPtrForDuplication(wrapped_ptr); 75 } 76 ClearCountersRawPtrCountingImplForTest77 static void ClearCounters() { 78 wrap_raw_ptr_cnt = 0; 79 release_wrapped_ptr_cnt = 0; 80 get_for_dereference_cnt = 0; 81 get_for_extraction_cnt = 0; 82 get_for_comparison_cnt = 0; 83 wrapped_ptr_swap_cnt = 0; 84 wrapped_ptr_less_cnt = 0; 85 pointer_to_member_operator_cnt = 0; 86 wrap_raw_ptr_for_dup_cnt = 0; 87 get_for_duplication_cnt = 0; 88 } 89 90 static inline int wrap_raw_ptr_cnt = INT_MIN; 91 static inline int release_wrapped_ptr_cnt = INT_MIN; 92 static inline int get_for_dereference_cnt = INT_MIN; 93 static inline int get_for_extraction_cnt = INT_MIN; 94 static inline int get_for_comparison_cnt = INT_MIN; 95 static inline int wrapped_ptr_swap_cnt = INT_MIN; 96 static inline int wrapped_ptr_less_cnt = INT_MIN; 97 static inline int pointer_to_member_operator_cnt = INT_MIN; 98 static inline int wrap_raw_ptr_for_dup_cnt = INT_MIN; 99 static inline int get_for_duplication_cnt = INT_MIN; 100 }; 101 102 } // namespace base::test 103 104 #endif // BASE_ALLOCATOR_PARTITION_ALLOCATOR_SRC_PARTITION_ALLOC_POINTERS_RAW_PTR_COUNTING_IMPL_FOR_TEST_H_ 105