include/boost/capy/detail/frame_memory_resource.hpp
81.8% Lines (9/11)
42.1% Functions (8/19)
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2025 Vinnie Falco ([email protected]) | ||
| 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/capy | ||
| 8 | // | ||
| 9 | |||
| 10 | #ifndef BOOST_CAPY_DETAIL_FRAME_MEMORY_RESOURCE_HPP | ||
| 11 | #define BOOST_CAPY_DETAIL_FRAME_MEMORY_RESOURCE_HPP | ||
| 12 | |||
| 13 | #include <boost/capy/detail/config.hpp> | ||
| 14 | |||
| 15 | #include <cstddef> | ||
| 16 | #include <memory> | ||
| 17 | #include <memory_resource> | ||
| 18 | #include <type_traits> | ||
| 19 | |||
| 20 | namespace boost { | ||
| 21 | namespace capy { | ||
| 22 | namespace detail { | ||
| 23 | |||
| 24 | /** Wrapper that adapts a standard Allocator to memory_resource. | ||
| 25 | |||
| 26 | This wrapper is used to store value-type allocators in the | ||
| 27 | execution context or trampoline frame. It rebinds the allocator | ||
| 28 | to std::byte for raw memory allocation. | ||
| 29 | |||
| 30 | @tparam Alloc The standard allocator type. | ||
| 31 | */ | ||
| 32 | template<class Alloc> | ||
| 33 | class frame_memory_resource : public std::pmr::memory_resource | ||
| 34 | { | ||
| 35 | using traits = std::allocator_traits<Alloc>; | ||
| 36 | using byte_alloc = typename traits::template rebind_alloc<std::byte>; | ||
| 37 | using byte_traits = std::allocator_traits<byte_alloc>; | ||
| 38 | |||
| 39 | static_assert( | ||
| 40 | requires { typename traits::value_type; }, | ||
| 41 | "Alloc must satisfy allocator requirements"); | ||
| 42 | |||
| 43 | static_assert( | ||
| 44 | std::is_copy_constructible_v<Alloc>, | ||
| 45 | "Alloc must be copy constructible"); | ||
| 46 | |||
| 47 | byte_alloc alloc_; | ||
| 48 | |||
| 49 | public: | ||
| 50 | /** Construct from an allocator. | ||
| 51 | |||
| 52 | The allocator is rebind-copied to std::byte. | ||
| 53 | |||
| 54 | @param a The allocator to adapt. | ||
| 55 | */ | ||
| 56 | explicit | ||
| 57 | 70 | frame_memory_resource(Alloc const& a) | |
| 58 | 70 | : alloc_(a) | |
| 59 | { | ||
| 60 | 70 | } | |
| 61 | |||
| 62 | /** Get the memory resource pointer. | ||
| 63 | |||
| 64 | @return Pointer to this memory resource. | ||
| 65 | */ | ||
| 66 | 4 | std::pmr::memory_resource* get() noexcept { return this; } | |
| 67 | |||
| 68 | protected: | ||
| 69 | void* | ||
| 70 | 10 | do_allocate(std::size_t bytes, std::size_t) override | |
| 71 | { | ||
| 72 | 16 | return byte_traits::allocate(alloc_, bytes); | |
| 73 | } | ||
| 74 | |||
| 75 | void | ||
| 76 | 10 | do_deallocate(void* p, std::size_t bytes, std::size_t) override | |
| 77 | { | ||
| 78 | 10 | byte_traits::deallocate(alloc_, static_cast<std::byte*>(p), bytes); | |
| 79 | 10 | } | |
| 80 | |||
| 81 | bool | ||
| 82 | ✗ | do_is_equal(const memory_resource& other) const noexcept override | |
| 83 | { | ||
| 84 | ✗ | return this == &other; | |
| 85 | } | ||
| 86 | }; | ||
| 87 | |||
| 88 | } // namespace detail | ||
| 89 | } // namespace capy | ||
| 90 | } // namespace boost | ||
| 91 | |||
| 92 | #endif | ||
| 93 |