| 1 | // |
|---|---|
| 2 | // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com) |
| 3 | // |
| 4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | // |
| 7 | // Official repository: https://github.com/boostorg/beast |
| 8 | // |
| 9 | |
| 10 | #ifndef BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP |
| 11 | #define BOOST_BEAST_IMPL_FLAT_STATIC_BUFFER_IPP |
| 12 | |
| 13 | #include <boost/beast/core/flat_static_buffer.hpp> |
| 14 | #include <boost/throw_exception.hpp> |
| 15 | #include <algorithm> |
| 16 | #include <cstring> |
| 17 | #include <iterator> |
| 18 | #include <memory> |
| 19 | #include <stdexcept> |
| 20 | |
| 21 | namespace boost { |
| 22 | namespace beast { |
| 23 | |
| 24 | /* Layout: |
| 25 | |
| 26 | begin_ in_ out_ last_ end_ |
| 27 | |<------->|<---------->|<---------->|<------->| |
| 28 | | readable | writable | |
| 29 | */ |
| 30 | |
| 31 | void |
| 32 | flat_static_buffer_base:: |
| 33 | clear() noexcept |
| 34 | { |
| 35 | in_ = begin_; |
| 36 | out_ = begin_; |
| 37 | last_ = begin_; |
| 38 | } |
| 39 | |
| 40 | auto |
| 41 | flat_static_buffer_base:: |
| 42 | prepare(std::size_t n) -> |
| 43 | mutable_buffers_type |
| 44 | { |
| 45 | if(n <= dist(first: out_, last: end_)) |
| 46 | { |
| 47 | last_ = out_ + n; |
| 48 | return {out_, n}; |
| 49 | } |
| 50 | auto const len = size(); |
| 51 | if(n > capacity() - len) |
| 52 | BOOST_THROW_EXCEPTION(std::length_error{ |
| 53 | "buffer overflow"}); |
| 54 | if(len > 0) |
| 55 | std::memmove(dest: begin_, src: in_, n: len); |
| 56 | in_ = begin_; |
| 57 | out_ = in_ + len; |
| 58 | last_ = out_ + n; |
| 59 | return {out_, n}; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | flat_static_buffer_base:: |
| 64 | consume(std::size_t n) noexcept |
| 65 | { |
| 66 | if(n >= size()) |
| 67 | { |
| 68 | in_ = begin_; |
| 69 | out_ = in_; |
| 70 | return; |
| 71 | } |
| 72 | in_ += n; |
| 73 | } |
| 74 | |
| 75 | void |
| 76 | flat_static_buffer_base:: |
| 77 | reset(void* p, std::size_t n) noexcept |
| 78 | { |
| 79 | begin_ = static_cast<char*>(p); |
| 80 | in_ = begin_; |
| 81 | out_ = begin_; |
| 82 | last_ = begin_; |
| 83 | end_ = begin_ + n; |
| 84 | } |
| 85 | |
| 86 | } // beast |
| 87 | } // boost |
| 88 | |
| 89 | #endif |
