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 PARTITION_ALLOC_PARTITION_ALLOC_FOR_TESTING_H_ 6 #define PARTITION_ALLOC_PARTITION_ALLOC_FOR_TESTING_H_ 7 8 #include "partition_alloc/partition_alloc.h" 9 10 namespace partition_alloc { 11 namespace internal { 12 13 constexpr bool AllowLeaks = true; 14 constexpr bool DisallowLeaks = false; 15 16 // A subclass of PartitionAllocator for testing. It will free all resources, 17 // i.e. allocated memory, memory inside freelist, and so on, when destructing 18 // it or when manually invoking reset(). 19 // If need to check if there are any memory allocated but not freed yet, 20 // use allow_leaks=false. We will see CHECK failure inside reset() if any 21 // leak is detected. Otherwise (e.g. intentional leaks), use allow_leaks=true. 22 template <bool allow_leaks> 23 struct PartitionAllocatorForTesting : public PartitionAllocator { PartitionAllocatorForTestingPartitionAllocatorForTesting24 PartitionAllocatorForTesting() : PartitionAllocator() {} 25 PartitionAllocatorForTestingPartitionAllocatorForTesting26 explicit PartitionAllocatorForTesting(PartitionOptions opts) 27 : PartitionAllocator(opts) {} 28 ~PartitionAllocatorForTestingPartitionAllocatorForTesting29 ~PartitionAllocatorForTesting() { reset(); } 30 resetPartitionAllocatorForTesting31 PA_ALWAYS_INLINE void reset() { 32 PartitionAllocator::root()->ResetForTesting(allow_leaks); 33 } 34 }; 35 36 } // namespace internal 37 38 using PartitionAllocatorForTesting = 39 internal::PartitionAllocatorForTesting<internal::DisallowLeaks>; 40 41 using PartitionAllocatorAllowLeaksForTesting = 42 internal::PartitionAllocatorForTesting<internal::AllowLeaks>; 43 44 } // namespace partition_alloc 45 46 #endif // PARTITION_ALLOC_PARTITION_ALLOC_FOR_TESTING_H_ 47