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
1 change: 1 addition & 0 deletions coroio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ set(SOURCES
address.cpp
init.cpp
notifier.cpp
read_buffer.cpp
socket.cpp
sockutils.cpp
ssl.cpp
Expand Down
95 changes: 69 additions & 26 deletions coroio/arena.hpp
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
#pragma once

#include <cstddef>
#include <limits>
#include <new>
#include <stack>
#include <vector>

namespace NNet {

/**
* @class TArenaAllocator
* @brief Arena allocator for objects of one type that are often created and destroyed.
*
* This class preallocates memory in pools of @c PoolSize objects and reuses the freed slots,
* so that each object does not need its own heap allocation. It hands out raw storage:
* construct the object in it with placement new and destroy the object before giving the
* storage back.
* @class TArena
* @brief Slots of one size, preallocated in pools, with a free list.
*
* @tparam T The type of object to allocate.
* @tparam PoolSize The number of objects in each preallocated pool (default is 1024).
*
* @code{.cpp}
* // Example usage:
* TArenaAllocator<TItem> allocator;
* TItem* item = new (allocator.Allocate()) TItem();
* // use item...
* item->~TItem();
* allocator.Deallocate(item);
* @endcode
* The size of a slot and the number of slots of a pool are set at construction.
* An arena of one pool (@p maxPools = 1) is one contiguous region, whose slots are
* also addressed by index; Allocate() returns nullptr when all of them are in use.
*/
template<typename T, size_t PoolSize = 1024>
class TArenaAllocator {
class TArena {
public:
TArenaAllocator() {
TArena(size_t slotSize, size_t alignment, size_t poolSize, size_t maxPools = std::numeric_limits<size_t>::max())
: SlotSize_(slotSize)
, Alignment_(alignment)
, PoolSize_(poolSize)
, MaxPools_(maxPools)
{
AllocatePool();
}

~TArenaAllocator() {
TArena(const TArena&) = delete;
TArena& operator=(const TArena&) = delete;

~TArena() {
for (auto block : Pools_) {
::operator delete(block, std::align_val_t(alignof(T)));
::operator delete(block, std::align_val_t(Alignment_));
}
}

void* Allocate() {
if (FreePages_.empty()) {
if (Pools_.size() == MaxPools_) {
return nullptr;
}
AllocatePool();
}
++AllocatedObjects_;
Expand All @@ -58,18 +58,61 @@ class TArenaAllocator {
return AllocatedObjects_;
}

/// Slot @p index of the first pool.
char* Slot(size_t index) const {
return Pools_.front() + index * SlotSize_;
}

/// Index of @p slot, a slot of the first pool.
size_t Index(const void* slot) const {
return static_cast<size_t>(static_cast<const char*>(slot) - Pools_.front()) / SlotSize_;
}

private:
void AllocatePool() {
char* pool = static_cast<char*>(::operator new(PoolSize * sizeof(T), std::align_val_t(alignof(T))));
char* pool = static_cast<char*>(::operator new(PoolSize_ * SlotSize_, std::align_val_t(Alignment_)));
Pools_.emplace_back(pool);
for (size_t i = 0; i < PoolSize; i++) {
FreePages_.push(&pool[i * sizeof(T)]);
for (size_t i = 0; i < PoolSize_; i++) {
FreePages_.push(&pool[i * SlotSize_]);
}
}

size_t SlotSize_;
size_t Alignment_;
size_t PoolSize_;
size_t MaxPools_;
std::vector<char*> Pools_;
std::stack<void*, std::vector<void*>> FreePages_;
int AllocatedObjects_ = 0;
};

/**
* @class TArenaAllocator
* @brief Arena allocator for objects of one type that are often created and destroyed.
*
* This class preallocates memory in pools of @c PoolSize objects and reuses the freed slots,
* so that each object does not need its own heap allocation. It hands out raw storage:
* construct the object in it with placement new and destroy the object before giving the
* storage back.
*
* @tparam T The type of object to allocate.
* @tparam PoolSize The number of objects in each preallocated pool (default is 1024).
*
* @code{.cpp}
* // Example usage:
* TArenaAllocator<TItem> allocator;
* TItem* item = new (allocator.Allocate()) TItem();
* // use item...
* item->~TItem();
* allocator.Deallocate(item);
* @endcode
*/
template<typename T, size_t PoolSize = 1024>
class TArenaAllocator: public TArena {
public:
TArenaAllocator()
: TArena(sizeof(T), alignof(T), PoolSize)
{ }
};

} // namespace NNet
14 changes: 14 additions & 0 deletions coroio/poller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <assert.h>

#include "base.hpp"
#include "read_buffer.hpp"

#ifdef Yield
#undef Yield
Expand Down Expand Up @@ -358,6 +359,18 @@ class TPollerBase {
auto TimersSize() const {
return Timers_.size();
}
/**
* @brief Sets the buffers of ReadBuffer() on this poller's handles.
*
* @throws std::logic_error after the first ReadBuffer(), std::invalid_argument for a bad config.
*/
void ConfigureReadBuffers(const TReadBufferConfig& config) {
Buffers_.Configure(config);
}
/// The buffers of ReadBuffer(), used by the handles.
NDetail::TBufferPool& ReadBuffers() {
return Buffers_;
}

protected:
/**
Expand Down Expand Up @@ -469,6 +482,7 @@ class TPollerBase {
unsigned LastFiredTimer_ = (unsigned)(-1); ///< ID of the last fired timer.
std::chrono::milliseconds MaxDuration_ = std::chrono::milliseconds(100); ///< Maximum poll duration.
timespec MaxDurationTs_ = GetMaxDuration(MaxDuration_); ///< Max duration represented as timespec.
NDetail::TBufferPool Buffers_; ///< Buffers of ReadBuffer().

private:
void QueueRemoval(int fd, int close) {
Expand Down
41 changes: 41 additions & 0 deletions coroio/read_buffer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "read_buffer.hpp"

#include <stdexcept>
#include <system_error>

namespace NNet::NDetail {

void TBufferPool::Configure(const TReadBufferConfig& config) {
if (Arena_) {
throw std::logic_error("read buffers are configured after their first use");
}
// Buffer ids are 16-bit, and a buffer ring takes a power of two of at most 32768 entries.
if (config.BufferSize == 0 || config.PerSocketLimit == 0
|| config.Count == 0 || config.Count > 32768 || (config.Count & (config.Count - 1)))
{
throw std::invalid_argument("bad read buffer config");
}
Config_ = config;
}

void TBufferPool::Allocate() {
if (!Arena_) {
// One pool, so that a buffer has an id. Its pages are touched only when used.
Arena_.emplace(Config_.BufferSize, 64, Config_.Count, 1);
Thread_ = std::this_thread::get_id();
}
}

std::span<char> TBufferPool::Acquire(TReadBuffer& lease) {
assert(!Owner_); // the owner's system picks the buffers
Allocate();
void* buffer = Arena_->Allocate();
if (!buffer) {
throw std::system_error(std::make_error_code(std::errc::no_buffer_space), "read buffers are all leased");
}
lease = TReadBuffer{};
Lend(lease, static_cast<uint16_t>(Arena_->Index(buffer)));
return {lease.Ptr_, Config_.BufferSize};
}

} // namespace NNet::NDetail
Loading
Loading