tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
align.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_MEMORY_ALIGN_HPP
4#define TETL_MEMORY_ALIGN_HPP
5
9
10namespace etl {
11
22[[nodiscard]] inline auto align(etl::size_t alignment, etl::size_t size, void*& ptr, etl::size_t& space) noexcept
23 -> void*
24{
25 auto off = static_cast<etl::size_t>(bit_cast<etl::uintptr_t>(ptr) & (alignment - 1));
26 if (off != 0) {
27 off = alignment - off;
28 }
29 if (space < off || space - off < size) {
30 return nullptr;
31 }
32
33 ptr = static_cast<char*>(ptr) + off;
34 space -= off;
35 return ptr;
36}
37
38} // namespace etl
39
40#endif // TETL_MEMORY_ALIGN_HPP
constexpr auto bit_cast(From const &src) noexcept -> To
Obtain a value of type To by reinterpreting the object representation of from. Every bit in the value...
Definition bit_cast.hpp:38
constexpr auto size(C const &c) noexcept(noexcept(c.size())) -> decltype(c.size())
Returns the size of the given container c or array array. Returns c.size(), converted to the return t...
Definition size.hpp:18
Definition adjacent_find.hpp:8
auto align(etl::size_t alignment, etl::size_t size, void *&ptr, etl::size_t &space) noexcept -> void *
Given a pointer ptr to a buffer of size space, returns a pointer aligned by the specified alignment f...
Definition align.hpp:22
TETL_BUILTIN_SIZET size_t
etl::size_t is the unsigned integer type of the result of the sizeof operator.
Definition size_t.hpp:14