detail/impl/stack.hpp

100.0% Lines (55/55) 75.6% Functions (236/312)
Line TLA Hits 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/boostorg/json
8 //
9
10 #ifndef BOOST_JSON_DETAIL_IMPL_STACK_HPP
11 #define BOOST_JSON_DETAIL_IMPL_STACK_HPP
12
13 #include <boost/core/detail/static_assert.hpp>
14 #include <memory>
15
16 namespace boost {
17 namespace json {
18 namespace detail {
19
20 template<>
21 struct stack::non_trivial<void>
22 {
23 using relocate_t = non_trivial* (*) (non_trivial*, void*);
24
25 relocate_t rel;
26 non_trivial* next;
27 std::size_t offset;
28
29 BOOST_JSON_DECL
30 non_trivial<>*
31 destroy() noexcept;
32
33 BOOST_JSON_DECL
34 non_trivial*
35 relocate(void* dst) noexcept;
36
37 protected:
38 ~non_trivial() = default;
39 };
40
41 template< class T >
42 struct stack::non_trivial
43 : stack::non_trivial<void>
44 {
45 T obj;
46
47 explicit
48 4x non_trivial(T t, non_trivial<>* next, std::size_t offset)
49 4x : non_trivial<void>{relocate, next, offset}, obj( std::move(t) )
50 4x {}
51
52 static
53 non_trivial<>*
54 4x relocate(non_trivial<>* src, void* dest) noexcept
55 {
56 4x non_trivial* self = static_cast<non_trivial*>(src);
57 4x non_trivial<>* result = nullptr;
58 4x if( dest )
59 3x result = ::new(dest) non_trivial( std::move(*self) );
60 4x self->~non_trivial();
61 4x return result;
62 }
63 };
64
65 template<class T>
66 void
67 332068x stack::
68 push_unchecked(T const& t)
69 {
70 332068x constexpr std::size_t n = sizeof(T);
71 BOOST_CORE_STATIC_ASSERT( is_trivially_copy_assignable<T>::value );
72 332068x BOOST_ASSERT( n <= cap_ - size_ );
73 332068x std::memcpy( base_ + size_, &t, n );
74 332068x size_ += n;
75 332068x }
76
77 template<class T>
78 void
79 795418x stack::
80 peek(T& t)
81 {
82 795418x constexpr std::size_t n = sizeof(T);
83 BOOST_CORE_STATIC_ASSERT( is_trivially_copy_assignable<T>::value );
84 795418x BOOST_ASSERT( size_ >= n );
85 795418x std::memcpy( &t, base_ + size_ - n, n );
86 795418x }
87
88 //--------------------------------------
89
90 // trivial
91 template<class T>
92 void
93 47892x stack::
94 push(T const& t, std::true_type)
95 {
96 47892x if( sizeof(T) > cap_ - size_ )
97 8953x reserve_impl( sizeof(T) + size_ );
98 47890x push_unchecked(t);
99 47890x }
100
101 // non-trivial
102 template<class T>
103 void
104 4x stack::
105 push(T&& t, std::false_type)
106 {
107 BOOST_CORE_STATIC_ASSERT( ! is_trivially_copy_assignable<T>::value );
108
109 using Holder = non_trivial< remove_cvref<T> >;
110 4x constexpr std::size_t size = sizeof(Holder);
111 4x constexpr std::size_t alignment = alignof(Holder);
112
113 void* ptr;
114 std::size_t offset;
115 do
116 {
117 7x std::size_t space = cap_ - size_;
118 7x unsigned char* buf = base_ + size_;
119 7x ptr = buf;
120 7x if( std::align(alignment, size, ptr, space) )
121 {
122 4x offset = (reinterpret_cast<unsigned char*>(ptr) - buf) + size;
123 4x break;
124 }
125
126 3x reserve_impl(size_ + size + alignment - 1);
127 3x }
128 while(true);
129 4x BOOST_ASSERT(
130 (reinterpret_cast<unsigned char*>(ptr) + size - offset) ==
131 (base_ + size_) );
132
133 4x head_ = ::new(ptr) Holder( static_cast<T&&>(t), head_, offset );
134 4x size_ += offset;
135 4x }
136
137 // trivial
138 template<class T>
139 void
140 328587x stack::
141 pop(T& t, std::true_type)
142 {
143 328587x BOOST_ASSERT( size_ >= sizeof(T) );
144 328587x peek(t);
145 328587x size_ -= sizeof(T);
146 328587x }
147
148 // non-trivial
149 template<class T>
150 void
151 3x stack::
152 pop(T& t, std::false_type)
153 {
154 3x auto next = head_->next;
155 3x auto offset = head_->offset;
156
157 using U = remove_cvref<T>;
158 using Holder = non_trivial<U>;
159 3x auto const head = static_cast<Holder*>(head_);
160
161 3x t = std::move( head->obj );
162 3x head->~Holder();
163
164 3x head_ = next;
165 3x size_ -= offset;
166 3x }
167
168 } // detail
169 } // json
170 } // boost
171
172 #endif
173