Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copied strings deprecation #71

Merged
Merged
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
5 changes: 5 additions & 0 deletions include/boost/http_proto/fields_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
namespace boost {
namespace http_proto {

namespace detail {
struct prefix_op;
} // detail

/** Mixin for modifiable HTTP fields

@par Iterators
Expand Down Expand Up @@ -46,6 +50,7 @@ class BOOST_SYMBOL_VISIBLE
friend class serializer;
friend class message_base;
friend struct detail::header;
friend struct detail::prefix_op;

BOOST_HTTP_PROTO_DECL
explicit
Expand Down
4 changes: 1 addition & 3 deletions include/boost/http_proto/message_base.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//
// Copyright (c) 2021 Vinnie Falco ([email protected])
// Copyright (c) 2024 Christian Mazakas
//
// 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)
Expand Down Expand Up @@ -93,9 +94,6 @@ class BOOST_SYMBOL_VISIBLE
BOOST_HTTP_PROTO_DECL
void
set_keep_alive(bool value);

private:
char* set_prefix_impl(std::size_t);
};

} // http_proto
Expand Down
32 changes: 32 additions & 0 deletions src/detail/align_up.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Copyright (c) 2024 Christian Mazakas
//
// 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/CPPAlliance/http_proto
//

#ifndef BOOST_HTTP_PROTO_DETAIL_ALIGN_UP_HPP
#define BOOST_HTTP_PROTO_DETAIL_ALIGN_UP_HPP

#include <cstddef>

namespace boost {
namespace http_proto {
namespace detail {

constexpr
inline
std::size_t
align_up(std::size_t s, std::size_t A)
{
return A * (
(s + A - 1) / A);
}

} // detail
} // http_proto
} // boost

#endif
123 changes: 0 additions & 123 deletions src/detail/copied_strings.hpp

This file was deleted.

6 changes: 3 additions & 3 deletions src/detail/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <string>
#include <utility>

#include "align_up.hpp"

namespace boost {
namespace http_proto {
namespace detail {
Expand Down Expand Up @@ -209,9 +211,7 @@ bytes_needed(
size = 19;
static constexpr auto A =
alignof(header::entry);
// round up to alignof(A)
return A * (
(size + A - 1) / A) +
return align_up(size, A) +
(count * sizeof(
header::entry));
}
Expand Down
111 changes: 111 additions & 0 deletions src/detail/header.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// Copyright (c) 2024 Christian Mazakas
//
// 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/CPPAlliance/http_proto
//

#ifndef BOOST_HTTP_PROTO_SRC_DETAIL_HEADER_HPP
#define BOOST_HTTP_PROTO_SRC_DETAIL_HEADER_HPP

#include <boost/http_proto/detail/except.hpp>
#include <boost/http_proto/message_base.hpp>
#include <boost/http_proto/detail/header.hpp>
#include <boost/core/span.hpp>
#include <cstddef>

namespace boost {
namespace http_proto {
namespace detail {

struct prefix_op
{
message_base& mb_;
span<char> prefix_;
char* buf_ = nullptr;
std::size_t n_ = 0;

prefix_op(
message_base& mb,
std::size_t n)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would noexcept help?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't apply noexcept here because this function allocates and it also enforces a length check via detail::throw_length_error().

: mb_{mb}
, n_{n}
{
auto& h = mb_.h_;
if( h.buf && n <= h.prefix )
{
prefix_ = {h.buf, n};
return;
}

// allocate or grow
if( n > h.prefix &&
static_cast<std::size_t>(n - h.prefix) >
static_cast<std::size_t>(max_offset - h.size) )
{
detail::throw_length_error();
}

auto n1 = detail::header::bytes_needed(
n + h.size - h.prefix,
h.count);

auto p = new char[n1];
if( h.buf != nullptr )
{
std::memcpy(
p + n,
h.buf + h.prefix,
h.size - h.prefix);
h.copy_table(p + n1);
}
else
{
std::memcpy(
p + n,
h.cbuf + h.prefix,
h.size - h.prefix);
}

prefix_ = {p, n};
buf_ = h.buf;

h.buf = p;
h.cbuf = p;
h.size = static_cast<
offset_type>(h.size +
n - h.prefix);
h.prefix = static_cast<
offset_type>(n);
h.cap = n1;
}

prefix_op(prefix_op&&) = delete;
prefix_op(prefix_op const&) = delete;

~prefix_op()
{
auto& h = mb_.h_;
if( n_ < h.prefix )
{
std::memmove(
h.buf + n_,
h.buf + h.prefix,
h.size - h.prefix);
h.size = static_cast<
offset_type>(h.size -
h.prefix + n_);
h.prefix = static_cast<
offset_type>(n_);
}
delete[] buf_;
}
};

} // detail
} // http_proto
} // boost

#endif // BOOST_HTTP_PROTO_SRC_DETAIL_HEADER_HPP
Loading
Loading