3#ifndef CUDA_BATTERY_SHARED_PTR_HPP
4#define CUDA_BATTERY_SHARED_PTR_HPP
19template <
class T,
class Allocator = standard_allocator>
31 template<
class Y,
class Alloc>
34 CUDA NI int* allocate_counter() {
35 int* c =
static_cast<int*
>(allocator.allocate(
sizeof(
int)));
40 CUDA void inc_counter() {
41 if(count !=
nullptr) {
48 : allocator(allocator), count(nullptr), ptr(nullptr) {}
51 : allocator(allocator), count(nullptr), ptr(nullptr) {}
56 : allocator(allocator), count(allocate_counter()), ptr(static_cast<T*>(ptr))
61 : allocator(from.allocator), ptr(from.ptr), count(from.count)
69 : allocator(from.allocator), ptr(static_cast<T*>(from.ptr)), count(from.count)
76 : allocator(from.allocator), ptr(from.ptr), count(from.count)
83 : allocator(from.allocator), ptr(static_cast<T*>(from.ptr)), count(from.count)
92 allocator.deallocate(ptr);
94 allocator.deallocate(count);
161 return count ==
nullptr ? 0 : *count;
168 CUDA explicit operator bool()
const {
169 return ptr !=
nullptr;
173template<
class T,
class Alloc,
class... Args>
175 Alloc allocator(alloc);
176 T* ptr =
static_cast<T*
>(allocator.allocate(
sizeof(T)));
177 assert(ptr !=
nullptr);
178 if constexpr(std::is_constructible<T, Args&&...,
const Alloc&>{}) {
179 new(ptr) T(std::forward<Args>(args)..., allocator);
182 new(ptr) T(std::forward<Args>(args)...);
188template<
class T,
class Alloc,
class... Args>
190 return ::battery::allocate_shared<T>(Alloc(), std::forward<Args>(args)...);
Definition shared_ptr.hpp:20
CUDA shared_ptr & operator=(shared_ptr &&r)
Definition shared_ptr.hpp:114
CUDA shared_ptr & operator=(std::nullptr_t)
Definition shared_ptr.hpp:132
shared_ptr< element_type, allocator_type > this_type
Definition shared_ptr.hpp:25
CUDA allocator_type get_allocator() const
Definition shared_ptr.hpp:164
CUDA NI ~shared_ptr()
Definition shared_ptr.hpp:88
T element_type
Definition shared_ptr.hpp:22
CUDA int use_count() const
Definition shared_ptr.hpp:160
CUDA pointer operator->() const
Definition shared_ptr.hpp:155
CUDA void reset()
Definition shared_ptr.hpp:137
CUDA NI shared_ptr(Y *ptr, const allocator_type &allocator=allocator_type())
Definition shared_ptr.hpp:55
CUDA T & operator*() const
Definition shared_ptr.hpp:150
CUDA void reset(Y *ptr)
Definition shared_ptr.hpp:142
CUDA pointer get() const
Definition shared_ptr.hpp:146
CUDA shared_ptr(this_type &&from)
Definition shared_ptr.hpp:60
CUDA shared_ptr(shared_ptr< Y, Allocator > &&from)
Definition shared_ptr.hpp:68
CUDA shared_ptr & operator=(const shared_ptr &r)
Definition shared_ptr.hpp:109
CUDA shared_ptr(const this_type &from)
Definition shared_ptr.hpp:75
T * pointer
Definition shared_ptr.hpp:23
CUDA shared_ptr(std::nullptr_t, const allocator_type &allocator=allocator_type())
Definition shared_ptr.hpp:50
CUDA shared_ptr & operator=(const shared_ptr< Y, Allocator > &r)
Definition shared_ptr.hpp:120
CUDA shared_ptr & operator=(shared_ptr< Y, Allocator > &&r)
Definition shared_ptr.hpp:126
CUDA shared_ptr(const allocator_type &allocator=allocator_type())
Definition shared_ptr.hpp:47
CUDA void swap(shared_ptr &other)
Definition shared_ptr.hpp:103
CUDA shared_ptr(const shared_ptr< Y, Allocator > &from)
Definition shared_ptr.hpp:82
Allocator allocator_type
Definition shared_ptr.hpp:24
Definition algorithm.hpp:10
CUDA constexpr void swap(T &a, T &b)
Definition utility.hpp:91
CUDA NI shared_ptr< T, Alloc > make_shared(Args &&... args)
Definition shared_ptr.hpp:189
CUDA NI shared_ptr< T, Alloc > allocate_shared(const Alloc &alloc, Args &&... args)
Definition shared_ptr.hpp:174
#define CUDA
Definition utility.hpp:59
#define NI
Definition utility.hpp:62