Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions include/condy/detail/buffers.hpp

This file was deleted.

65 changes: 60 additions & 5 deletions include/condy/provided_buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "condy/concepts.hpp"
#include "condy/condy_uring.hpp"
#include "condy/detail/buffers.hpp"
#include "condy/detail/context.hpp"
#include "condy/detail/ring.hpp"
#include "condy/detail/utils.hpp"
Expand All @@ -22,6 +21,7 @@
#include <stdexcept>
#include <sys/mman.h>
#include <sys/types.h>
#include <utility>
#include <vector>

namespace condy {
Expand Down Expand Up @@ -244,11 +244,57 @@ class BundledProvidedBufferPool;
* @note The lifetime of the provided buffer must not exceed the lifetime of the
* provided buffer pool it is associated with.
*/
class ProvidedBuffer
: public detail::ManagedBuffer<detail::BundledProvidedBufferPool> {
class ProvidedBuffer {
public:
using Base = detail::ManagedBuffer<detail::BundledProvidedBufferPool>;
using Base::Base;
using CondyBuffer = void;

ProvidedBuffer() = default;
ProvidedBuffer(void *data, size_t size,
detail::BundledProvidedBufferPool *pool)
: data_(data), size_(size), pool_(pool) {}
ProvidedBuffer(ProvidedBuffer &&other) noexcept
: data_(std::exchange(other.data_, nullptr)),
size_(std::exchange(other.size_, 0)),
pool_(std::exchange(other.pool_, nullptr)) {}
ProvidedBuffer &operator=(ProvidedBuffer &&other) noexcept {
if (this != &other) {
reset();
data_ = std::exchange(other.data_, nullptr);
size_ = std::exchange(other.size_, 0);
pool_ = std::exchange(other.pool_, nullptr);
}
return *this;
}

~ProvidedBuffer() noexcept { reset(); }

CONDY_DELETE_COPY(ProvidedBuffer);

public:
/**
* @brief Get the data pointer of the buffer
*/
void *data() const noexcept { return data_; }

/**
* @brief Get the size of the buffer
*/
size_t size() const noexcept { return size_; }

/**
* @brief Reset the buffer, returning it to the pool if owned
*/
void reset() noexcept;

/**
* @brief Check if the buffer owns a buffer from a pool.
*/
bool owns_buffer() const noexcept { return pool_ != nullptr; }

private:
void *data_ = nullptr;
size_t size_ = 0;
detail::BundledProvidedBufferPool *pool_ = nullptr;
};

namespace detail {
Expand Down Expand Up @@ -411,6 +457,15 @@ class BundledProvidedBufferPool {

} // namespace detail

inline void ProvidedBuffer::reset() noexcept {
if (pool_ != nullptr) {
pool_->add_buffer_back(data_, size_);
}
data_ = nullptr;
size_ = 0;
pool_ = nullptr;
}

/**
* @brief Provided buffer pool.
* @details A provided buffer pool manages a pool of buffers that can be used in
Expand Down
Loading
Loading