summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorDaniel Bertalan <dani@danielbertalan.dev>2023-04-25 07:36:08 +0200
committerAndreas Kling <kling@serenityos.org>2023-04-29 08:24:18 +0200
commitc911fb0150130209c0e7ace58ab5682689233c20 (patch)
treeb1996fa03e3634533d30cfd608db6db44cfe367b /AK
parentab98ad4e700d2db33ef3d81722c4ff5cd86d6b14 (diff)
downloadserenity-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.h10
1 files changed, 10 insertions, 0 deletions
diff --git a/AK/Span.h b/AK/Span.h
index 6c77ce04f4..a80502eac1 100644
--- a/AK/Span.h
+++ b/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);