Skip to content
Merged
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: 79 additions & 0 deletions include/condy/detail/buffers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* @file buffers.hpp
* @brief Basic buffer types and conversion utilities.
* @details This file defines basic buffer types and conversion functions.
* Buffer types are primarily used in asynchronous operations.
*/

#pragma once

#include "condy/detail/utils.hpp"
#include <cassert>
#include <cstddef>
#include <cstring>
#include <utility>

namespace condy {
namespace detail {

template <typename BufferPool> struct ManagedBuffer {
public:
using CondyBuffer = void;

ManagedBuffer() = default;
ManagedBuffer(void *data, size_t size, BufferPool *pool)
: data_(data), size_(size), pool_(pool) {}
ManagedBuffer(ManagedBuffer &&other) noexcept
: data_(std::exchange(other.data_, nullptr)),
size_(std::exchange(other.size_, 0)),
pool_(std::exchange(other.pool_, nullptr)) {}
ManagedBuffer &operator=(ManagedBuffer &&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;
}

~ManagedBuffer() { reset(); }

CONDY_DELETE_COPY(ManagedBuffer);

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 {
if (pool_ != nullptr) {
pool_->add_buffer_back(data_, size_);
}
data_ = nullptr;
size_ = 0;
pool_ = nullptr;
}

/**
* @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;
BufferPool *pool_ = nullptr;
};

} // namespace detail
} // namespace condy
66 changes: 4 additions & 62 deletions include/condy/provided_buffers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#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 Down Expand Up @@ -241,58 +242,7 @@ class BundledProvidedBufferPool;
* @note The lifetime of the provided buffer must not exceed the lifetime of the
* provided buffer pool it is associated with.
*/
struct ProvidedBuffer {
public:
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() { reset(); }

CONDY_DELETE_COPY(ProvidedBuffer);

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

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

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

/**
* @brief Check if the provided 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;
};
using ProvidedBuffer = detail::ManagedBuffer<detail::BundledProvidedBufferPool>;

namespace detail {

Expand Down Expand Up @@ -428,7 +378,8 @@ class BundledProvidedBufferPool {
return buffers;
}

void add_buffer_back(void *ptr) noexcept {
void add_buffer_back(void *ptr, [[maybe_unused]] size_t size) noexcept {
assert(size <= buffer_size_);
char *base = get_buffers_base_();
assert(ptr >= base);
size_t offset = static_cast<char *>(ptr) - base;
Expand Down Expand Up @@ -468,15 +419,6 @@ class BundledProvidedBufferPool {

} // namespace detail

inline void ProvidedBuffer::reset() noexcept {
if (pool_ != nullptr) {
pool_->add_buffer_back(data_);
}
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