Skip to content

Commit

Permalink
Minor improvement of the cetlvast::TrackingMemoryResource (#132)
Browse files Browse the repository at this point in the history
It is now based on `cetl::pmr::get_default_resource()` (instead of
`std::malloc` etc).
  • Loading branch information
serges147 authored Jul 5, 2024
1 parent 6c279ce commit e9bf9ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cetlvast/include/cetlvast/memory_resource_mock.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
});

Expand Down
26 changes: 16 additions & 10 deletions cetlvast/include/cetlvast/tracking_memory_resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
#define CETLVAST_TRACKING_MEMORY_RESOURCE_HPP_INCLUDED

#include "cetl/pf17/cetlpf.hpp"
#include "cetl/pmr/memory.hpp"

#include <algorithm>
#include <cstddef>
#include <ios>
#include <ostream>
#include <vector>

Expand All @@ -30,9 +32,10 @@ class TrackingMemoryResource final : public cetl::pmr::memory_resource
};

// NOLINTBEGIN
std::vector<Allocation> allocations{};
std::size_t total_allocated_bytes = 0;
std::size_t total_deallocated_bytes = 0;
std::vector<Allocation> 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:
Expand All @@ -48,15 +51,15 @@ 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});

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;
Expand All @@ -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);
}
Expand Down

0 comments on commit e9bf9ba

Please sign in to comment.