diff options
author | Daniel Bertalan <dani@danielbertalan.dev> | 2023-04-25 07:36:08 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-04-29 08:24:18 +0200 |
commit | c911fb0150130209c0e7ace58ab5682689233c20 (patch) | |
tree | b1996fa03e3634533d30cfd608db6db44cfe367b /AK | |
parent | ab98ad4e700d2db33ef3d81722c4ff5cd86d6b14 (diff) | |
download | serenity-c911fb0150130209c0e7ace58ab5682689233c20.zip |
AK: Add `Span::align_to`
This method returns a sub-span whose data pointer and size is aligned to
a specified alignment.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/Span.h | 10 |
1 files changed, 10 insertions, 0 deletions
@@ -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); |