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

Renderer Engineering

Explore bounded image work, allocator lifetimes, cancellation, and Asio coroutines.

Project files 15

Describe scratch policies and measurements

The tile-rendering interface, allocation counters, and result ownership.

43 lines allocator_lab.hpp
#pragma once
#include "prism_engineering/asset_pipeline.hpp"
#include <cstddef>
#include <cstdint>
#include <memory_resource>

namespace prism_engineering {
struct AllocationCounts {
  std::size_t calls{}, requested_bytes{}, live_bytes{}, peak_live_bytes{};
};
// Single-threaded instrumentation. Equality is identity, not upstream equality:
// allocating through one counter and deallocating through another loses accounting.
class CountingResource final : public std::pmr::memory_resource {
public:
  explicit CountingResource(std::pmr::memory_resource& upstream) noexcept : upstream_{upstream} {}
  CountingResource(const CountingResource&)=delete;
  CountingResource& operator=(const CountingResource&)=delete;
  [[nodiscard]] AllocationCounts counts() const noexcept { return counts_; }
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_;
  AllocationCounts counts_;
};

enum class ResourceChoice { ordinary, monotonic, pool };
struct RenderMeasurement {
  PreparedImage image; // Ordinary owning allocation, outside scratch accounting.
  AllocationCounts requests;
  AllocationCounts upstream_before_release;
  AllocationCounts upstream_after_release;
  std::int64_t elapsed_nanoseconds{};
};

// Same 64x64, four-samples-per-pixel integer scene under each resource policy.
// All temporary tile objects die before returning; result pixels own their bytes.
[[nodiscard]] PreparedImage render_tiles(std::pmr::memory_resource& scratch);
[[nodiscard]] PreparedImage render_plain();
[[nodiscard]] RenderMeasurement measure_render(ResourceChoice choice);
[[nodiscard]] std::uint64_t image_checksum(const PreparedImage& image) noexcept;
} // namespace prism_engineering

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

Taking your work offline? Download all course companions