detail/impl/stack.ipp
100.0% Lines (43/43)
100.0% List of functions (6/6)
Functions (6)
Function
Calls
Lines
Branches
Blocks
boost::json::detail::stack::non_trivial<void>::destroy()
:20
1x
100.0%
–
100.0%
boost::json::detail::stack::non_trivial<void>::relocate(void*)
:28
3x
100.0%
–
100.0%
boost::json::detail::stack::~stack()
:34
2185850x
100.0%
–
100.0%
boost::json::detail::stack::stack(boost::json::storage_ptr, unsigned char*, unsigned long)
:43
21245x
100.0%
–
100.0%
boost::json::detail::stack::clear()
:56
6360224x
100.0%
–
100.0%
boost::json::detail::stack::reserve_impl(unsigned long)
:65
124748x
100.0%
–
92.0%
| 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_IPP | ||
| 11 | #define BOOST_JSON_DETAIL_IMPL_STACK_IPP | ||
| 12 | |||
| 13 | #include <boost/json/detail/stack.hpp> | ||
| 14 | |||
| 15 | namespace boost { | ||
| 16 | namespace json { | ||
| 17 | namespace detail { | ||
| 18 | |||
| 19 | stack::non_trivial<>* | ||
| 20 | 1x | stack::non_trivial<>::destroy() noexcept | |
| 21 | { | ||
| 22 | 1x | non_trivial* const result = next; | |
| 23 | 1x | rel(this, nullptr); | |
| 24 | 1x | return result; | |
| 25 | } | ||
| 26 | |||
| 27 | stack::non_trivial<>* | ||
| 28 | 3x | stack::non_trivial<>::relocate(void* dst) noexcept | |
| 29 | { | ||
| 30 | 3x | return rel(this, dst); | |
| 31 | } | ||
| 32 | |||
| 33 | |||
| 34 | 2185850x | stack:: | |
| 35 | ~stack() | ||
| 36 | { | ||
| 37 | 2185850x | clear(); | |
| 38 | 2185850x | if(base_ != buf_) | |
| 39 | 118086x | sp_->deallocate( | |
| 40 | 118086x | base_, cap_); | |
| 41 | 2185850x | } | |
| 42 | |||
| 43 | 21245x | stack:: | |
| 44 | stack( | ||
| 45 | storage_ptr sp, | ||
| 46 | unsigned char* buf, | ||
| 47 | 21245x | std::size_t buf_size) noexcept | |
| 48 | 21245x | : sp_(std::move(sp)) | |
| 49 | 21245x | , cap_(buf_size) | |
| 50 | 21245x | , base_(buf) | |
| 51 | 21245x | , buf_(buf) | |
| 52 | { | ||
| 53 | 21245x | } | |
| 54 | |||
| 55 | void | ||
| 56 | 6360224x | stack:: | |
| 57 | clear() noexcept | ||
| 58 | { | ||
| 59 | 6360225x | while(head_) | |
| 60 | 1x | head_ = head_->destroy(); | |
| 61 | 6360224x | size_ = 0; | |
| 62 | 6360224x | } | |
| 63 | |||
| 64 | void | ||
| 65 | 124748x | stack:: | |
| 66 | reserve_impl(std::size_t n) | ||
| 67 | { | ||
| 68 | // caller checks this | ||
| 69 | 124748x | BOOST_ASSERT(n > cap_); | |
| 70 | |||
| 71 | 124748x | auto const base = static_cast<unsigned char*>( sp_->allocate(n) ); | |
| 72 | 124746x | if(base_) | |
| 73 | { | ||
| 74 | // copy trivials | ||
| 75 | 6660x | std::memcpy(base, base_, size_); | |
| 76 | |||
| 77 | // copy non-trivials | ||
| 78 | 6660x | non_trivial<>* src = head_; | |
| 79 | 6660x | non_trivial<>** prev = &head_; | |
| 80 | 6663x | while(src) | |
| 81 | { | ||
| 82 | 3x | std::size_t const buf_offset = | |
| 83 | 3x | reinterpret_cast<unsigned char*>(src) - base_; | |
| 84 | 3x | non_trivial<>* dest = src->relocate(base + buf_offset); | |
| 85 | 3x | *prev = dest; | |
| 86 | 3x | prev = &dest->next; | |
| 87 | 3x | src = dest->next; | |
| 88 | } | ||
| 89 | |||
| 90 | 6660x | if(base_ != buf_) | |
| 91 | 6660x | sp_->deallocate(base_, cap_); | |
| 92 | } | ||
| 93 | 124746x | base_ = base; | |
| 94 | 124746x | cap_ = n; | |
| 95 | 124746x | } | |
| 96 | |||
| 97 | } // detail | ||
| 98 | } // namespace json | ||
| 99 | } // namespace boost | ||
| 100 | |||
| 101 | #endif | ||
| 102 |