Start work on buffer

This commit is contained in:
2024-12-02 00:35:56 +01:00
parent 27c3969e69
commit 72fe8dd2d6
5 changed files with 58 additions and 1 deletions

View File

@ -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",

View File

@ -5,7 +5,6 @@
#include "asl/annotations.hpp"
#include "asl/memory.hpp"
#include "asl/utility.hpp"
#include "asl/box.hpp"
namespace asl
{

46
asl/buffer.hpp Normal file
View File

@ -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

View File

@ -33,3 +33,5 @@ struct layout
};
} // namespace asl
#define AslOffsetOf(S, M) (static_cast<isize_t>(__builtin_offsetof(S, M)))

View File

@ -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);