libs/capy/include/boost/capy/detail/frame_memory_resource.hpp

30.0% Lines (3/10) 14.3% Functions (2/14) -% Branches (0/0)
libs/capy/include/boost/capy/detail/frame_memory_resource.hpp
Line Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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/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 59 frame_memory_resource(Alloc const& a)
58 59 : alloc_(a)
59 {
60 59 }
61
62 protected:
63 void*
64 do_allocate(std::size_t bytes, std::size_t) override
65 {
66 return byte_traits::allocate(alloc_, bytes);
67 }
68
69 void
70 do_deallocate(void* p, std::size_t bytes, std::size_t) override
71 {
72 byte_traits::deallocate(alloc_, static_cast<std::byte*>(p), bytes);
73 }
74
75 bool
76 do_is_equal(const memory_resource& other) const noexcept override
77 {
78 return this == &other;
79 }
80 };
81
82 } // namespace detail
83 } // namespace capy
84 } // namespace boost
85
86 #endif
87