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

Record the allocator experiment

Run resource-policy samples, print measurements, and optionally save the tile image.

36 lines allocator_main.cpp
#include "prism_engineering/allocator_lab.hpp"
#include <fstream>
#include <iostream>
#include <locale>
#include <stdexcept>
using namespace prism_engineering;
int main(int argc,char** argv) {
  try {
    if(argc>2)throw std::runtime_error{"usage: prism-allocators [image.ppm]"};
    const auto baseline=render_plain();
    std::cout<<"policy,run,request_calls,requested_bytes,peak_requested,upstream_calls,upstream_requested,upstream_peak,retained_upstream,after_release,elapsed_ns,checksum\n";
    for(const auto choice:{ResourceChoice::ordinary,ResourceChoice::monotonic,ResourceChoice::pool}) {
      const char* name=choice==ResourceChoice::ordinary?"ordinary":choice==ResourceChoice::monotonic?"monotonic":"pool";
      for(int run=0;run<10;++run) {
        const auto result=measure_render(choice);
        if(result.image.pixels!=baseline.pixels)throw std::runtime_error{"allocation policy changed rendered pixels"};
        std::cout<<name<<','<<run<<','<<result.requests.calls<<','<<result.requests.requested_bytes<<','
                 <<result.requests.peak_live_bytes<<','<<result.upstream_before_release.calls<<','
                 <<result.upstream_before_release.requested_bytes<<','<<result.upstream_before_release.peak_live_bytes<<','
                 <<result.upstream_before_release.live_bytes<<','
                 <<result.upstream_after_release.live_bytes<<','<<result.elapsed_nanoseconds<<','<<image_checksum(result.image)<<'\n';
      }
    }
    if(argc==2) {
      std::ofstream output{argv[1],std::ios::binary}; output.imbue(std::locale::classic());
      output<<"P6\n64 64\n255\n";
      for(const auto pixel:baseline.pixels) {
        const char bytes[]{static_cast<char>(pixel.r),static_cast<char>(pixel.g),static_cast<char>(pixel.b)};
        output.write(bytes,3);
      }
      output.flush(); output.close(); if(!output)throw std::runtime_error{"image write failed"};
    }
    return 0;
  } catch(const std::exception& error) { std::cerr<<error.what()<<'\n'; return 1; }
}

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

Taking your work offline? Download all course companions