Mmmmm – NDC{TechTown} 2023 © Björn Fahller @
[email protected] 71/110
Improving the tracing resource
class tracing_resource final : public std::pmr::memory_resource
{
public:
tracing_resource(std::ostream& os,
std::pmr::memory_resource* next = std::pmr::get_default_resource())
: os_(os), next_(next) {}
private:
void* do_allocate(size_t bytes, size_t align) override {
auto* addr = next_->allocate(bytes, align);
os_ << "allocate(" << bytes << ", " << align << ") -> " << addr << '\n';
return addr;
}
void do_deallocate(void* addr, size_t bytes, size_t align) override {
os_ << "deallocate(" << addr << ", " << bytes << ", " << align << ")\n";
next_->deallocate(addr, bytes, align);
}
bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
auto p = dynamic_cast(&other);
return p && next_ == p->next_;
}
std::ostream& os_;
std::pmr::memory_resource* next_;
};
Use the default
memory resource
if none is given