mirror of
https://github.com/boostorg/boostlook.git
synced 2026-02-25 16:22:12 +00:00
754 lines
26 KiB
HTML
754 lines
26 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1">
|
|
<style>html.fonts-loading{visibility:hidden;opacity:0}</style>
|
|
<script>document.documentElement.classList.add('fonts-loading');</script>
|
|
|
|
<link rel="preload" href="../../_/font/NotoSansDisplay.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
|
|
<link rel="preload" href="../../_/font/NotoSansDisplay-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
|
|
<link rel="preload" href="../../_/font/MonaspaceNeon-Var.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
|
|
<link rel="preload" href="../../_/font/MonaspaceXenon-Var.woff2" as="font" type="font/woff2" crossorigin="anonymous" />
|
|
|
|
<script>
|
|
(function() {
|
|
'use strict';
|
|
|
|
var revealed = false;
|
|
|
|
var reveal = function() {
|
|
if (revealed) return;
|
|
revealed = true;
|
|
document.documentElement.classList.remove('fonts-loading');
|
|
};
|
|
|
|
setTimeout(reveal, 3000);
|
|
|
|
if (!('FontFace' in window) || !('fonts' in document)) {
|
|
setTimeout(reveal, 100);
|
|
return;
|
|
}
|
|
|
|
var uiRoot = '../../_';
|
|
var fonts = [
|
|
{
|
|
family: 'Noto Sans',
|
|
url: uiRoot + '/font/NotoSansDisplay.woff2',
|
|
descriptors: { style: 'normal', weight: '100 900', stretch: '62.5% 100%' }
|
|
},
|
|
{
|
|
family: 'Noto Sans',
|
|
url: uiRoot + '/font/NotoSansDisplay-Italic.woff2',
|
|
descriptors: { style: 'italic', weight: '100 900', stretch: '62.5% 100%' }
|
|
},
|
|
{
|
|
family: 'Monaspace Neon',
|
|
url: uiRoot + '/font/MonaspaceNeon-Var.woff2',
|
|
descriptors: { style: 'normal', weight: '400' }
|
|
},
|
|
{
|
|
family: 'Monaspace Xenon',
|
|
url: uiRoot + '/font/MonaspaceXenon-Var.woff2',
|
|
descriptors: { style: 'italic', weight: '400' }
|
|
}
|
|
];
|
|
|
|
var loadPromises = fonts.map(function(f) {
|
|
try {
|
|
var face = new FontFace(f.family, 'url("' + f.url + '")', f.descriptors);
|
|
return face.load().then(function(loaded) {
|
|
document.fonts.add(loaded);
|
|
return loaded;
|
|
}).catch(function() {
|
|
return null;
|
|
});
|
|
} catch (e) {
|
|
return Promise.resolve(null);
|
|
}
|
|
});
|
|
|
|
Promise.all(loadPromises)
|
|
.then(function() {
|
|
return document.fonts.ready;
|
|
})
|
|
.then(reveal)
|
|
.catch(reveal);
|
|
})();
|
|
</script> <title>Custom Dynamic Buffer :: Boost Libraries Documentation</title>
|
|
<link rel="canonical" href="https://antora.cppalliance.org/develop/lib/doc/capy/7.examples/7h.custom-dynamic-buffer.html">
|
|
<link rel="prev" href="7g.parallel-fetch.html">
|
|
<link rel="next" href="7i.echo-server-corosio.html">
|
|
<meta name="generator" content="Antora 3.1.14">
|
|
<link rel="stylesheet" href="../../_/css/boostlook.css">
|
|
<link rel="stylesheet" href="../../_/css/site.css">
|
|
<link rel="stylesheet" href="../../_/css/vendor/tabs.css">
|
|
<script>
|
|
(function() {
|
|
if (window.self !== window.top) return;
|
|
var theme = localStorage.getItem('antora-theme');
|
|
if (!theme && window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
|
theme = 'dark';
|
|
}
|
|
if (theme === 'dark') document.documentElement.classList.add('dark');
|
|
})();
|
|
</script>
|
|
<script>var uiRootPath = '../../_'</script>
|
|
<link rel="icon" href="../../_/img/favicons/favicon.ico" type="image/x-icon">
|
|
<!-- Favicon configuration -->
|
|
<link rel="apple-touch-icon" sizes="180x180" href="../../_/img/favicons/apple-touch-icon.png">
|
|
<link rel="icon" type="image/png" sizes="32x32" href="../../_/img/favicons/favicon-32x32.png">
|
|
<link rel="icon" type="image/png" sizes="16x16" href="../../_/img/favicons/favicon-16x16.png">
|
|
<link rel="manifest" href="../../_/img/favicons/site.webmanifest">
|
|
<link rel="shortcut icon" href="../../_/img/favicons/favicon.ico">
|
|
</head>
|
|
<body class="article toc2 toc-left">
|
|
<div class="boostlook">
|
|
<script type="module">import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs'; mermaid.initialize({"startOnLoad":true});</script> <div id="header">
|
|
<div id="toc" class="nav-container toc2" data-component="capy" data-version="">
|
|
<aside class="nav">
|
|
<button class="nav-close"></button>
|
|
<div class="panels">
|
|
<div class="nav-panel-menu is-active" data-panel="menu">
|
|
<nav class="nav-menu">
|
|
<div class="title-row">
|
|
<h3 class="title"><a href="../index.html">Boost.Capy</a></h3>
|
|
<button class="theme-toggle" aria-label="Toggle dark mode" title="Toggle theme" style="display:none">
|
|
<i class="fas fa-sun theme-icon-light"></i>
|
|
<i class="fas fa-moon theme-icon-dark"></i>
|
|
</button> </div>
|
|
<ul class="nav-list">
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../index.html">Introduction</a>
|
|
</li>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../why-capy.html">Why Capy?</a>
|
|
</li>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../quick-start.html">Quick Start</a>
|
|
</li>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../2.cpp20-coroutines/2.intro.html">Introduction To C++20 Coroutines</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../2.cpp20-coroutines/2a.foundations.html">Part I: Foundations</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../2.cpp20-coroutines/2b.syntax.html">Part II: C++20 Syntax</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../2.cpp20-coroutines/2c.machinery.html">Part III: Coroutine Machinery</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../2.cpp20-coroutines/2d.advanced.html">Part IV: Advanced Topics</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../3.concurrency/3.intro.html">Introduction to Concurrency</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../3.concurrency/3a.foundations.html">Part I: Foundations</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../3.concurrency/3b.synchronization.html">Part II: Synchronization</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../3.concurrency/3c.advanced.html">Part III: Advanced Primitives</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../3.concurrency/3d.patterns.html">Part IV: Communication & Patterns</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../4.coroutines/4.intro.html">Coroutines in Capy</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4a.tasks.html">The task Type</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4b.launching.html">Launching Coroutines</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4c.executors.html">Executors and Execution Contexts</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4d.io-awaitable.html">The IoAwaitable Protocol</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4e.cancellation.html">Stop Tokens and Cancellation</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4f.composition.html">Concurrent Composition</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../4.coroutines/4g.allocators.html">Frame Allocators</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../5.buffers/5.intro.html">Buffer Sequences</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5a.overview.html">Why Concepts, Not Spans</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5b.types.html">Buffer Types</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5c.sequences.html">Buffer Sequences</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5d.system-io.html">System I/O Integration</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5e.algorithms.html">Buffer Algorithms</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../5.buffers/5f.dynamic.html">Dynamic Buffers</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../6.streams/6.intro.html">Stream Concepts</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6a.overview.html">Overview</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6b.streams.html">Streams (Partial I/O)</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6c.sources-sinks.html">Sources and Sinks (Complete I/O)</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6d.buffer-concepts.html">Buffer Sources and Sinks</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6e.algorithms.html">Transfer Algorithms</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../6.streams/6f.isolation.html">Physical Isolation</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="7.intro.html">Example Programs</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7a.hello-task.html">Hello Task</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7b.producer-consumer.html">Producer-Consumer</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7c.buffer-composition.html">Buffer Composition</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7d.mock-stream-testing.html">Mock Stream Testing</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7e.type-erased-echo.html">Type-Erased Echo</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7f.timeout-cancellation.html">Timeout with Cancellation</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7g.parallel-fetch.html">Parallel Fetch</a>
|
|
</li>
|
|
<li class=" is-current-page" data-depth="2">
|
|
<a class="nav-link" href="7h.custom-dynamic-buffer.html">Custom Dynamic Buffer</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7i.echo-server-corosio.html">Echo Server with Corosio</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="7j.stream-pipeline.html">Stream Pipeline</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../8.design/8.intro.html">Design</a>
|
|
</li>
|
|
<ul class="nav-list">
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8a.CapyLayering.html">Layered Abstractions</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8b.Separation.html">Why Capy Is Separate</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8c.ReadStream.html">ReadStream</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8d.ReadSource.html">ReadSource</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8e.BufferSource.html">BufferSource</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8f.WriteStream.html">WriteStream</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8g.WriteSink.html">WriteSink</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8h.BufferSink.html">BufferSink</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8i.TypeEraseAwaitable.html">Type-Erasing Awaitables</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8j.any_buffer_sink.html">AnyBufferSink</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8k.Executor.html">Executor</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8l.RunApi.html">Run API</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8m.WhyNotCobalt.html">Why Not Cobalt?</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8n.WhyNotCobaltConcepts.html">Why Not Cobalt Concepts?</a>
|
|
</li>
|
|
<li class="" data-depth="2">
|
|
<a class="nav-link" href="../8.design/8o.WhyNotTMC.html">Why Not TooManyCooks?</a>
|
|
</li>
|
|
</ul>
|
|
<li class="" data-depth="1">
|
|
<a class="nav-link" href="../reference/boost/capy.html">Reference</a>
|
|
</li>
|
|
</ul>
|
|
</ul>
|
|
</nav>
|
|
</div>
|
|
</div>
|
|
</aside>
|
|
</div>
|
|
</div> <div id="content">
|
|
<article class="doc max-width-reset">
|
|
<div class="toolbar" role="navigation">
|
|
<button class="nav-toggle"></button>
|
|
<nav class="breadcrumbs" aria-label="breadcrumbs">
|
|
<ul>
|
|
<li>
|
|
<a href="../index.html" aria-label="Home: Boost.Capy">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="1rem" height="1rem" viewBox="0 -960 960 960" fill="#000000" aria-hidden="true"><path d="M160-120v-480l320-240 320 240v480H560v-280H400v280H160Z"/></svg>
|
|
</a>
|
|
</li>
|
|
<li><a href="7.intro.html">Example Programs</a></li>
|
|
<li><a href="7h.custom-dynamic-buffer.html">Custom Dynamic Buffer</a></li>
|
|
</ul>
|
|
</nav>
|
|
<div class="spirit-nav">
|
|
<a accesskey="p" href="7g.parallel-fetch.html">
|
|
<span class="material-symbols-outlined" title="Previous: Parallel Fetch">arrow_back</span>
|
|
</a>
|
|
<a accesskey="u" href="7.intro.html">
|
|
<span class="material-symbols-outlined" title="Up: Example Programs">arrow_upward</span>
|
|
</a>
|
|
<a accesskey="n" href="7i.echo-server-corosio.html">
|
|
<span class="material-symbols-outlined" title="Next: Echo Server with Corosio">arrow_forward</span>
|
|
</a>
|
|
</div></div>
|
|
<h1 class="page">Custom Dynamic Buffer</h1>
|
|
<div id="preamble">
|
|
<div class="sectionbody">
|
|
<div class="paragraph">
|
|
<p>Implementing the DynamicBuffer concept for a custom allocation strategy.</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_what_you_will_learn"><a class="anchor" href="#_what_you_will_learn"></a>What You Will Learn</h2>
|
|
<div class="sectionbody">
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p>Implementing the DynamicBuffer concept</p>
|
|
</li>
|
|
<li>
|
|
<p>Understanding <code>prepare</code>, <code>commit</code>, <code>consume</code> lifecycle</p>
|
|
</li>
|
|
<li>
|
|
<p>Custom memory management for I/O</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_prerequisites"><a class="anchor" href="#_prerequisites"></a>Prerequisites</h2>
|
|
<div class="sectionbody">
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p>Completed <a href="7g.parallel-fetch.html" class="xref page">Parallel Fetch</a></p>
|
|
</li>
|
|
<li>
|
|
<p>Understanding of dynamic buffers from <a href="../5.buffers/5f.dynamic.html" class="xref page">Dynamic Buffers</a></p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_source_code"><a class="anchor" href="#_source_code"></a>Source Code</h2>
|
|
<div class="sectionbody">
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-cpp hljs" data-lang="cpp">#include <boost/capy.hpp>
|
|
#include <boost/capy/test/run_blocking.hpp>
|
|
#include <boost/capy/test/stream.hpp>
|
|
#include <boost/capy/test/fuse.hpp>
|
|
#include <boost/capy/cond.hpp>
|
|
#include <iostream>
|
|
#include <vector>
|
|
#include <cassert>
|
|
#include <cstring>
|
|
|
|
using namespace boost::capy;
|
|
|
|
// Custom dynamic buffer with statistics tracking
|
|
class tracked_buffer
|
|
{
|
|
std::vector<char> storage_;
|
|
std::size_t read_pos_ = 0; // Start of readable data
|
|
std::size_t write_pos_ = 0; // End of readable data
|
|
std::size_t max_size_;
|
|
|
|
// Statistics
|
|
std::size_t total_prepared_ = 0;
|
|
std::size_t total_committed_ = 0;
|
|
std::size_t total_consumed_ = 0;
|
|
|
|
public:
|
|
explicit tracked_buffer(std::size_t max_size = 65536)
|
|
: max_size_(max_size)
|
|
{
|
|
storage_.reserve(1024);
|
|
}
|
|
|
|
// === DynamicBuffer interface ===
|
|
|
|
// Consumer: readable data
|
|
const_buffer data() const noexcept
|
|
{
|
|
return const_buffer(
|
|
storage_.data() + read_pos_,
|
|
write_pos_ - read_pos_);
|
|
}
|
|
|
|
// Capacity queries
|
|
std::size_t size() const noexcept
|
|
{
|
|
return write_pos_ - read_pos_;
|
|
}
|
|
|
|
std::size_t max_size() const noexcept
|
|
{
|
|
return max_size_;
|
|
}
|
|
|
|
std::size_t capacity() const noexcept
|
|
{
|
|
return storage_.capacity() - read_pos_;
|
|
}
|
|
|
|
// Producer: prepare space for writing
|
|
mutable_buffer prepare(std::size_t n)
|
|
{
|
|
total_prepared_ += n;
|
|
|
|
// Compact if needed
|
|
if (storage_.size() + n > storage_.capacity() && read_pos_ > 0)
|
|
{
|
|
compact();
|
|
}
|
|
|
|
// Grow if needed
|
|
std::size_t required = write_pos_ + n;
|
|
if (required > max_size_)
|
|
throw std::length_error("tracked_buffer: max_size exceeded");
|
|
|
|
if (required > storage_.size())
|
|
storage_.resize(required);
|
|
|
|
return mutable_buffer(
|
|
storage_.data() + write_pos_,
|
|
n);
|
|
}
|
|
|
|
// Producer: mark bytes as written
|
|
void commit(std::size_t n)
|
|
{
|
|
total_committed_ += n;
|
|
write_pos_ += n;
|
|
}
|
|
|
|
// Consumer: mark bytes as processed
|
|
void consume(std::size_t n)
|
|
{
|
|
std::size_t actual = std::min(n, size()); // std::size_t
|
|
total_consumed_ += actual;
|
|
read_pos_ += actual;
|
|
|
|
if (read_pos_ == write_pos_)
|
|
{
|
|
// Buffer empty, reset positions
|
|
read_pos_ = 0;
|
|
write_pos_ = 0;
|
|
}
|
|
}
|
|
|
|
// === Statistics ===
|
|
|
|
void print_stats() const
|
|
{
|
|
std::cout << "Buffer statistics:\n"
|
|
<< " Total prepared: " << total_prepared_ << " bytes\n"
|
|
<< " Total committed: " << total_committed_ << " bytes\n"
|
|
<< " Total consumed: " << total_consumed_ << " bytes\n"
|
|
<< " Current size: " << size() << " bytes\n"
|
|
<< " Capacity: " << capacity() << " bytes\n";
|
|
}
|
|
|
|
private:
|
|
void compact()
|
|
{
|
|
if (read_pos_ == 0)
|
|
return;
|
|
|
|
std::size_t len = write_pos_ - read_pos_;
|
|
std::memmove(storage_.data(), storage_.data() + read_pos_, len);
|
|
read_pos_ = 0;
|
|
write_pos_ = len;
|
|
}
|
|
};
|
|
|
|
// Demonstrate using the custom buffer
|
|
task<> read_into_tracked_buffer(test::stream& stream, tracked_buffer& buffer)
|
|
{
|
|
// Read data in chunks
|
|
while (true)
|
|
{
|
|
auto space = buffer.prepare(256); // mutable_buffer
|
|
// ec: std::error_code, n: std::size_t
|
|
auto [ec, n] = co_await stream.read_some(space);
|
|
|
|
if (ec == cond::eof)
|
|
break;
|
|
|
|
if (ec)
|
|
throw std::system_error(ec);
|
|
|
|
buffer.commit(n);
|
|
|
|
std::cout << "Read " << n << " bytes, buffer size now: "
|
|
<< buffer.size() << "\n";
|
|
}
|
|
}
|
|
|
|
void demo_tracked_buffer()
|
|
{
|
|
std::cout << "=== Tracked Buffer Demo ===\n\n";
|
|
|
|
// Setup mock stream with test data
|
|
test::fuse f;
|
|
test::stream mock(f);
|
|
mock.provide("Hello, ");
|
|
mock.provide("World! ");
|
|
mock.provide("This is a test of the custom buffer.\n");
|
|
// Stream returns eof when data is exhausted
|
|
|
|
tracked_buffer buffer;
|
|
|
|
test::run_blocking()(read_into_tracked_buffer(mock, buffer));
|
|
|
|
std::cout << "\nFinal buffer contents: ";
|
|
auto data = buffer.data(); // const_buffer
|
|
std::cout.write(static_cast<char const*>(data.data()), data.size());
|
|
std::cout << "\n\n";
|
|
|
|
buffer.print_stats();
|
|
|
|
// Consume some data
|
|
std::cout << "\nConsuming 7 bytes...\n";
|
|
buffer.consume(7);
|
|
buffer.print_stats();
|
|
}
|
|
|
|
int main()
|
|
{
|
|
demo_tracked_buffer();
|
|
return 0;
|
|
}</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_build"><a class="anchor" href="#_build"></a>Build</h2>
|
|
<div class="sectionbody">
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-cmake hljs" data-lang="cmake">add_executable(custom_dynamic_buffer custom_dynamic_buffer.cpp)
|
|
target_link_libraries(custom_dynamic_buffer PRIVATE capy)</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_walkthrough"><a class="anchor" href="#_walkthrough"></a>Walkthrough</h2>
|
|
<div class="sectionbody">
|
|
<div class="sect2">
|
|
<h3 id="_dynamicbuffer_requirements"><a class="anchor" href="#_dynamicbuffer_requirements"></a>DynamicBuffer Requirements</h3>
|
|
<div class="paragraph">
|
|
<p>A DynamicBuffer must provide:</p>
|
|
</div>
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-cpp hljs" data-lang="cpp">// Consumer interface
|
|
const_buffer data() const; // Readable data
|
|
void consume(std::size_t n); // Mark bytes as processed
|
|
|
|
// Producer interface
|
|
mutable_buffer prepare(std::size_t n); // Space for writing
|
|
void commit(std::size_t n); // Mark bytes as written
|
|
|
|
// Capacity queries
|
|
std::size_t size() const; // Readable bytes
|
|
std::size_t max_size() const; // Maximum allowed
|
|
std::size_t capacity() const; // Currently allocated</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect2">
|
|
<h3 id="_the_producerconsumer_flow"><a class="anchor" href="#_the_producerconsumer_flow"></a>The Producer/Consumer Flow</h3>
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-cpp hljs" data-lang="cpp">// 1. Producer prepares space
|
|
auto space = buffer.prepare(256); // mutable_buffer
|
|
|
|
// 2. Data is written into space
|
|
// ec: std::error_code, n: std::size_t
|
|
auto [ec, n] = co_await stream.read_some(space);
|
|
|
|
// 3. Producer commits written bytes
|
|
buffer.commit(n);
|
|
|
|
// 4. Consumer reads data
|
|
auto data = buffer.data(); // const_buffer
|
|
process(data);
|
|
|
|
// 5. Consumer marks bytes as processed
|
|
buffer.consume(processed_bytes);</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect2">
|
|
<h3 id="_memory_management"><a class="anchor" href="#_memory_management"></a>Memory Management</h3>
|
|
<div class="paragraph">
|
|
<p>The <code>tracked_buffer</code> implementation:</p>
|
|
</div>
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p>Uses a single contiguous vector</p>
|
|
</li>
|
|
<li>
|
|
<p>Tracks read and write positions</p>
|
|
</li>
|
|
<li>
|
|
<p>Compacts when needed to reuse space</p>
|
|
</li>
|
|
<li>
|
|
<p>Grows on demand up to <code>max_size</code></p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_output"><a class="anchor" href="#_output"></a>Output</h2>
|
|
<div class="sectionbody">
|
|
<div class="listingblock">
|
|
<div class="content">
|
|
<pre class="highlightjs highlight"><code class="language-asciidoc hljs" data-lang="asciidoc">=== Tracked Buffer Demo ===
|
|
|
|
Read 51 bytes, buffer size now: 51
|
|
|
|
Final buffer contents: Hello, World! This is a test of the custom buffer.
|
|
|
|
|
|
Buffer statistics:
|
|
Total prepared: 512 bytes
|
|
Total committed: 51 bytes
|
|
Total consumed: 0 bytes
|
|
Current size: 51 bytes
|
|
Capacity: 1024 bytes
|
|
|
|
Consuming 7 bytes...
|
|
Buffer statistics:
|
|
Total prepared: 512 bytes
|
|
Total committed: 51 bytes
|
|
Total consumed: 7 bytes
|
|
Current size: 44 bytes
|
|
Capacity: 1017 bytes</code></pre>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_exercises"><a class="anchor" href="#_exercises"></a>Exercises</h2>
|
|
<div class="sectionbody">
|
|
<div class="olist arabic">
|
|
<ol class="arabic">
|
|
<li>
|
|
<p>Add a "high water mark" statistic that tracks maximum buffer size reached</p>
|
|
</li>
|
|
<li>
|
|
<p>Implement a ring buffer version that never moves data</p>
|
|
</li>
|
|
<li>
|
|
<p>Add an allocator parameter for custom memory allocation</p>
|
|
</li>
|
|
</ol>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="sect1">
|
|
<h2 id="_next_steps"><a class="anchor" href="#_next_steps"></a>Next Steps</h2>
|
|
<div class="sectionbody">
|
|
<div class="ulist">
|
|
<ul>
|
|
<li>
|
|
<p><a href="7i.echo-server-corosio.html" class="xref page">Echo Server with Corosio</a> — Real networking</p>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="edit-this-page">
|
|
<a href="https://github.com/cppalliance/capy/edit/develop/doc/modules/ROOT/pages/7.examples/7h.custom-dynamic-buffer.adoc">Edit this Page</a>
|
|
</div>
|
|
<nav class="pagination">
|
|
<span class="prev"><a href="7g.parallel-fetch.html">Parallel Fetch</a></span>
|
|
<span class="next"><a href="7i.echo-server-corosio.html">Echo Server with Corosio</a></span>
|
|
</nav>
|
|
</article>
|
|
</div>
|
|
<div id="footer">
|
|
<script id="site-script" src="../../_/js/site.js" data-ui-root-path="../../_"></script>
|
|
<script async src="../../_/js/vendor/highlight.js"></script>
|
|
<script async src="../../_/js/vendor/tabs.js" data-sync-storage-key="preferred-tab"></script>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|