mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:57:44 +00:00
AK: Add Span::align_to
This method returns a sub-span whose data pointer and size is aligned to a specified alignment.
This commit is contained in:
parent
ab98ad4e70
commit
c911fb0150
1 changed files with 10 additions and 0 deletions
10
AK/Span.h
10
AK/Span.h
|
@ -167,6 +167,16 @@ public:
|
|||
return { this->m_values, min(size(), length) };
|
||||
}
|
||||
|
||||
[[nodiscard]] Span align_to(size_t alignment) const
|
||||
{
|
||||
auto* start = reinterpret_cast<T*>(align_up_to((FlatPtr)data(), alignment));
|
||||
auto* end = reinterpret_cast<T*>(align_down_to((FlatPtr)(data() + size()), alignment));
|
||||
if (end < start)
|
||||
return {};
|
||||
size_t length = end - start;
|
||||
return { start, length };
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE constexpr T* offset(size_t start) const
|
||||
{
|
||||
VERIFY(start < this->m_size);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue