38 #ifndef ECUDA_PTR_SHARED_PTR_HPP
39 #define ECUDA_PTR_SHARED_PTR_HPP
43 #include "../global.hpp"
45 #include "../algorithm.hpp"
62 struct sp_counter_base
64 std::size_t owner_count;
65 sp_counter_base() : owner_count(1) {}
66 virtual ~sp_counter_base() {}
67 virtual void dispose(
void* ) = 0;
68 virtual void* get_deleter() {
return 0; }
72 struct sp_counter_impl_p : sp_counter_base
74 sp_counter_impl_p() : sp_counter_base() {}
78 template<
typename T,
class Deleter>
79 struct sp_counter_impl_pd : sp_counter_base
82 sp_counter_impl_pd( Deleter deleter ) : sp_counter_base(), deleter(deleter) {}
83 virtual void dispose(
void* p ) {
if( p ) deleter(p); }
85 virtual void* get_deleter() {
return reinterpret_cast<void*
>(&deleter); }
130 detail::sp_counter_base* counter;
152 #ifndef __CUDA_ARCH__
153 if( ptr ) counter =
new detail::sp_counter_impl_p<T>();
166 template<
typename U,
class Deleter>
169 #ifndef __CUDA_ARCH__
170 if( ptr ) counter =
new detail::sp_counter_impl_pd<T,Deleter>( deleter );
192 #ifndef __CUDA_ARCH__
193 ++(counter->owner_count);
207 #ifndef __CUDA_ARCH__
208 if( counter ) ++(counter->owner_count);
225 #ifndef __CUDA_ARCH__
226 if( counter ) ++(counter->owner_count);
230 #ifdef ECUDA_CPP11_AVAILABLE
270 template<
typename U,
class Deleter>
273 counter = current_ptr ?
new detail::sp_counter_impl_pd<T,Deleter>( src.get_deleter() ) : NULL;
287 #ifndef __CUDA_ARCH__
289 --(counter->owner_count);
290 if( counter->owner_count == 0 ) {
291 counter->dispose( current_ptr );
324 #ifdef ECUDA_CPP11_AVAILABLE
416 __DEVICE__ inline typename ecuda::add_lvalue_reference<T>::type
operator*() const __NOEXCEPT__ {
return *
reinterpret_cast<T*
>(current_ptr); }
444 #ifdef ECUDA_CPP11_AVAILABLE
445 __HOST__ __DEVICE__ explicit operator bool() const __NOEXCEPT__ {
return get() != NULL; }
477 template<
typename T2>
__HOST__ __DEVICE__ bool operator< ( const shared_ptr<T2>& other )
const __NOEXCEPT__ {
return get() < other.get(); }
479 template<
typename T2>
__HOST__ __DEVICE__ bool operator<=( const shared_ptr<T2>& other )
const __NOEXCEPT__ {
return get() <= other.get(); }
491 template<
typename U,
typename V>
492 friend std::basic_ostream<U,V>& operator<<( std::basic_ostream<U,V>& out,
const shared_ptr& ptr )
A smart pointer that retains shared ownership of an object in device memory.
__HOST__ __DEVICE__ bool unique() const __NOEXCEPT__
Checks if this is the only shared_ptr instance managing the current object.
__HOST__ __DEVICE__ T * operator->() const __NOEXCEPT__
Dereferences pointer to the managed object.
The default destruction policy used by smart pointers to device memory.
__HOST__ __DEVICE__ shared_ptr(const shared_ptr< U > &src, T *ptr) __NOEXCEPT__
The aliasing constructor.
__HOST__ __DEVICE__ ~shared_ptr()
Destructor.
__HOST__ __DEVICE__ void reset(U *ptr, Deleter d)
Replaces the managed object with another.
A smart pointer that retains sole ownership of an object.
__DEVICE__ ecuda::add_lvalue_reference< T >::type operator*() const __NOEXCEPT__
Dereferences pointer to the managed object.
__HOST__ __DEVICE__ bool owner_before(const shared_ptr< U > &other) const
__HOST__ __DEVICE__ shared_ptr & operator=(const shared_ptr &src) __NOEXCEPT__
Replaces the managed object.
__HOST__ __DEVICE__ bool operator>=(const shared_ptr< T2 > &other) const __NOEXCEPT__
__HOST__ __DEVICE__ bool operator==(const shared_ptr< T2 > &other) const __NOEXCEPT__
__HOST__ __DEVICE__ void swap(T &a, T &b) __NOEXCEPT__
__HOST__ __DEVICE__ bool operator!=(const shared_ptr< T2 > &other) const __NOEXCEPT__
__HOST__ __DEVICE__ void reset() __NOEXCEPT__
Releases ownership of the managed object.
__HOST__ __DEVICE__ shared_ptr(U *ptr)
Constructs a shared_ptr with a pointer to the managed object.
__HOST__ __DEVICE__ shared_ptr & operator=(const shared_ptr< U > &src) __NOEXCEPT__
Replaces the managed object.
T element_type
type of the managed object
__HOST__ __DEVICE__ std::size_t use_count() const __NOEXCEPT__
Returns the number of smart pointers managing the current object.
__HOST__ __DEVICE__ void swap(shared_ptr &other) __NOEXCEPT__
Exchanges the contents of *this and other.
__HOST__ __DEVICE__ bool operator>(const shared_ptr< T2 > &other) const __NOEXCEPT__
__HOST__ __DEVICE__ shared_ptr(const shared_ptr &src) __NOEXCEPT__
Copy constructor.
__HOST__ __DEVICE__ shared_ptr(U *ptr, Deleter deleter)
Constructs a shared_ptr with a pointer to the managed object.
__HOST__ __DEVICE__ shared_ptr(const shared_ptr< U > &src) __NOEXCEPT__
Copy constructor.
__HOST__ __DEVICE__ __CONSTEXPR__ shared_ptr() __NOEXCEPT__
Default constructor constructs a shared_ptr with no managed object.
__HOST__ __DEVICE__ void reset(U *ptr)
Replaces the managed object with another.