summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/tracked_ptr.hpp
blob: cebe9de3979e52663ee05dd30d9248b317ba1c94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2025, Amlal El Mahrouss (amlal@nekernel.org)
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
// Official repository: https://github.com/ocl-foss-org/core

#ifndef OCL_TRACKED_PTR
#define OCL_TRACKED_PTR

#include "boost/assert/source_location.hpp"
#include <ocl/detail/config.hpp>
#include <atomic>
#include <stdexcept>

namespace ocl
{

	template <typename Type>
	class tracked_allocator
	{
	public:
		std::atomic<std::size_t> allocated_count_{0};
		std::atomic<std::size_t> deallocated_count_{0};

	public:
		explicit tracked_allocator() = default;
		virtual ~tracked_allocator() = default;

		tracked_allocator& operator=(const tracked_allocator&) = default;
		tracked_allocator(const tracked_allocator&)			   = default;

	public:
		using pointer_type = Type*;
		using type		   = Type;

		template <typename... Args>
		void retain(pointer_type& ptr, Args&&... args)
		{
			ptr = new type(std::forward<Args>(args)...);

			if (ptr)
			{
				++allocated_count_;
				return;
			}

			throw std::bad_alloc();
		}

		void dispose(pointer_type& ptr)
		{
			if (ptr)
			{
				if (allocated_count_)
					--allocated_count_;

				++deallocated_count_;

				delete ptr;
				ptr = nullptr;
			}
		}
	};

	template <typename Type>
	class tracked_mgr
	{
	private:
		tracked_allocator<Type> allocator_;

	public:
		explicit tracked_mgr() = default;
		virtual ~tracked_mgr() = default;

		tracked_mgr& operator=(const tracked_mgr&) = default;
		tracked_mgr(const tracked_mgr&)			   = default;

	public:
		using pointer_type = Type*;
		using type		   = Type;

		const tracked_allocator<Type>& allocator() noexcept
		{
			return allocator_;
		}

		template <typename... Args>
		pointer_type retain(Args&&... args)
		{
			pointer_type ptr = nullptr;
			allocator_.retain(ptr, std::forward<Args>(args)...);

			return ptr;
		}

		template <typename... Args>
		[[maybe_unused]] pointer_type must_retain(Args&&... args)
		{
			try
			{
				return this->retain(std::forward<Args>(args)...);
			}
			catch (...)
			{
				std::terminate();
			}
		}

		void dispose(pointer_type& ptr) noexcept
		{
			allocator_.dispose(ptr);
		}
	};

	template <typename Type, typename Mgr = tracked_mgr<Type>>
	class tracked_ptr
	{
		using manager = Mgr;

		manager mgr_;

	public:
		template <typename... Args>
		tracked_ptr(Args&&... args)
			: ptr_(nullptr)
		{
			ptr_ = mgr_.retain(std::forward<Args>(args)...);
		}

		virtual ~tracked_ptr() noexcept
		{
			this->reset();
		}

		tracked_ptr(const tracked_ptr&)			   = delete;
		tracked_ptr& operator=(const tracked_ptr&) = delete;

	public:
		using pointer_type		   = Type*;
		using type				   = Type;
		using reference_type	   = Type&;
		using const_reference_type = const Type&;
		using manager_type		   = tracked_mgr<manager>;
		using pointer			   = pointer_type;
		using reference			   = reference_type;

		void reset()
		{
			if (ptr_)
			{
				mgr_.dispose(ptr_);
			}
		}

		pointer_type get() const
		{
			return ptr_;
		}

		type& operator*() const
		{
			return *ptr_;
		}

		pointer_type operator->() const
		{
			return ptr_;
		}

		explicit operator bool() const
		{
			return ptr_ != nullptr;
		}

		void swap(tracked_ptr& other)
		{
			std::swap(ptr_, other.ptr_);
		}

	public:
		tracked_ptr(tracked_ptr&& other) noexcept
			: ptr_(other.ptr_)
		{
			other.ptr_ = nullptr;
		}

		tracked_ptr& operator=(tracked_ptr&& other) noexcept
		{
			if (this != &other)
			{
				ptr_	   = other.ptr_;
				other.ptr_ = nullptr;
			}

			return *this;
		}

	private:
		pointer_type ptr_{nullptr};
	};

	template <typename Type>
	inline auto make_tracked() -> tracked_ptr<Type>
	{
		return tracked_ptr<Type>();
	}

	template <typename RetType, typename... Type>
	inline auto make_tracked(Type&&... arg) -> tracked_ptr<RetType>
	{
		return tracked_ptr<RetType>(std::forward<Type>(arg)...);
	}

	template <typename Type>
	inline void swap(tracked_ptr<Type>& a, tracked_ptr<Type>& b)
	{
		a.swap(b);
	}

	namespace detail
	{
		using tracked_error = std::runtime_error;

		inline void throw_tracked_error(const boost::source_location& loc = BOOST_CURRENT_LOCATION)
		{
			throw tracked_error(loc.to_string());
		}
	} // namespace detail

	/// @brief This function is a standard way to verify a container' validity, inspired from NeKernel/VMKernel.
	template <typename Type>
	inline void try_throw(tracked_ptr<Type>& ptr)
	{
		if (ptr.manager().allocator().allocated_count_ < ptr.manager().allocator().deallocated_count_)
		{
			detail::throw_tracked_error();
		}
	}

} // namespace ocl

#endif // ifndef OCL_TRACKED_PTR