From e9bf9ba46d618dda4a39110dbc118549a905f9a7 Mon Sep 17 00:00:00 2001 From: Sergei Date: Fri, 5 Jul 2024 10:56:41 +0300 Subject: [PATCH] Minor improvement of the `cetlvast::TrackingMemoryResource` (#132) It is now based on `cetl::pmr::get_default_resource()` (instead of `std::malloc` etc). --- .../include/cetlvast/memory_resource_mock.hpp | 4 +-- .../cetlvast/tracking_memory_resource.hpp | 26 ++++++++++++------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/cetlvast/include/cetlvast/memory_resource_mock.hpp b/cetlvast/include/cetlvast/memory_resource_mock.hpp index cba9619..ff943b2 100644 --- a/cetlvast/include/cetlvast/memory_resource_mock.hpp +++ b/cetlvast/include/cetlvast/memory_resource_mock.hpp @@ -25,7 +25,7 @@ class MemoryResourceMock : public cetl::pmr::memory_resource MOCK_METHOD(void*, do_allocate, (std::size_t, std::size_t), (override)); MOCK_METHOD(void, do_deallocate, (void*, std::size_t, std::size_t)); // NOLINTNEXTLINE(bugprone-exception-escape) - MOCK_METHOD(bool, do_is_equal, (const memory_resource&), (const, noexcept, override)); + MOCK_METHOD(bool, do_is_equal, (const cetl::pmr::memory_resource&), (const, noexcept, override)); #if (__cplusplus < CETL_CPP_STANDARD_17) // NOLINTNEXTLINE(bugprone-exception-escape) @@ -45,7 +45,7 @@ class MemoryResourceMock : public cetl::pmr::memory_resource .WillRepeatedly([&mr](void* p, std::size_t size_bytes, std::size_t alignment) { mr.deallocate(p, size_bytes, alignment); }); - EXPECT_CALL(*this, do_is_equal(_)).WillRepeatedly([&mr](const memory_resource& rhs) { + EXPECT_CALL(*this, do_is_equal(_)).WillRepeatedly([&mr](const cetl::pmr::memory_resource& rhs) { return mr.is_equal(rhs); }); diff --git a/cetlvast/include/cetlvast/tracking_memory_resource.hpp b/cetlvast/include/cetlvast/tracking_memory_resource.hpp index 092abfa..e757f9e 100644 --- a/cetlvast/include/cetlvast/tracking_memory_resource.hpp +++ b/cetlvast/include/cetlvast/tracking_memory_resource.hpp @@ -7,8 +7,10 @@ #define CETLVAST_TRACKING_MEMORY_RESOURCE_HPP_INCLUDED #include "cetl/pf17/cetlpf.hpp" -#include "cetl/pmr/memory.hpp" +#include +#include +#include #include #include @@ -30,9 +32,10 @@ class TrackingMemoryResource final : public cetl::pmr::memory_resource }; // NOLINTBEGIN - std::vector allocations{}; - std::size_t total_allocated_bytes = 0; - std::size_t total_deallocated_bytes = 0; + std::vector allocations{}; + std::size_t total_allocated_bytes = 0; + std::size_t total_deallocated_bytes = 0; + cetl::pmr::memory_resource* memory_{cetl::pmr::get_default_resource()}; // NOLINTEND private: @@ -48,7 +51,7 @@ class TrackingMemoryResource final : public cetl::pmr::memory_resource return nullptr; } - auto ptr = std::malloc(size_bytes); + void* ptr = memory_->allocate(size_bytes, alignment); total_allocated_bytes += size_bytes; allocations.push_back({size_bytes, ptr}); @@ -56,7 +59,7 @@ class TrackingMemoryResource final : public cetl::pmr::memory_resource return ptr; } - void do_deallocate(void* ptr, std::size_t size_bytes, std::size_t) override + void do_deallocate(void* ptr, std::size_t size_bytes, std::size_t alignment) override { auto prev_alloc = std::find_if(allocations.cbegin(), allocations.cend(), [ptr](const auto& alloc) { return alloc.pointer == ptr; @@ -67,22 +70,25 @@ class TrackingMemoryResource final : public cetl::pmr::memory_resource } total_deallocated_bytes += size_bytes; - std::free(ptr); + memory_->deallocate(ptr, size_bytes, alignment); } #if (__cplusplus < CETL_CPP_STANDARD_17) - void* do_reallocate(void* ptr, std::size_t old_size_bytes, std::size_t new_size_bytes, std::size_t) override + void* do_reallocate(void* ptr, + std::size_t old_size_bytes, + std::size_t new_size_bytes, + std::size_t alignment) override { total_allocated_bytes -= old_size_bytes; total_allocated_bytes += new_size_bytes; - return std::realloc(ptr, new_size_bytes); + return memory_->reallocate(ptr, old_size_bytes, new_size_bytes, alignment); } #endif - bool do_is_equal(const memory_resource& rhs) const noexcept override + bool do_is_equal(const cetl::pmr::memory_resource& rhs) const noexcept override { return (&rhs == this); }