Start work on buffer
This commit is contained in:
@ -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",
|
||||
|
@ -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
46
asl/buffer.hpp
Normal 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
|
||||
|
@ -33,3 +33,5 @@ struct layout
|
||||
};
|
||||
|
||||
} // namespace asl
|
||||
|
||||
#define AslOffsetOf(S, M) (static_cast<isize_t>(__builtin_offsetof(S, M)))
|
||||
|
8
asl/tests/buffer_tests.cpp
Normal file
8
asl/tests/buffer_tests.cpp
Normal 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);
|
||||
|
Reference in New Issue
Block a user