summaryrefslogtreecommitdiff
path: root/asl/buffer.hpp
diff options
context:
space:
mode:
authorSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-02 00:35:56 +0100
committerSteven Le Rouzic <steven.lerouzic@gmail.com>2024-12-20 15:35:58 +0100
commit72fe8dd2d6d6a3a5592646950a4e43b9a79ed9b4 (patch)
tree24f9db48382dab0ff16eef438a79e8e59e173b99 /asl/buffer.hpp
parent27c3969e69f5525ccb0bc462a615a14142b5f178 (diff)
Start work on buffer
Diffstat (limited to 'asl/buffer.hpp')
-rw-r--r--asl/buffer.hpp46
1 files changed, 46 insertions, 0 deletions
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
+