diff --git a/include/xo/arena/DCircularBuffer.hpp b/include/xo/arena/DCircularBuffer.hpp index 0cb007a..30a3896 100644 --- a/include/xo/arena/DCircularBuffer.hpp +++ b/include/xo/arena/DCircularBuffer.hpp @@ -39,8 +39,8 @@ namespace xo { using size_type = std::size_t; using byte = std::byte; /** a contiguous addres range **/ - using span_type = span; - using const_span_type = span; + using span_type = span; + using const_span_type = span; ///@} @@ -162,7 +162,7 @@ namespace xo { * * Require: @p hi < @ref reserved_range_.hi **/ - bool _expand_to(byte * hi); + bool _expand_to(char * hi); /** shrink occupied rnage to the smallest contiguous range that contains both: * all of .input_range_, and all pinned ranges in .pinned_spans_ diff --git a/include/xo/arena/span.hpp b/include/xo/arena/span.hpp index c038fce..b919266 100644 --- a/include/xo/arena/span.hpp +++ b/include/xo/arena/span.hpp @@ -88,6 +88,14 @@ namespace xo { return span(lo, hi); } + /** @brief create span from raw memory **/ + static span from_memory(span span_memory) { + CharT * lo = (CharT *)span_memory.lo(); + CharT * hi = (CharT *)span_memory.hi(); + + return span(lo, hi); + } + /** @brief concatenate two contiguous spans */ static span concat(const span & span1, const span & span2) { if (span1.is_null()) diff --git a/src/arena/DCircularBuffer.cpp b/src/arena/DCircularBuffer.cpp index 5ad7e02..e00aaaf 100644 --- a/src/arena/DCircularBuffer.cpp +++ b/src/arena/DCircularBuffer.cpp @@ -48,17 +48,19 @@ namespace xo { log && log(xtag("page_z", page_z), xtag("align_z", align_z)); - auto span = mmap_util::map_aligned_range(config.max_capacity_, - align_z, - enable_hugepage_flag, - config.debug_flag_); + auto mapped_span + = span::from_memory(mmap_util::map_aligned_range + (config.max_capacity_, + align_z, + enable_hugepage_flag, + config.debug_flag_)); - if (!span.lo()) { + if (!mapped_span.lo()) { throw std::runtime_error(tostr("DCircularBuffer: reserve address range failed", xtag("size", config.max_capacity_))); } - return DCircularBuffer(config, page_z, align_z, span); + return DCircularBuffer(config, page_z, align_z, mapped_span); } DCircularBuffer::DCircularBuffer(const CircularBufferConfig & config, @@ -302,7 +304,8 @@ namespace xo { } bool - DCircularBuffer::_expand_to(byte * hi) { + DCircularBuffer::_expand_to(char * hi) + { scope log(XO_DEBUG(config_.debug_flag_)); if (hi < mapped_range_.hi()) { @@ -312,7 +315,7 @@ namespace xo { size_t add_z = hi - mapped_range_.hi(); size_t add_commit_z = padding::with_padding(add_z, buffer_align_z_); - byte * commit_start = mapped_range_.hi(); + char * commit_start = mapped_range_.hi(); if (::mprotect(commit_start, add_commit_z, diff --git a/utest/DCircularBuffer.test.cpp b/utest/DCircularBuffer.test.cpp index be86947..2d8df93 100644 --- a/utest/DCircularBuffer.test.cpp +++ b/utest/DCircularBuffer.test.cpp @@ -34,18 +34,16 @@ namespace xo { REQUIRE(buf.occupied_range().size() == 0); REQUIRE(buf.input_range().size() == 0); - std::string_view s0 = "abcdefghijk"; + auto s0 = DCircularBuffer::const_span_type::from_cstr("abcdefghijk"); /* return value is unaccepted suffix of input */ - REQUIRE(buf.append(DCircularBuffer::span_type((byte *)s0.begin(), - (byte *)s0.end())).empty()); + REQUIRE(buf.append(s0).empty()); REQUIRE(buf.verify_ok(verify_policy::log_only())); REQUIRE(buf.mapped_range().size() == getpagesize()); REQUIRE(buf.occupied_range().size() == s0.size()); REQUIRE(buf.input_range().size() == s0.size()); - std::string_view s1 = "lmnopq"; - REQUIRE(buf.append(DCircularBuffer::span_type((byte *)s1.begin(), - (byte *)s1.end())).empty()); + auto s1 = DCircularBuffer::const_span_type::from_cstr("lmnopq"); + REQUIRE(buf.append(s1).empty()); REQUIRE(buf.mapped_range().size() == getpagesize()); REQUIRE(buf.occupied_range().size() == s0.size() + s1.size());