Skip to content

Commit

Permalink
Wrote IoRingStreamBuffer tests; doing line/cond coverage testing now.
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyastolfi committed Jan 3, 2024
1 parent c5186ec commit 5973ba7
Show file tree
Hide file tree
Showing 12 changed files with 977 additions and 111 deletions.
2 changes: 1 addition & 1 deletion src/llfs/buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace std {
//
/** \brief Resizes the given buffer storage object and returns a MutableBuffer of the same size.
*/
inline llfs::MutableBuffer resize_buffer_storage(std::unique_ptr<llfs::u8>& p_storage,
inline llfs::MutableBuffer resize_buffer_storage(std::unique_ptr<llfs::u8[]>& p_storage,
llfs::usize size)
{
p_storage.reset(new llfs::u8[size]);
Expand Down
2 changes: 1 addition & 1 deletion src/llfs/buffer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ using namespace llfs::int_types;

TEST(BufferTest, ResizeBufferStorage)
{
std::unique_ptr<u8> storage;
std::unique_ptr<u8[]> storage;
llfs::MutableBuffer buffer = resize_buffer_storage(storage, 1977);

EXPECT_NE(buffer.data(), nullptr);
Expand Down
10 changes: 10 additions & 0 deletions src/llfs/ioring_buffer_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ IoRingBufferPool::Buffer::Buffer() noexcept : allocated_{nullptr}
/*explicit*/ IoRingBufferPool::Buffer::Buffer(batt::SharedPtr<const Allocated>&& allocated) noexcept
: allocated_{std::move(allocated)}
{
LLFS_VLOG_IF(1, this->allocated_)
<< BATT_INSPECT(this->allocated_->use_count()) << "->" << (this->allocated_->use_count() + 1);
}

//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
//
IoRingBufferPool::Buffer::~Buffer() noexcept
{
LLFS_VLOG_IF(1, this->allocated_)
<< BATT_INSPECT(this->allocated_->use_count()) << "->" << (this->allocated_->use_count() - 1);
}

//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
Expand Down
7 changes: 7 additions & 0 deletions src/llfs/ioring_buffer_pool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,13 @@ class IoRingBufferPool
*/
Buffer(const Buffer&) = default;

/** \brief Destroys this Buffer object, decrementing the ref count on the underlying memory by
* one.
*/
~Buffer() noexcept;

//----- --- -- - - - -

/** This class is copyable; all copies share the same underlying buffer memory. When the last
* copy is destroyed, the buffer is returned to the pool.
*/
Expand Down
26 changes: 16 additions & 10 deletions src/llfs/ioring_buffer_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@

namespace llfs {

//=#=#==#==#===============+=+=+=+=++=++++++++++++++-++-+--+-+----+---------------
// class IoRingStreamBuffer::BufferView

//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
//
IoRingBufferView IoRingBufferView::split(usize byte_offset) noexcept
template <typename SliceT>
auto BasicIoRingBufferView<SliceT>::split(usize byte_offset) noexcept -> Self
{
byte_offset = std::min(byte_offset, this->slice.size());

IoRingBufferView prefix{
.buffer = this->buffer,
.slice = ConstBuffer{this->slice.data(), byte_offset},
Self prefix{
this->buffer,
SliceT{this->slice.data(), byte_offset},
};

this->slice += byte_offset;
Expand All @@ -32,24 +30,32 @@ IoRingBufferView IoRingBufferView::split(usize byte_offset) noexcept

//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
//
bool IoRingBufferView::can_merge_with(const IoRingBufferView& other) const noexcept
template <typename SliceT>
bool BasicIoRingBufferView<SliceT>::can_merge_with(const Self& other) const noexcept
{
return this->buffer == other.buffer //
&& get_buffer_end(this->slice) == other.slice.data();
}

//==#==========+==+=+=++=+++++++++++-+-+--+----- --- -- - - - -
//
bool IoRingBufferView::merge_with(const IoRingBufferView& other) noexcept
template <typename SliceT>
bool BasicIoRingBufferView<SliceT>::merge_with(const Self& other) noexcept
{
if (!this->can_merge_with(other)) {
return false;
}
this->slice = ConstBuffer{
this->slice = SliceT{
this->slice.data(),
this->slice.size() + other.slice.size(),
};
return true;
}

//=#=#==#==#===============+=+=+=+=++=++++++++++++++-++-+--+-+----+---------------
// Explicitly specialize for MutableBuffer and ConstBuffer only.

template class BasicIoRingBufferView<ConstBuffer>;
template class BasicIoRingBufferView<MutableBuffer>;

} //namespace llfs
79 changes: 65 additions & 14 deletions src/llfs/ioring_buffer_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,59 @@

namespace llfs {

//+++++++++++-+-+--+----- --- -- - - - -
//=#=#==#==#===============+=+=+=+=++=++++++++++++++-++-+--+-+----+---------------
//
/** \brief A read-only view (slice) of a pooled buffer.
*/
struct IoRingBufferView {
template <typename SliceT>
struct BasicIoRingBufferView {
using Self = BasicIoRingBufferView;

//+++++++++++-+-+--+----- --- -- - - - -

IoRingBufferPool::Buffer buffer;
ConstBuffer slice;
SliceT slice;

//+++++++++++-+-+--+----- --- -- - - - -

BasicIoRingBufferView() = default;

explicit BasicIoRingBufferView(IoRingBufferPool::Buffer&& buffer, const SliceT& slice) noexcept
: buffer{std::move(buffer)}
, slice{slice}
{
}

//----- --- -- - - - -
explicit BasicIoRingBufferView(const IoRingBufferPool::Buffer& buffer,
const SliceT& slice) noexcept
: buffer{buffer}
, slice{slice}
{
}

template <typename SliceU, typename = std::enable_if_t<!std::is_same_v<SliceU, SliceT>>>
/*implicit*/ BasicIoRingBufferView(const BasicIoRingBufferView<SliceU>& other) noexcept
: buffer{other.buffer}
, slice{other.slice}
{
}

template <typename SliceU, typename = std::enable_if_t<!std::is_same_v<SliceU, SliceT>>,
typename = void>
/*implicit*/ BasicIoRingBufferView(BasicIoRingBufferView<SliceU>&& other) noexcept
: buffer{std::move(other.buffer)}
, slice{other.slice}
{
}

BasicIoRingBufferView(const Self&) = default;
BasicIoRingBufferView& operator=(const Self&) = default;

//+++++++++++-+-+--+----- --- -- - - - -

/** \brief Returns a pointer to the start of the viewed data (`this->slice.data()`).
*/
const void* data() const noexcept
auto* data() const noexcept
{
return this->slice.data();
}
Expand All @@ -42,25 +82,36 @@ struct IoRingBufferView {
return this->slice.size();
}

/** \brief Returns a new buffer view containing the prefix of `this` ending at `byte_offset`;
* `this` is adjusted to start at `byte_offset` (immediately after the returned prefix).
/** \brief Returns true iff this->size() is 0.
*/
bool empty() const noexcept
{
return this->size() == 0;
}

/** \brief Returns a new buffer view containing the prefix of `this` ending at
* `byte_offset`; `this` is adjusted to start at `byte_offset` (immediately after the
* returned prefix).
*
* If byte_offset is after the end of `this->slice`, then it is automatically truncated to
* `this->slice.size()`, a copy of `this` is returned, and `this` becomes empty.
*/
IoRingBufferView split(usize byte_offset) noexcept;
Self split(usize byte_offset) noexcept;

/** \brief Returns true iff other is a view of the same Buffer as this, and other.slice comes
* immediately after this->slice.
/** \brief Returns true iff other is a view of the same Buffer as this, and other.slice
* comes immediately after this->slice.
*/
bool can_merge_with(const IoRingBufferView& other) const noexcept;
bool can_merge_with(const Self& other) const noexcept;

/** \brief If this->can_merge_with(other) would return true, then this view's slice is extended
* to include other.slice and true is returned. Otherwise returns false.
/** \brief If this->can_merge_with(other) would return true, then this view's slice is
* extended to include other.slice and true is returned. Otherwise returns false.
*/
bool merge_with(const IoRingBufferView& other) noexcept;
bool merge_with(const Self& other) noexcept;
};

using IoRingConstBufferView = BasicIoRingBufferView<ConstBuffer>;
using IoRingMutableBufferView = BasicIoRingBufferView<MutableBuffer>;

} //namespace llfs

#endif // LLFS_IORING_BUFFER_VIEW_HPP
Loading

0 comments on commit 5973ba7

Please sign in to comment.