From 72fe8dd2d6d6a3a5592646950a4e43b9a79ed9b4 Mon Sep 17 00:00:00 2001 From: Steven Le Rouzic Date: Mon, 2 Dec 2024 00:35:56 +0100 Subject: Start work on buffer --- asl/BUILD.bazel | 2 ++ asl/box.hpp | 1 - asl/buffer.hpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ asl/layout.hpp | 2 ++ asl/tests/buffer_tests.cpp | 8 ++++++++ 5 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 asl/buffer.hpp create mode 100644 asl/tests/buffer_tests.cpp (limited to 'asl') diff --git a/asl/BUILD.bazel b/asl/BUILD.bazel index 391ae9d..cbfe91e 100644 --- a/asl/BUILD.bazel +++ b/asl/BUILD.bazel @@ -5,6 +5,7 @@ cc_library( "annotations.hpp", "assert.hpp", "box.hpp", + "buffer.hpp", "config.hpp", "float.hpp", "format.hpp", @@ -45,6 +46,7 @@ cc_library( ], ) for name in [ "box", + "buffer", "float", "format", "functional", diff --git a/asl/box.hpp b/asl/box.hpp index eeb2b9c..7556d76 100644 --- a/asl/box.hpp +++ b/asl/box.hpp @@ -5,7 +5,6 @@ #include "asl/annotations.hpp" #include "asl/memory.hpp" #include "asl/utility.hpp" -#include "asl/box.hpp" namespace asl { diff --git a/asl/buffer.hpp b/asl/buffer.hpp new file mode 100644 index 0000000..abf29fe --- /dev/null +++ b/asl/buffer.hpp @@ -0,0 +1,46 @@ +#pragma once + +#include "asl/meta.hpp" +#include "asl/allocator.hpp" +#include "asl/annotations.hpp" + +namespace asl +{ + +template +class buffer +{ + + T* m_data{}; + isize_t m_capacity{}; + + // bit 63 : 0 = on heap, 1 = inline + // bits [62:56] : size when inline + // bits [52:0] : size when on heap + size_t m_size_encoded{}; + + ASL_NO_UNIQUE_ADDRESS Allocator m_allocator; + + static_assert(align_of <= align_of); + static_assert(align_of == align_of); + static_assert(align_of == align_of); + +public: + + static constexpr isize_t kInlineCapacity = []() { + // 1 byte is used for size inline in m_size_encoded. + // This is enough because we have at most 24 bytes available, + // so 23 chars of capacity. + const isize_t available_size = size_of + size_of + size_of - 1; + return available_size / size_of; + }(); + + constexpr buffer() requires default_constructible = default; + + explicit constexpr buffer(Allocator allocator) + : m_allocator{ASL_MOVE(allocator)} + {} +}; + +} // namespace asl + diff --git a/asl/layout.hpp b/asl/layout.hpp index 2295a1c..990af46 100644 --- a/asl/layout.hpp +++ b/asl/layout.hpp @@ -33,3 +33,5 @@ struct layout }; } // namespace asl + +#define AslOffsetOf(S, M) (static_cast(__builtin_offsetof(S, M))) diff --git a/asl/tests/buffer_tests.cpp b/asl/tests/buffer_tests.cpp new file mode 100644 index 0000000..133a549 --- /dev/null +++ b/asl/tests/buffer_tests.cpp @@ -0,0 +1,8 @@ +#include + +#include + +static_assert(asl::buffer::kInlineCapacity == 5); +static_assert(asl::buffer::kInlineCapacity == 2); +static_assert(asl::buffer::kInlineCapacity == 23); + -- cgit