Theme Catppuccin
Make yourself at homeCatppuccin

A softer light for long learning sessions.

Appearance

Loading your preference…

Dark palette

Choose a dark palette to try it.

Light uses Latte. Your choices stay in this browser.

Back to your source shelf

Inside the companion

First Light

Make your first CPU image, rasterize triangles, and trace an allocation budget.

Project files 15

Describe an allocation report

The public result of the bounded PMR experiment.

41 lines memory_lab.hpp
#pragma once
#include <cstddef>
#include <memory_resource>
#include <string>

namespace prism {

// An instrumented interface, not an owner of upstream. Declare upstream first,
// this resource second, and its borrowing containers last (reverse destruction).
// This deliberately single-threaded teaching resource needs external locking
// if shared. Counters describe live requested bytes, not arena physical capacity.
class CountingResource final : public std::pmr::memory_resource {
public:
    explicit CountingResource(std::pmr::memory_resource& upstream) noexcept : upstream_{upstream} {}
    ~CountingResource() override = default;
    CountingResource(const CountingResource&) = delete;
    CountingResource& operator=(const CountingResource&) = delete;
    // Borrowers retain this resource's address and equality is identity-based.
    CountingResource(CountingResource&&) = delete;
    CountingResource& operator=(CountingResource&&) = delete;
    [[nodiscard]] std::size_t allocations() const noexcept { return allocations_; }
    [[nodiscard]] std::size_t live_bytes() const noexcept { return live_; }
    [[nodiscard]] std::size_t peak_bytes() const noexcept { return peak_; }
private:
    void* do_allocate(std::size_t bytes, std::size_t alignment) override;
    void do_deallocate(void* pointer, std::size_t bytes, std::size_t alignment) override;
    bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
        return this == &other;
    }
    std::pmr::memory_resource& upstream_;
    std::size_t allocations_{}, live_{}, peak_{};
};

struct MemoryReport {
    std::string promoted_title; // Independent ordinary owner survives arena release.
    std::size_t allocations{}, peak_bytes{}, live_after_scope{};
    bool bounded_failure{};
};
[[nodiscard]] MemoryReport run_memory_lab();
} // namespace prism

This is the file included with the course. Copy and download give you its complete source.

Taking your work offline? Download all course companions