diff options
author | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-02 00:35:56 +0100 |
---|---|---|
committer | Steven Le Rouzic <steven.lerouzic@gmail.com> | 2024-12-20 15:35:58 +0100 |
commit | 72fe8dd2d6d6a3a5592646950a4e43b9a79ed9b4 (patch) | |
tree | 24f9db48382dab0ff16eef438a79e8e59e173b99 /asl | |
parent | 27c3969e69f5525ccb0bc462a615a14142b5f178 (diff) |
Start work on buffer
Diffstat (limited to 'asl')
-rw-r--r-- | asl/BUILD.bazel | 2 | ||||
-rw-r--r-- | asl/box.hpp | 1 | ||||
-rw-r--r-- | asl/buffer.hpp | 46 | ||||
-rw-r--r-- | asl/layout.hpp | 2 | ||||
-rw-r--r-- | asl/tests/buffer_tests.cpp | 8 |
5 files changed, 58 insertions, 1 deletions
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<is_object T, allocator Allocator = DefaultAllocator>
+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<T> <= align_of<T*>);
+ static_assert(align_of<T*> == align_of<isize_t>);
+ static_assert(align_of<T*> == align_of<size_t>);
+
+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<T*> + size_of<isize_t> + size_of<size_t> - 1;
+ return available_size / size_of<T>;
+ }();
+
+ constexpr buffer() requires default_constructible<Allocator> = 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<isize_t>(__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 <asl/buffer.hpp>
+
+#include <asl/testing/testing.hpp>
+
+static_assert(asl::buffer<int32_t>::kInlineCapacity == 5);
+static_assert(asl::buffer<int64_t>::kInlineCapacity == 2);
+static_assert(asl::buffer<char>::kInlineCapacity == 23);
+
|