Nix 2.36.0
Nix, the purely functional package manager; unstable internal interfaces
Loading...
Searching...
No Matches
alignment.hh
Go to the documentation of this file.
1#pragma once
3
4#include "nix/util/error.hh"
5
6#include <cassert>
7#include <limits>
8#include <type_traits>
9#include <bit>
10
11namespace nix {
12
16template<typename T>
17 requires std::is_unsigned_v<T>
18constexpr T alignUp(T val, unsigned alignment)
19{
20 assert(std::has_single_bit(alignment) && "alignment must be a power of 2");
21 assert(alignment <= std::numeric_limits<T>::max());
22 T mask = ~(static_cast<T>(alignment) - 1u);
23 if (val > std::numeric_limits<T>::max() - (alignment - 1)) /* Overflow check. */
24 throw Error("can't align %d to %d: value is too large", val, alignment);
25 return (val + alignment - 1) & mask;
26}
27
28} // namespace nix
constexpr T alignUp(T val, unsigned alignment)
Definition alignment.hh:18
Definition error.hh:310
This file defines two main structs/classes used in nix error handling.