92.31% Lines (24/26) 100.00% Functions (8/8)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) 2   // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #include <boost/capy/ex/recycling_memory_resource.hpp> 10   #include <boost/capy/ex/recycling_memory_resource.hpp>
11   11  
12   namespace boost { 12   namespace boost {
13   namespace capy { 13   namespace capy {
14   14  
HITCBC 15   51 recycling_memory_resource::~recycling_memory_resource() = default; 15   52 recycling_memory_resource::~recycling_memory_resource() = default;
16   16  
17   recycling_memory_resource::pool& 17   recycling_memory_resource::pool&
HITCBC 18   153 recycling_memory_resource::global() noexcept 18   160 recycling_memory_resource::global() noexcept
19   { 19   {
HITCBC 20   153 static pool p; 20   160 static pool p;
HITCBC 21   153 return p; 21   160 return p;
22   } 22   }
23   23  
24   std::mutex& 24   std::mutex&
HITCBC 25   153 recycling_memory_resource::global_mutex() noexcept 25   160 recycling_memory_resource::global_mutex() noexcept
26   { 26   {
27   static std::mutex mtx; 27   static std::mutex mtx;
HITCBC 28   153 return mtx; 28   160 return mtx;
29   } 29   }
30   30  
31   void* 31   void*
HITCBC 32   144 recycling_memory_resource::allocate_slow( 32   151 recycling_memory_resource::allocate_slow(
33   std::size_t rounded, std::size_t idx) 33   std::size_t rounded, std::size_t idx)
34   { 34   {
35   { 35   {
HITCBC 36   144 std::lock_guard<std::mutex> lock(global_mutex()); 36   151 std::lock_guard<std::mutex> lock(global_mutex());
HITCBC 37   144 if(auto* p = global().buckets[idx].pop(local().buckets[idx])) 37   151 if(auto* p = global().buckets[idx].pop(local().buckets[idx]))
MISUBC 38   return p; 38   return p;
HITCBC 39   144 } 39   151 }
HITCBC 40   144 return ::operator new(rounded); 40   151 return ::operator new(rounded);
41   } 41   }
42   42  
43   void 43   void
HITCBC 44   9 recycling_memory_resource::deallocate_slow( 44   9 recycling_memory_resource::deallocate_slow(
45   void* p, std::size_t idx) 45   void* p, std::size_t idx)
46   { 46   {
47   { 47   {
HITCBC 48   9 std::lock_guard<std::mutex> lock(global_mutex()); 48   9 std::lock_guard<std::mutex> lock(global_mutex());
HITCBC 49   9 if(global().buckets[idx].push(p)) 49   9 if(global().buckets[idx].push(p))
HITCBC 50   9 return; 50   9 return;
HITCBC 51   9 } 51   9 }
MISUBC 52   ::operator delete(p); 52   ::operator delete(p);
53   } 53   }
54   54  
55   void* 55   void*
HITCBC 56   2933 recycling_memory_resource::do_allocate(std::size_t bytes, std::size_t alignment) 56   2938 recycling_memory_resource::do_allocate(std::size_t bytes, std::size_t alignment)
57   { 57   {
HITCBC 58   2933 return allocate_fast(bytes, alignment); 58   2938 return allocate_fast(bytes, alignment);
59   } 59   }
60   60  
61   void 61   void
HITCBC 62   2933 recycling_memory_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment) 62   2938 recycling_memory_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
63   { 63   {
HITCBC 64   2933 deallocate_fast(p, bytes, alignment); 64   2938 deallocate_fast(p, bytes, alignment);
HITCBC 65   2933 } 65   2938 }
66   66  
67   std::pmr::memory_resource* 67   std::pmr::memory_resource*
HITCBC 68   3085 get_recycling_memory_resource() noexcept 68   3093 get_recycling_memory_resource() noexcept
69   { 69   {
HITCBC 70   3085 static recycling_memory_resource instance; 70   3093 static recycling_memory_resource instance;
HITCBC 71   3085 return &instance; 71   3093 return &instance;
72   } 72   }
73   73  
74   } // namespace capy 74   } // namespace capy
75   } // namespace boost 75   } // namespace boost