3#ifndef CUDA_BATTERY_UNIQUE_PTR_HPP
4#define CUDA_BATTERY_UNIQUE_PTR_HPP
19template <
class T,
class Allocator = standard_allocator>
30 template<
class U,
class Alloc>
34 : allocator(allocator), ptr(nullptr) {}
36 : allocator(allocator), ptr(nullptr) {}
40 : allocator(allocator), ptr(ptr) {}
48 : ptr(static_cast<T*>(from.ptr)), allocator(from.allocator)
58 allocator.deallocate(ptr);
102 CUDA explicit operator bool()
const {
103 return ptr !=
nullptr;
117template<
class T,
class Alloc,
class... Args>
119 Alloc allocator(alloc);
120 T* ptr =
static_cast<T*
>(allocator.allocate(
sizeof(T)));
121 assert(ptr !=
nullptr);
122 if constexpr(std::is_constructible<T, Args&&...,
const Alloc&>{}) {
123 new(ptr) T(std::forward<Args>(args)..., allocator);
126 new(ptr) T(std::forward<Args>(args)...);
132template<
class T,
class Alloc,
class... Args>
141#include <cooperative_groups.h>
162template<
class T,
class Alloc,
class... Args>
163__device__
NI T& make_unique_block(unique_ptr<T, Alloc>& ptr, Args&&... args) {
164 __shared__ T* raw_ptr;
165 auto block = cooperative_groups::this_thread_block();
166 invoke_one(block, [&](){
171 T* data_ptr = raw_ptr;
179 __device__
void* raw_ptr;
185template<
class T,
class Alloc,
class... Args>
186__device__
NI T& make_unique_grid(unique_ptr<T, Alloc>& ptr, Args&&... args) {
187 auto grid = cooperative_groups::this_grid();
188 invoke_one(grid, [&](){
190 impl::raw_ptr =
static_cast<void*
>(ptr.get());
193 T* data_ptr =
static_cast<T*
>(impl::raw_ptr);
Definition unique_ptr.hpp:20
CUDA unique_ptr & operator=(unique_ptr< U, Allocator > &&r)
Definition unique_ptr.hpp:74
Allocator allocator_type
Definition unique_ptr.hpp:24
CUDA unique_ptr & operator=(unique_ptr &&r)
Definition unique_ptr.hpp:68
T * pointer
Definition unique_ptr.hpp:23
CUDA pointer operator->() const
Definition unique_ptr.hpp:111
CUDA unique_ptr(unique_ptr< U, Allocator > &&from)
Definition unique_ptr.hpp:47
CUDA unique_ptr(this_type &&from)
Definition unique_ptr.hpp:42
CUDA unique_ptr(const this_type &)=delete
CUDA unique_ptr & operator=(std::nullptr_t)
Definition unique_ptr.hpp:79
CUDA pointer get() const
Definition unique_ptr.hpp:94
CUDA void reset(pointer ptr=pointer())
Definition unique_ptr.hpp:90
CUDA NI ~unique_ptr()
Definition unique_ptr.hpp:55
CUDA pointer release()
Definition unique_ptr.hpp:84
CUDA allocator_type get_allocator() const
Definition unique_ptr.hpp:98
T element_type
Definition unique_ptr.hpp:22
CUDA unique_ptr(const allocator_type &allocator=allocator_type())
Definition unique_ptr.hpp:33
CUDA T & operator*() const
Definition unique_ptr.hpp:106
CUDA void swap(unique_ptr &other)
Definition unique_ptr.hpp:63
CUDA unique_ptr(std::nullptr_t, const allocator_type &allocator=allocator_type())
Definition unique_ptr.hpp:35
unique_ptr< element_type, allocator_type > this_type
Definition unique_ptr.hpp:25
CUDA unique_ptr(pointer ptr, const allocator_type &allocator=allocator_type())
Definition unique_ptr.hpp:39
Definition algorithm.hpp:10
CUDA unique_ptr< T, Alloc > make_unique(Args &&... args)
Definition unique_ptr.hpp:133
CUDA NI unique_ptr< T, Alloc > allocate_unique(const Alloc &alloc, Args &&... args)
Definition unique_ptr.hpp:118
CUDA constexpr void swap(T &a, T &b)
Definition utility.hpp:91
#define CUDA
Definition utility.hpp:59
#define NI
Definition utility.hpp:62