| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.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/cppalliance/http_proto | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_HTTP_PROTO_DETAIL_IMPL_WORKSPACE_HPP | ||
| 11 | #define BOOST_HTTP_PROTO_DETAIL_IMPL_WORKSPACE_HPP | ||
| 12 | |||
| 13 | #include <boost/config.hpp> | ||
| 14 | |||
| 15 | namespace boost { | ||
| 16 | namespace http_proto { | ||
| 17 | namespace detail { | ||
| 18 | |||
| 19 | #if defined(BOOST_MSVC) | ||
| 20 | #pragma warning(push) | ||
| 21 | #pragma warning(disable : 4324) /* structure was padded due to __declspec(align()) */ | ||
| 22 | #endif | ||
| 23 | |||
| 24 | struct workspace::any | ||
| 25 | { | ||
| 26 | any* next = nullptr; | ||
| 27 | |||
| 28 | BOOST_HTTP_PROTO_DECL | ||
| 29 | virtual ~any() = 0; | ||
| 30 | }; | ||
| 31 | |||
| 32 | template<class U> | ||
| 33 | struct alignas(::max_align_t) | ||
| 34 | workspace::any_impl : any | ||
| 35 | { | ||
| 36 | U u; | ||
| 37 | |||
| 38 | any_impl(any_impl const&) = delete; | ||
| 39 | any_impl(any_impl&&) = delete; | ||
| 40 | |||
| 41 | template<class... Args> | ||
| 42 | 1856 | explicit any_impl(Args&&... args) | |
| 43 |
3/6✗ Branch 6 not taken.
✓ Branch 7 taken 52 times.
✗ Branch 8 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 72 times.
✓ Branch 3 taken 16 times.
|
1856 | : u(std::forward<Args>(args)...) |
| 44 | { | ||
| 45 | 1856 | } | |
| 46 | }; | ||
| 47 | |||
| 48 | struct workspace::undo | ||
| 49 | { | ||
| 50 | explicit | ||
| 51 | 1011 | undo(workspace& ws0) noexcept | |
| 52 | 1011 | : ws_(ws0) | |
| 53 | 1011 | , head_(ws0.head_) | |
| 54 | { | ||
| 55 | 1011 | } | |
| 56 | |||
| 57 | 1011 | ~undo() | |
| 58 | { | ||
| 59 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1011 times.
|
1011 | if(head_) |
| 60 | ✗ | ws_.head_ = head_; | |
| 61 | 1011 | } | |
| 62 | |||
| 63 | void | ||
| 64 | 1011 | commit() noexcept | |
| 65 | { | ||
| 66 | 1011 | head_ = nullptr; | |
| 67 | 1011 | } | |
| 68 | |||
| 69 | private: | ||
| 70 | workspace& ws_; | ||
| 71 | unsigned char* head_; | ||
| 72 | }; | ||
| 73 | |||
| 74 | template<class T> | ||
| 75 | constexpr | ||
| 76 | std::size_t | ||
| 77 | 2 | workspace:: | |
| 78 | space_needed() | ||
| 79 | { | ||
| 80 | using U = typename std::decay<T>::type; | ||
| 81 | |||
| 82 | static_assert( | ||
| 83 | alignof(U) <= alignof(::max_align_t), | ||
| 84 | "Overaligned types not supported"); | ||
| 85 | |||
| 86 | 2 | return sizeof(any_impl<U>); | |
| 87 | } | ||
| 88 | |||
| 89 | template<class T, class... Args> | ||
| 90 | auto | ||
| 91 | 1856 | workspace:: | |
| 92 | emplace(Args&&... args) -> | ||
| 93 | typename std::decay<T>::type& | ||
| 94 | { | ||
| 95 | static_assert( | ||
| 96 | alignof(T) <= alignof(::max_align_t), | ||
| 97 | "Overaligned types not supported"); | ||
| 98 | |||
| 99 | using U = any_impl<typename | ||
| 100 | std::decay<T>::type>; | ||
| 101 | |||
| 102 | 1856 | undo u(*this); | |
| 103 | 1856 | auto prev_head = head_; | |
| 104 |
1/2✓ Branch 1 taken 928 times.
✗ Branch 2 not taken.
|
1856 | head_ = bump_down(sizeof(U), alignof(U)); |
| 105 |
3/8✗ Branch 6 not taken.
✓ Branch 7 taken 52 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 72 times.
✓ Branch 3 taken 407 times.
|
1856 | auto p = ::new(head_) U( |
| 106 | std::forward<Args>(args)...); | ||
| 107 | 1856 | u.commit(); | |
| 108 | 1856 | p->next = reinterpret_cast< | |
| 109 | any*>(prev_head); | ||
| 110 | 1856 | return p->u; | |
| 111 | 1856 | } | |
| 112 | |||
| 113 | template<class T> | ||
| 114 | T* | ||
| 115 | 83 | workspace:: | |
| 116 | push_array( | ||
| 117 | std::size_t n, | ||
| 118 | T const& t) | ||
| 119 | { | ||
| 120 | struct alignas(::max_align_t) | ||
| 121 | U : any | ||
| 122 | { | ||
| 123 | std::size_t n_ = 0; | ||
| 124 | |||
| 125 | 83 | U() = default; | |
| 126 | 166 | ~U() | |
| 127 | { | ||
| 128 | 166 | for(std::size_t i = n_; | |
| 129 |
2/2✓ Branch 0 taken 269 times.
✓ Branch 1 taken 83 times.
|
704 | i-- > 0;) |
| 130 | 538 | data()[i].~T(); | |
| 131 | 332 | } | |
| 132 | |||
| 133 | 83 | U( std::size_t n, | |
| 134 | T const& t) | ||
| 135 | 83 | : U() | |
| 136 | { | ||
| 137 |
2/2✓ Branch 0 taken 269 times.
✓ Branch 1 taken 83 times.
|
352 | while(n_ < n) |
| 138 | { | ||
| 139 | 269 | new(&data()[n_]) T(t); | |
| 140 | 269 | ++n_; | |
| 141 | } | ||
| 142 | 83 | } | |
| 143 | |||
| 144 | 621 | T* data() noexcept | |
| 145 | { | ||
| 146 | return reinterpret_cast< | ||
| 147 | 621 | T*>(this + 1); | |
| 148 | } | ||
| 149 | }; | ||
| 150 | |||
| 151 | 83 | undo u(*this); | |
| 152 | 83 | auto prev_head = head_; | |
| 153 | 166 | head_ = bump_down( | |
| 154 |
1/2✓ Branch 1 taken 83 times.
✗ Branch 2 not taken.
|
83 | sizeof(U) + n * sizeof(T), |
| 155 | alignof(::max_align_t)); | ||
| 156 | 83 | auto p = ::new(head_) U(n, t); | |
| 157 | 83 | u.commit(); | |
| 158 | 83 | p->next = reinterpret_cast< | |
| 159 | any*>(prev_head); | ||
| 160 | 166 | return p->data(); | |
| 161 | 83 | } | |
| 162 | |||
| 163 | #if defined(BOOST_MSVC) | ||
| 164 | #pragma warning(pop) /* C4324 */ | ||
| 165 | #endif | ||
| 166 | |||
| 167 | } // detail | ||
| 168 | } // http_proto | ||
| 169 | } // boost | ||
| 170 | |||
| 171 | #endif | ||
| 172 |