summaryrefslogtreecommitdiff
path: root/boost/beast/core/detail/impl/temporary_buffer.ipp
blob: 932e8473712c567ad14b1f97dc7466bb9d782728 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Copyright (c) 2019 Damian Jarek(damian.jarek93@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/boostorg/beast
//

#ifndef BOOST_BEAST_DETAIL_IMPL_TEMPORARY_BUFFER_IPP
#define BOOST_BEAST_DETAIL_IMPL_TEMPORARY_BUFFER_IPP

#include <boost/beast/core/detail/temporary_buffer.hpp>
#include <boost/beast/core/detail/clamp.hpp>
#include <boost/core/exchange.hpp>
#include <boost/assert.hpp>
#include <memory>
#include <cstring>

namespace boost {
namespace beast {
namespace detail {

void
temporary_buffer::
append(string_view s)
{
    grow(s.size());
    unchecked_append(s);
}

void
temporary_buffer::
append(string_view s1, string_view s2)
{
    grow(s1.size() + s2.size());
    unchecked_append(s1);
    unchecked_append(s2);
}

void
temporary_buffer::
unchecked_append(string_view s)
{
    auto n = s.size();
    std::memcpy(&data_[size_], s.data(), n);
    size_ += n;
}

void
temporary_buffer::
grow(std::size_t n)
{
    if (capacity_ - size_ >= n)
        return;

    auto const capacity = (n + size_) * 2u;
    BOOST_ASSERT(! detail::sum_exceeds(
        n, size_, capacity));
    char* const p = new char[capacity];
    std::memcpy(p, data_, size_);
    deallocate(boost::exchange(data_, p));
    capacity_ = capacity;
}
} // detail
} // beast
} // boost

#endif