timemory 3.3.0
Modular C++ Toolkit for Performance Analysis and Logging. Profiling API and Tools for C, C++, CUDA, Fortran, and Python. The C++ template API is essentially a framework to creating tools: it is designed to provide a unifying interface for recording various performance measurements alongside data logging and interfaces to other tools.
shared_stateful_allocator.hpp
Go to the documentation of this file.
1// MIT License
2//
3// Copyright (c) 2020, The Regents of the University of California,
4// through Lawrence Berkeley National Laboratory (subject to receipt of any
5// required approvals from the U.S. Dept. of Energy). All rights reserved.
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in all
15// copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23// SOFTWARE.
24
25#pragma once
26
29#include "timemory/units.hpp"
31
32#include <cstddef>
33#include <functional>
34#include <memory>
35#include <type_traits>
36#include <utility>
37#include <vector>
38
39namespace tim
40{
41namespace data
42{
43/// \class tim::data::shared_stateful_allocator
44/// \tparam AllocT The allocator type
45///
46/// \brief Ensures that allocators which have state are not deleted until
47/// all handles to the allocator have been released.
48template <typename AllocT>
50{
52
53 // call this function to get an allocator
54 static auto request()
55 {
56 auto_lock_t _lk{ type_mutex<this_type>() };
57 return instance().request_impl();
58 }
59
60 // call this function when the allocator should be
61 // evaluated for freeing up it's memory
62 static auto release(std::shared_ptr<AllocT>& _v)
63 {
64 auto_lock_t _lk{ type_mutex<this_type>() };
65 return instance().release_impl(_v);
66 }
67
68private:
69 using alloctor_array_t = std::vector<std::shared_ptr<AllocT>>;
70
71 std::shared_ptr<AllocT> request_impl()
72 {
73 m_data.emplace_back(std::make_shared<AllocT>());
74 return m_data.back();
75 }
76
77 int64_t release_impl(std::shared_ptr<AllocT>& _v)
78 {
79 if(m_data.empty())
80 return 0;
81 auto itr = m_data.begin();
82 for(; itr != m_data.end(); ++itr)
83 {
84 if(itr->get() == _v.get())
85 break;
86 }
87 if(itr == m_data.end())
88 return 0;
89 auto _count = itr->use_count();
90 // only instances are held by m_data and _v
91 if(_count == 2)
92 itr->reset();
93 return _count - 2;
94 }
95
96 alloctor_array_t m_data = {};
97
98 static this_type& instance()
99 {
100 static auto _instance = this_type{};
101 return _instance;
102 }
103};
104
105} // namespace data
106} // namespace tim
Definition: kokkosp.cpp:39
std::unique_lock< mutex_t > auto_lock_t
Unique lock type around mutex_t.
Definition: locking.hpp:42
Ensures that allocators which have state are not deleted until all handles to the allocator have been...
static auto release(std::shared_ptr< AllocT > &_v)
shared_stateful_allocator< AllocT > this_type