mirror of
https://github.com/NixOS/nix.git
synced 2025-11-08 19:46:02 +01:00
libutil: Add alignUp helper function
This commit is contained in:
parent
ddf7de0a76
commit
a91b787524
4 changed files with 43 additions and 0 deletions
18
src/libutil-tests/alignment.cc
Normal file
18
src/libutil-tests/alignment.cc
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include "nix/util/alignment.hh"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace nix {
|
||||
|
||||
TEST(alignUp, value)
|
||||
{
|
||||
for (uint64_t i = 1; i <= 8; ++i)
|
||||
EXPECT_EQ(alignUp(i, 8), 8);
|
||||
}
|
||||
|
||||
TEST(alignUp, notAPowerOf2)
|
||||
{
|
||||
ASSERT_DEATH({ alignUp(1u, 42); }, "alignment must be a power of 2");
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -44,6 +44,7 @@ config_priv_h = configure_file(
|
|||
subdir('nix-meson-build-support/common')
|
||||
|
||||
sources = files(
|
||||
'alignment.cc',
|
||||
'archive.cc',
|
||||
'args.cc',
|
||||
'base-n.cc',
|
||||
|
|
|
|||
23
src/libutil/include/nix/util/alignment.hh
Normal file
23
src/libutil/include/nix/util/alignment.hh
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <cassert>
|
||||
#include <type_traits>
|
||||
#include <cstdint>
|
||||
#include <bit>
|
||||
|
||||
namespace nix {
|
||||
|
||||
/// Aligns val upwards to be a multiple of alignment.
|
||||
///
|
||||
/// @pre alignment must be a power of 2.
|
||||
template<typename T>
|
||||
requires std::is_unsigned_v<T>
|
||||
constexpr T alignUp(T val, unsigned alignment)
|
||||
{
|
||||
assert(std::has_single_bit(alignment) && "alignment must be a power of 2");
|
||||
T mask = ~(T{alignment} - 1u);
|
||||
return (val + alignment - 1) & mask;
|
||||
}
|
||||
|
||||
} // namespace nix
|
||||
|
|
@ -4,6 +4,7 @@ include_dirs = [ include_directories('../..') ]
|
|||
|
||||
headers = files(
|
||||
'abstract-setting-to-json.hh',
|
||||
'alignment.hh',
|
||||
'ansicolor.hh',
|
||||
'archive.hh',
|
||||
'args.hh',
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue