emlabcpp
modern opinionated embedded C++ library
util.h
Go to the documentation of this file.
1 
24 #pragma once
25 
26 #include <bit>
27 #include <cstddef>
28 #include <cstdint>
29 
30 namespace emlabcpp::pmr
31 {
32 
33 constexpr std::uintptr_t align_addr( std::uintptr_t const addr, std::size_t const alignment )
34 {
35  // note: based on tips from sarah@#include discord
36  auto const low_bit_mask = alignment - 1;
37  return ( addr + low_bit_mask ) & ~low_bit_mask;
38 }
39 
41 inline void* align( void* const ptr, std::size_t const alignment )
42 {
43  auto const iptr = std::bit_cast< std::uintptr_t >( ptr );
44  auto addr = align_addr( iptr, alignment );
45  return static_cast< std::byte* >( ptr ) + ( addr - iptr );
46 }
47 
48 } // namespace emlabcpp::pmr
MIT License.
Definition: aliases.h:36
constexpr std::uintptr_t align_addr(std::uintptr_t const addr, std::size_t const alignment)
Definition: util.h:33
void * align(void *const ptr, std::size_t const alignment)
TODO: this needs tests.
Definition: util.h:41
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118