From a390a1d4770ba3f69a963e8ad23ee822cccc7fac Mon Sep 17 00:00:00 2001 From: Chris Kennelly Date: Mon, 11 Nov 2024 11:51:53 -0800 Subject: [PATCH] Modernize to [[nodiscard]] PiperOrigin-RevId: 695430742 Change-Id: I47f912f0f37fb9d424450c5308d8842d5170bf1c --- tcmalloc/huge_allocator.h | 3 +-- tcmalloc/huge_cache.h | 4 ++-- tcmalloc/huge_page_aware_allocator.h | 9 ++++----- tcmalloc/huge_page_filler.h | 3 +-- tcmalloc/huge_page_filler_fuzz.cc | 2 +- tcmalloc/huge_page_filler_test.cc | 4 ++-- tcmalloc/huge_region_fuzz.cc | 2 +- tcmalloc/internal/cpu_utils.h | 4 ++-- tcmalloc/internal/percpu_tcmalloc.h | 8 ++++---- tcmalloc/metadata_allocator.h | 2 +- tcmalloc/mock_metadata_allocator.h | 2 +- tcmalloc/mock_virtual_allocator.h | 3 +-- tcmalloc/system-alloc.h | 2 +- tcmalloc/transfer_cache.cc | 2 +- tcmalloc/transfer_cache_internals.h | 3 +-- 15 files changed, 24 insertions(+), 29 deletions(-) diff --git a/tcmalloc/huge_allocator.h b/tcmalloc/huge_allocator.h index 02a075b7e..1d5e2b837 100644 --- a/tcmalloc/huge_allocator.h +++ b/tcmalloc/huge_allocator.h @@ -44,8 +44,7 @@ class VirtualAllocator { VirtualAllocator& operator=(VirtualAllocator&&) = delete; // Allocates bytes of virtual address space with align alignment. - ABSL_MUST_USE_RESULT virtual AddressRange operator()(size_t bytes, - size_t align) = 0; + [[nodiscard]] virtual AddressRange operator()(size_t bytes, size_t align) = 0; }; // This tracks available ranges of hugepages and fulfills requests for diff --git a/tcmalloc/huge_cache.h b/tcmalloc/huge_cache.h index e8786f6ea..d34f4c5f1 100644 --- a/tcmalloc/huge_cache.h +++ b/tcmalloc/huge_cache.h @@ -44,8 +44,8 @@ class MemoryModifyFunction { public: virtual ~MemoryModifyFunction() = default; - ABSL_MUST_USE_RESULT virtual bool operator()(Range r) = 0; - ABSL_MUST_USE_RESULT bool operator()(HugeRange r) { + [[nodiscard]] virtual bool operator()(Range r) = 0; + [[nodiscard]] bool operator()(HugeRange r) { return (*this)(Range{r.start().first_page(), r.len().in_pages()}); } }; diff --git a/tcmalloc/huge_page_aware_allocator.h b/tcmalloc/huge_page_aware_allocator.h index d2eeabebe..730161024 100644 --- a/tcmalloc/huge_page_aware_allocator.h +++ b/tcmalloc/huge_page_aware_allocator.h @@ -251,7 +251,7 @@ class HugePageAwareAllocator final : public PageAllocatorInterface { explicit Unback(HugePageAwareAllocator& hpaa ABSL_ATTRIBUTE_LIFETIME_BOUND) : hpaa_(hpaa) {} - ABSL_MUST_USE_RESULT bool operator()(Range r) override + [[nodiscard]] bool operator()(Range r) override ABSL_EXCLUSIVE_LOCKS_REQUIRED(pageheap_lock) { #ifndef NDEBUG pageheap_lock.AssertHeld(); @@ -269,7 +269,7 @@ class HugePageAwareAllocator final : public PageAllocatorInterface { HugePageAwareAllocator& hpaa ABSL_ATTRIBUTE_LIFETIME_BOUND) : hpaa_(hpaa) {} - ABSL_MUST_USE_RESULT bool operator()(Range r) override + [[nodiscard]] bool operator()(Range r) override ABSL_NO_THREAD_SAFETY_ANALYSIS { #ifndef NDEBUG pageheap_lock.AssertHeld(); @@ -296,8 +296,7 @@ class HugePageAwareAllocator final : public PageAllocatorInterface { HugePageAwareAllocator& hpaa ABSL_ATTRIBUTE_LIFETIME_BOUND) : hpaa_(hpaa) {} - ABSL_MUST_USE_RESULT AddressRange operator()(size_t bytes, - size_t align) override + [[nodiscard]] AddressRange operator()(size_t bytes, size_t align) override ABSL_EXCLUSIVE_LOCKS_REQUIRED(pageheap_lock) { return hpaa_.AllocAndReport(bytes, align); } @@ -312,7 +311,7 @@ class HugePageAwareAllocator final : public PageAllocatorInterface { HugePageAwareAllocator& hpaa ABSL_ATTRIBUTE_LIFETIME_BOUND) : hpaa_(hpaa) {} - ABSL_MUST_USE_RESULT void* operator()(size_t bytes) override + [[nodiscard]] void* operator()(size_t bytes) override ABSL_EXCLUSIVE_LOCKS_REQUIRED(pageheap_lock) { return hpaa_.forwarder_.arena().Alloc(bytes); } diff --git a/tcmalloc/huge_page_filler.h b/tcmalloc/huge_page_filler.h index 10bf9e2a1..ae2769f6d 100644 --- a/tcmalloc/huge_page_filler.h +++ b/tcmalloc/huge_page_filler.h @@ -216,8 +216,7 @@ class PageTracker : public TList::Elem { bool has_dense_spans_ = false; - ABSL_MUST_USE_RESULT bool ReleasePages(Range r, - MemoryModifyFunction& unback) { + [[nodiscard]] bool ReleasePages(Range r, MemoryModifyFunction& unback) { bool success = unback(r); if (ABSL_PREDICT_TRUE(success)) { unbroken_ = false; diff --git a/tcmalloc/huge_page_filler_fuzz.cc b/tcmalloc/huge_page_filler_fuzz.cc index 618105303..a4c6eb8f1 100644 --- a/tcmalloc/huge_page_filler_fuzz.cc +++ b/tcmalloc/huge_page_filler_fuzz.cc @@ -56,7 +56,7 @@ absl::flat_hash_set& ReleasedPages() { class MockUnback final : public MemoryModifyFunction { public: - ABSL_MUST_USE_RESULT bool operator()(Range r) override { + [[nodiscard]] bool operator()(Range r) override { if (!unback_success) { return false; } diff --git a/tcmalloc/huge_page_filler_test.cc b/tcmalloc/huge_page_filler_test.cc index 1da2c7963..c9be50640 100644 --- a/tcmalloc/huge_page_filler_test.cc +++ b/tcmalloc/huge_page_filler_test.cc @@ -186,7 +186,7 @@ class PageTrackerTest : public testing::Test { class MockUnbackInterface final : public MemoryModifyFunction { public: - ABSL_MUST_USE_RESULT bool operator()(Range r) override { + [[nodiscard]] bool operator()(Range r) override { TC_CHECK_LT(actual_index_, ABSL_ARRAYSIZE(actual_)); actual_[actual_index_].r = r; TC_CHECK_LT(actual_index_, ABSL_ARRAYSIZE(expected_)); @@ -619,7 +619,7 @@ class BlockingUnback final : public MemoryModifyFunction { public: constexpr BlockingUnback() = default; - ABSL_MUST_USE_RESULT bool operator()(Range r) override { + [[nodiscard]] bool operator()(Range r) override { if (!mu_) { return success_; } diff --git a/tcmalloc/huge_region_fuzz.cc b/tcmalloc/huge_region_fuzz.cc index ed116ed61..e0854e486 100644 --- a/tcmalloc/huge_region_fuzz.cc +++ b/tcmalloc/huge_region_fuzz.cc @@ -35,7 +35,7 @@ namespace { class MockUnback final : public MemoryModifyFunction { public: - ABSL_MUST_USE_RESULT bool operator()(Range r) override { + [[nodiscard]] bool operator()(Range r) override { release_callback_(); if (!unback_success_) { diff --git a/tcmalloc/internal/cpu_utils.h b/tcmalloc/internal/cpu_utils.h index 7c0aa31f4..feed1fa4c 100644 --- a/tcmalloc/internal/cpu_utils.h +++ b/tcmalloc/internal/cpu_utils.h @@ -56,14 +56,14 @@ class CpuSet { // Sets the CPU affinity of the process with the given pid. Returns true if // successful. If returns false, please check the global 'errno' variable to // determine the specific error that occurred. - ABSL_MUST_USE_RESULT bool SetAffinity(pid_t pid) { + [[nodiscard]] bool SetAffinity(pid_t pid) { return sched_setaffinity(pid, kCpuSetBytes, cpu_set_.data()) == 0; } // Gets the CPU affinity of the process with the given pid. Return trues if // successful. If returns false, please check the global 'errno' variable to // determine the specific error that occurred. - ABSL_MUST_USE_RESULT bool GetAffinity(pid_t pid) { + [[nodiscard]] bool GetAffinity(pid_t pid) { return sched_getaffinity(pid, kCpuSetBytes, cpu_set_.data()) == 0; } diff --git a/tcmalloc/internal/percpu_tcmalloc.h b/tcmalloc/internal/percpu_tcmalloc.h index 460ab8ddb..9a1c357fb 100644 --- a/tcmalloc/internal/percpu_tcmalloc.h +++ b/tcmalloc/internal/percpu_tcmalloc.h @@ -150,7 +150,7 @@ class TcmallocSlab { // provides the number of size classes for which the // capacity needs to be updated. // callback drains the old slab. - ABSL_MUST_USE_RESULT ResizeSlabsInfo UpdateMaxCapacities( + [[nodiscard]] ResizeSlabsInfo UpdateMaxCapacities( void* new_slabs, absl::FunctionRef capacity, absl::FunctionRef update_capacity, absl::FunctionRef populated, DrainHandler drain_handler, @@ -168,7 +168,7 @@ class TcmallocSlab { // // Caller must ensure that there are no concurrent calls to InitCpu, // ShrinkOtherCache, or Drain. - ABSL_MUST_USE_RESULT ResizeSlabsInfo ResizeSlabs( + [[nodiscard]] ResizeSlabsInfo ResizeSlabs( Shift new_shift, void* new_slabs, absl::FunctionRef capacity, absl::FunctionRef populated, DrainHandler drain_handler); @@ -200,7 +200,7 @@ class TcmallocSlab { // Remove an item (LIFO) from the current CPU's slab. If the slab is empty, // invokes and returns its result. - ABSL_MUST_USE_RESULT void* Pop(size_t class_size); + [[nodiscard]] void* Pop(size_t size_class); // Add up to items to the current cpu slab from the array located at // . Returns the number of items that were added (possibly 0). All @@ -351,7 +351,7 @@ class TcmallocSlab { // It's important that we use consistent values for slabs/shift rather than // loading from the atomic repeatedly whenever we use one of the values. - ABSL_MUST_USE_RESULT std::pair GetSlabsAndShift( + [[nodiscard]] std::pair GetSlabsAndShift( std::memory_order order) const { return slabs_and_shift_.load(order).Get(); } diff --git a/tcmalloc/metadata_allocator.h b/tcmalloc/metadata_allocator.h index 68d7ebec4..fd9a7a055 100644 --- a/tcmalloc/metadata_allocator.h +++ b/tcmalloc/metadata_allocator.h @@ -32,7 +32,7 @@ class MetadataAllocator { MetadataAllocator& operator=(MetadataAllocator&&) = delete; // Allocates bytes suitable for metadata. - ABSL_MUST_USE_RESULT virtual void* operator()(size_t bytes) = 0; + [[nodiscard]] virtual void* operator()(size_t bytes) = 0; }; } // namespace tcmalloc::tcmalloc_internal diff --git a/tcmalloc/mock_metadata_allocator.h b/tcmalloc/mock_metadata_allocator.h index 2b3555194..1a8ea5576 100644 --- a/tcmalloc/mock_metadata_allocator.h +++ b/tcmalloc/mock_metadata_allocator.h @@ -31,7 +31,7 @@ class FakeMetadataAllocator final : public MetadataAllocator { } } - ABSL_MUST_USE_RESULT void* operator()(size_t size) override { + [[nodiscard]] void* operator()(size_t size) override { void* ptr = malloc(size); metadata_allocs_.push_back(ptr); return ptr; diff --git a/tcmalloc/mock_virtual_allocator.h b/tcmalloc/mock_virtual_allocator.h index 693450fcf..a2773418b 100644 --- a/tcmalloc/mock_virtual_allocator.h +++ b/tcmalloc/mock_virtual_allocator.h @@ -29,8 +29,7 @@ namespace tcmalloc::tcmalloc_internal { class FakeVirtualAllocator final : public VirtualAllocator { public: - ABSL_MUST_USE_RESULT AddressRange operator()(size_t bytes, - size_t align) override; + [[nodiscard]] AddressRange operator()(size_t bytes, size_t align) override; static constexpr size_t kMaxBacking = 1024 * 1024; // This isn't super good form but we'll never have more than one HAT diff --git a/tcmalloc/system-alloc.h b/tcmalloc/system-alloc.h index 76e12cbb7..83fdfcc25 100644 --- a/tcmalloc/system-alloc.h +++ b/tcmalloc/system-alloc.h @@ -65,7 +65,7 @@ int SystemReleaseErrors(); // be released, partial pages will not.) // // Returns true on success. -ABSL_MUST_USE_RESULT bool SystemRelease(void* start, size_t length); +[[nodiscard]] bool SystemRelease(void* start, size_t length); // This call is the inverse of SystemRelease: the pages in this range // are in use and should be faulted in. (In principle this is a diff --git a/tcmalloc/transfer_cache.cc b/tcmalloc/transfer_cache.cc index d1e130a38..b42b19d05 100644 --- a/tcmalloc/transfer_cache.cc +++ b/tcmalloc/transfer_cache.cc @@ -48,7 +48,7 @@ void BackingTransferCache::InsertRange(absl::Span batch) const { tc_globals.transfer_cache().InsertRange(size_class_, batch); } -ABSL_MUST_USE_RESULT int BackingTransferCache::RemoveRange( +[[nodiscard]] int BackingTransferCache::RemoveRange( const absl::Span batch) const { return tc_globals.transfer_cache().RemoveRange(size_class_, batch); } diff --git a/tcmalloc/transfer_cache_internals.h b/tcmalloc/transfer_cache_internals.h index 164fb443e..2a3bd4ce0 100644 --- a/tcmalloc/transfer_cache_internals.h +++ b/tcmalloc/transfer_cache_internals.h @@ -179,8 +179,7 @@ class TransferCache { // Returns the actual number of fetched elements and stores elements in the // batch. - ABSL_MUST_USE_RESULT int RemoveRange(int size_class, - const absl::Span batch) + [[nodiscard]] int RemoveRange(int size_class, const absl::Span batch) ABSL_LOCKS_EXCLUDED(lock_) { TC_ASSERT(!batch.empty()); TC_ASSERT_LE(batch.size(), kMaxObjectsToMove);