src/ex/recycling_memory_resource.cpp
92.3% Lines (24/26)
100.0% List of functions (8/8)
71.4% Branches (10/14)
Functions (8)
Function
Calls
Lines
Branches
Blocks
boost::capy::recycling_memory_resource::~recycling_memory_resource()
:15
0
100.0%
–
–
boost::capy::recycling_memory_resource::global()
:18
0
100.0%
75.0%
–
boost::capy::recycling_memory_resource::global_mutex()
:25
0
100.0%
–
–
boost::capy::recycling_memory_resource::allocate_slow(unsigned long, unsigned long)
:32
0
83.3%
66.7%
–
boost::capy::recycling_memory_resource::deallocate_slow(void*, unsigned long)
:44
0
83.3%
66.7%
–
boost::capy::recycling_memory_resource::do_allocate(unsigned long, unsigned long)
:56
0
100.0%
–
–
boost::capy::recycling_memory_resource::do_deallocate(void*, unsigned long, unsigned long)
:62
0
100.0%
–
–
boost::capy::get_recycling_memory_resource()
:68
0
100.0%
75.0%
–
| Line | Branch | 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 | 49x | recycling_memory_resource::~recycling_memory_resource() = default; | ||
| 16 | ||||
| 17 | recycling_memory_resource::pool& | |||
| 18 | 111x | recycling_memory_resource::global() noexcept | ||
| 19 | { | |||
| 20 |
3/4✓ Branch 0 taken 33 times.
✓ Branch 1 taken 78 times.
✓ Branch 3 taken 33 times.
✗ Branch 4 not taken.
|
111x | static pool p; | |
| 21 | 111x | return p; | ||
| 22 | } | |||
| 23 | ||||
| 24 | std::mutex& | |||
| 25 | 111x | recycling_memory_resource::global_mutex() noexcept | ||
| 26 | { | |||
| 27 | static std::mutex mtx; | |||
| 28 | 111x | return mtx; | ||
| 29 | } | |||
| 30 | ||||
| 31 | void* | |||
| 32 | 107x | recycling_memory_resource::allocate_slow( | ||
| 33 | std::size_t rounded, std::size_t idx) | |||
| 34 | { | |||
| 35 | { | |||
| 36 |
1/1✓ Branch 2 taken 107 times.
|
107x | std::lock_guard<std::mutex> lock(global_mutex()); | |
| 37 |
1/2✗ Branch 3 not taken.
✓ Branch 4 taken 107 times.
|
107x | if(auto* p = global().buckets[idx].pop(local().buckets[idx])) | |
| 38 | ✗ | return p; | ||
| 39 | 107x | } | ||
| 40 | 107x | return ::operator new(rounded); | ||
| 41 | } | |||
| 42 | ||||
| 43 | void | |||
| 44 | 4x | recycling_memory_resource::deallocate_slow( | ||
| 45 | void* p, std::size_t idx) | |||
| 46 | { | |||
| 47 | { | |||
| 48 |
1/1✓ Branch 2 taken 4 times.
|
4x | std::lock_guard<std::mutex> lock(global_mutex()); | |
| 49 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4x | if(global().buckets[idx].push(p)) | |
| 50 | 4x | return; | ||
| 51 | 4x | } | ||
| 52 | ✗ | ::operator delete(p); | ||
| 53 | } | |||
| 54 | ||||
| 55 | void* | |||
| 56 | 2938x | recycling_memory_resource::do_allocate(std::size_t bytes, std::size_t alignment) | ||
| 57 | { | |||
| 58 | 2938x | return allocate_fast(bytes, alignment); | ||
| 59 | } | |||
| 60 | ||||
| 61 | void | |||
| 62 | 2938x | recycling_memory_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) | ||
| 63 | { | |||
| 64 | 2938x | deallocate_fast(p, bytes, alignment); | ||
| 65 | 2938x | } | ||
| 66 | ||||
| 67 | std::pmr::memory_resource* | |||
| 68 | 3232x | get_recycling_memory_resource() noexcept | ||
| 69 | { | |||
| 70 |
3/4✓ Branch 0 taken 49 times.
✓ Branch 1 taken 3183 times.
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
|
3232x | static recycling_memory_resource instance; | |
| 71 | 3232x | return &instance; | ||
| 72 | } | |||
| 73 | ||||
| 74 | } // namespace capy | |||
| 75 | } // namespace boost | |||
| 76 |