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

Vulkan · Signal Garden

Query a device and study a clear-color window with explicit frame and presentation lifetimes.

Project files 6

Check byte counts before allocating

GPU-independent alignment and bounded-size helpers.

26 lines contracts.hpp
#pragma once

#include <cstddef>
#include <limits>
#include <optional>

namespace signal_garden {
// General alignment, including non-power-of-two values. This does not allocate.
[[nodiscard]] constexpr std::optional<std::size_t>
align_up(std::size_t value, std::size_t alignment) noexcept {
    if (alignment == 0) return std::nullopt;
    const auto remainder = value % alignment;
    const auto padding = remainder == 0 ? 0 : alignment - remainder;
    if (value > std::numeric_limits<std::size_t>::max() - padding)
        return std::nullopt;
    return value + padding;
}

// Preconditions become checked results at the allocation boundary.
[[nodiscard]] constexpr std::optional<std::size_t>
checked_bytes(std::size_t count, std::size_t stride, std::size_t budget) noexcept {
    if (stride == 0 || count > budget / stride) return std::nullopt;
    return count * stride;
}
} // namespace signal_garden

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

Taking your work offline? Download all course companions