summaryrefslogtreecommitdiffhomepage
path: root/include/ocl/tracked_ptr.hpp
blob: cda37a817ff88f5a346e3de664bd51449f07149f (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
/*
 * File: ocl/tracked_ptr.hpp
 * Purpose: Strict pointer type implementation in C++
 * Author: Amlal El Mahrouss (amlal@nekernel.org)
 * Copyright 2025, Amlal El Mahrouss, licensed under the Boost Software License.
 */

#ifndef __OCL_TRACKED_PTR
#define __OCL_TRACKED_PTR

#include <exception>
#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... U>
		void retain(pointer_type& ptr, U&&... args)
		{
			ptr = new type(std::forward<U>(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... U>
		pointer_type retain(U&&... args)
		{
			pointer_type ptr = nullptr;
			allocator_.retain(ptr, std::forward<U>(args)...);

			return ptr;
		}

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

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

	template <typename Type, typename Mgr = tracked_mgr<Type>>
	class tracked_ptr
	{
	public:
		static Mgr& manager() noexcept
		{
			static Mgr mgr;
			return mgr;
		}

	public:
		template <typename... U>
		tracked_ptr(U&&... args)
			: ptr_(nullptr)
		{
			ptr_ = tracked_ptr::manager().retain(std::forward<U>(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;

		void reset()
		{
			if (ptr_)
			{
				tracked_ptr::manager().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)
			{
				this->reset();
				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 U, typename... Type>
	inline auto make_tracked(Type&&... arg) -> tracked_ptr<U>
	{
		return tracked_ptr<U>(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()
		{
			throw tracked_error("tracked_error: memory leak detected.");
		}
	} // namespace detail

	/// @brief a Must Pass function is a standard way to verify a container' validity, inspired from NeKernel/VMKernel.
	template <typename Type>
	inline void must_pass(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