10#include <ocl/detail/config.hpp>
19 template <
typename Type>
23 std::atomic<std::size_t> allocated_count_{0};
24 std::atomic<std::size_t> deallocated_count_{0};
34 using pointer_type = Type*;
37 template <
typename... Args>
38 void retain(pointer_type& ptr, Args&&... args)
40 ptr =
new type(std::forward<Args>(args)...);
48 throw std::bad_alloc();
51 void dispose(pointer_type& ptr)
66 template <
typename Type>
70 tracked_allocator<Type> allocator_;
80 using pointer_type = Type*;
83 const tracked_allocator<Type>&
allocator()
noexcept
88 template <
typename... Args>
89 pointer_type retain(Args&&... args)
91 pointer_type ptr =
nullptr;
92 allocator_.retain(ptr, std::forward<Args>(args)...);
97 template <
typename... Args>
98 [[maybe_unused]] pointer_type must_retain(Args&&... args)
102 return this->retain(std::forward<Args>(args)...);
110 void dispose(pointer_type& ptr)
noexcept
112 allocator_.dispose(ptr);
116 template <
typename Type,
typename Mgr = tracked_mgr<Type>>
124 template <
typename... Args>
128 ptr_ = mgr_.retain(std::forward<Args>(args)...);
140 using pointer_type = Type*;
142 using reference_type = Type&;
143 using const_reference_type =
const Type&;
144 using manager_type = tracked_mgr<manager>;
145 using pointer = pointer_type;
146 using reference = reference_type;
156 pointer_type get()
const
161 type& operator*()
const
166 pointer_type operator->()
const
171 explicit operator bool()
const
173 return ptr_ !=
nullptr;
178 std::swap(ptr_, other.ptr_);
185 other.ptr_ =
nullptr;
193 other.ptr_ =
nullptr;
200 pointer_type ptr_{
nullptr};
203 template <
typename Type>
204 inline auto make_tracked() -> tracked_ptr<Type>
206 return tracked_ptr<Type>();
209 template <
typename RetType,
typename... Type>
210 inline auto make_tracked(Type&&... arg) -> tracked_ptr<RetType>
212 return tracked_ptr<RetType>(std::forward<Type>(arg)...);
215 template <
typename Type>
216 inline void swap(tracked_ptr<Type>& a, tracked_ptr<Type>& b)
223 using tracked_error = std::runtime_error;
225 inline void throw_tracked_error(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
227 ::boost::throw_exception(tracked_error(
""), loc);
232 template <
typename Type>
235 if (ptr.manager().allocator().allocated_count_ < ptr.manager().allocator().deallocated_count_)
237 detail::throw_tracked_error();
Allocator operations structure. Takes care of memory mgmt within a pool.
Definition allocator_fwd.hpp:103
Definition tracked_ptr.hpp:21
Definition tracked_ptr.hpp:68
Definition tracked_ptr.hpp:118
Support for the smart pointer file for the OCL.
Definition smart_ptr.hpp:18
void try_throw(tracked_ptr< Type > &ptr)
This function is a standard way to verify a container' validity, inspired from NeKernel/VMKernel.
Definition tracked_ptr.hpp:233