Line data Source code
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_WORKSPACE_HPP 11 : #define BOOST_HTTP_PROTO_DETAIL_WORKSPACE_HPP 12 : 13 : #include <boost/http_proto/detail/except.hpp> 14 : #include <boost/assert.hpp> 15 : #include <cstdlib> 16 : #include <new> 17 : #include <utility> 18 : #include <stddef.h> // ::max_align_t 19 : 20 : namespace boost { 21 : namespace http_proto { 22 : namespace detail { 23 : 24 : /** A contiguous buffer of storage used by algorithms. 25 : 26 : Objects of this type retain ownership of a 27 : contiguous buffer of storage allocated upon 28 : construction. This storage is divided into 29 : three regions: 30 : 31 : @li The reserved area, which starts at the 32 : beginning of the buffer and can grow 33 : upwards towards the end of the buffer. 34 : 35 : @li The acquired area, which starts at the 36 : end of the buffer and can grow downwards 37 : towards the beginning of the buffer. 38 : 39 : @li The unused area, which starts from the 40 : end of the reserved area and stretches 41 : until the beginning of the acquired area. 42 : */ 43 : class workspace 44 : { 45 : unsigned char* base_ = nullptr; 46 : unsigned char* begin_ = nullptr; 47 : unsigned char* head_ = nullptr; 48 : unsigned char* end_ = nullptr; 49 : 50 : template<class> 51 : struct any_impl; 52 : struct any; 53 : struct undo; 54 : 55 : public: 56 : /** Return the number of aligned bytes required for T 57 : */ 58 : template<class T> 59 : static 60 : constexpr 61 : std::size_t 62 : space_needed(); 63 : 64 : /** Destructor. 65 : */ 66 : ~workspace(); 67 : 68 : /** Constructor. 69 : 70 : @param n The number of bytes of storage 71 : to allocate for the internal buffer. 72 : */ 73 : explicit 74 : workspace( 75 : std::size_t n); 76 : 77 : /** Constructor. 78 : */ 79 745 : workspace() = default; 80 : 81 : /** Constructor. 82 : */ 83 : workspace(workspace&&) noexcept; 84 : 85 : /** Allocate internal storage. 86 : 87 : @throws std::logic_error this->size() > 0 88 : 89 : @throws std::invalid_argument n == 0 90 : */ 91 : void 92 : allocate( 93 : std::size_t n); 94 : 95 : /** Return a pointer to the unused area. 96 : */ 97 : void* 98 7958 : data() noexcept 99 : { 100 7958 : return begin_; 101 : } 102 : 103 : /** Return the size of the unused area. 104 : */ 105 : std::size_t 106 20 : size() const noexcept 107 : { 108 20 : return head_ - begin_; 109 : } 110 : 111 : /** Clear the contents while preserving capacity. 112 : */ 113 : BOOST_HTTP_PROTO_DECL 114 : void 115 : clear() noexcept; 116 : 117 : /** Convert unused storage to reserved storage. 118 : 119 : @throws std::invalid_argument n >= this->size() 120 : */ 121 : BOOST_HTTP_PROTO_DECL 122 : void 123 : reserve(std::size_t n); 124 : 125 : template<class T> 126 : typename std::decay<T>::type& 127 : push(T&& t); 128 : 129 : template<class T> 130 : T* 131 : push_array( 132 : std::size_t n, 133 : T const& t); 134 : 135 : private: 136 : BOOST_HTTP_PROTO_DECL 137 : void* 138 : bump_down( 139 : std::size_t size, 140 : std::size_t align); 141 : }; 142 : 143 : } // detail 144 : } // http_proto 145 : } // boost 146 : 147 : #include <boost/http_proto/detail/impl/workspace.hpp> 148 : 149 : #endif