WPILibC++ 2025.1.1
Loading...
Searching...
No Matches
align.hpp
Go to the documentation of this file.
1// Copyright (C) 2015-2023 Jonathan Müller and foonathan/memory contributors
2// SPDX-License-Identifier: Zlib
3
4#ifndef WPI_MEMORY_DETAIL_ALIGN_HPP_INCLUDED
5#define WPI_MEMORY_DETAIL_ALIGN_HPP_INCLUDED
6
7#include <cstdint>
8
9#include "../config.hpp"
10#include "assert.hpp"
11
12namespace wpi
13{
14 namespace memory
15 {
16 namespace detail
17 {
18 // whether or not an alignment is valid, i.e. a power of two not zero
19 constexpr bool is_valid_alignment(std::size_t alignment) noexcept
20 {
21 return alignment && (alignment & (alignment - 1)) == 0u;
22 }
23
24 // returns the offset needed to align ptr for given alignment
25 // alignment must be valid
26 inline std::size_t align_offset(std::uintptr_t address, std::size_t alignment) noexcept
27 {
29 auto misaligned = address & (alignment - 1);
30 return misaligned != 0 ? (alignment - misaligned) : 0;
31 }
32 inline std::size_t align_offset(void* ptr, std::size_t alignment) noexcept
33 {
34 return align_offset(reinterpret_cast<std::uintptr_t>(ptr), alignment);
35 }
36
37 // whether or not the pointer is aligned for given alignment
38 // alignment must be valid
39 bool is_aligned(void* ptr, std::size_t alignment) noexcept;
40
41 // maximum alignment value
42 constexpr std::size_t max_alignment = alignof(std::max_align_t);
43 static_assert(is_valid_alignment(max_alignment), "ehm..?");
44
45 // returns the minimum alignment required for a node of given size
46 std::size_t alignment_for(std::size_t size) noexcept;
47 } // namespace detail
48 } // namespace memory
49} // namespace wpi
50
51#endif // WPI_MEMORY_DETAIL_ALIGN_HPP_INCLUDED
Configuration macros.
auto ptr(T p) -> const void *
Converts p to const void* for pointer formatting.
Definition format.h:3821
detail namespace with internal helper functions
Definition input_adapters.h:32
std::size_t align_offset(std::uintptr_t address, std::size_t alignment) noexcept
Definition align.hpp:26
constexpr std::size_t max_alignment
Definition align.hpp:42
bool is_aligned(void *ptr, std::size_t alignment) noexcept
constexpr bool is_valid_alignment(std::size_t alignment) noexcept
Definition align.hpp:19
std::size_t alignment_for(std::size_t size) noexcept
Memory namespace.
Definition heap_allocator.hpp:20
Foonathan namespace.
Definition ntcore_cpp.h:26
#define WPI_MEMORY_ASSERT(Expr)
Definition assert.hpp:46