src/ex/recycling_memory_resource.cpp

100.0% Lines (26/26) 100.0% List of functions (8/8)
recycling_memory_resource.cpp
f(x) Functions (8)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2025 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/capy
8 //
9
10 #include <boost/capy/ex/recycling_memory_resource.hpp>
11
12 namespace boost {
13 namespace capy {
14
15 55x recycling_memory_resource::~recycling_memory_resource() = default;
16
17 recycling_memory_resource::pool&
18 298x recycling_memory_resource::global() noexcept
19 {
20 298x static pool p;
21 298x return p;
22 }
23
24 std::mutex&
25 298x recycling_memory_resource::global_mutex() noexcept
26 {
27 static std::mutex mtx;
28 298x return mtx;
29 }
30
31 void*
32 221x recycling_memory_resource::allocate_slow(
33 std::size_t rounded, std::size_t idx)
34 {
35 {
36 221x std::lock_guard<std::mutex> lock(global_mutex());
37 221x if(auto* p = global().buckets[idx].pop(local().buckets[idx]))
38 1x return p;
39 221x }
40 220x return ::operator new(rounded);
41 }
42
43 void
44 77x recycling_memory_resource::deallocate_slow(
45 void* p, std::size_t idx)
46 {
47 {
48 77x std::lock_guard<std::mutex> lock(global_mutex());
49 77x if(global().buckets[idx].push(p))
50 41x return;
51 77x }
52 36x ::operator delete(p);
53 }
54
55 void*
56 2975x recycling_memory_resource::do_allocate(std::size_t bytes, std::size_t alignment)
57 {
58 2975x return allocate_fast(bytes, alignment);
59 }
60
61 void
62 2975x recycling_memory_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
63 {
64 2975x deallocate_fast(p, bytes, alignment);
65 2975x }
66
67 std::pmr::memory_resource*
68 3141x get_recycling_memory_resource() noexcept
69 {
70 3141x static recycling_memory_resource instance;
71 3141x return &instance;
72 }
73
74 } // namespace capy
75 } // namespace boost
76