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 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
|
/*
* Copyright 2008-2018 NVIDIA Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <thrust/detail/config.h>
#include <thrust/detail/allocator/allocator_traits.h>
#include <thrust/detail/type_traits/is_call_possible.h>
#include <thrust/detail/integer_traits.h>
#if THRUST_CPP_DIALECT >= 2011
#include <thrust/detail/type_deduction.h>
#endif
#include <thrust/detail/memory_wrapper.h>
#include <new>
THRUST_NAMESPACE_BEGIN
namespace detail
{
#if THRUST_CPP_DIALECT >= 2011
// std::allocator's member functions are deprecated in C++17 and removed in
// C++20, so we can't just use the generic implementation for allocator_traits
// that calls the allocator's member functions.
// Instead, specialize allocator_traits for std::allocator and defer to
// std::allocator_traits<std::allocator> and let the STL do whatever it needs
// to for the current c++ version. Manually forward the calls to suppress
// host/device warnings.
template <typename T>
struct allocator_traits<std::allocator<T>>
: public std::allocator_traits<std::allocator<T>>
{
private:
using superclass = std::allocator_traits<std::allocator<T>>;
public:
using allocator_type = typename superclass::allocator_type;
using value_type = typename superclass::value_type;
using pointer = typename superclass::pointer;
using const_pointer = typename superclass::const_pointer;
using void_pointer = typename superclass::void_pointer;
using const_void_pointer = typename superclass::const_void_pointer;
using difference_type = typename superclass::difference_type;
using size_type = typename superclass::size_type;
using propagate_on_container_swap = typename superclass::propagate_on_container_swap;
using propagate_on_container_copy_assignment =
typename superclass::propagate_on_container_copy_assignment;
using propagate_on_container_move_assignment =
typename superclass::propagate_on_container_move_assignment;
// std::allocator_traits added this in C++17, but thrust::allocator_traits defines
// it unconditionally.
using is_always_equal = typename eval_if<
allocator_traits_detail::has_is_always_equal<allocator_type>::value,
allocator_traits_detail::nested_is_always_equal<allocator_type>,
is_empty<allocator_type>
>::type;
// std::allocator_traits doesn't provide these, but
// thrust::detail::allocator_traits does. These used to be part of the
// std::allocator API but were deprecated in C++17.
using reference = typename thrust::detail::pointer_traits<pointer>::reference;
using const_reference = typename thrust::detail::pointer_traits<const_pointer>::reference;
template <typename U>
using rebind_alloc = std::allocator<U>;
template <typename U>
using rebind_traits = allocator_traits<std::allocator<U>>;
__thrust_exec_check_disable__
__host__ __device__
static pointer allocate(allocator_type &a, size_type n)
{
return superclass::allocate(a, n);
}
__thrust_exec_check_disable__
__host__ __device__
static pointer allocate(allocator_type &a, size_type n, const_void_pointer hint)
{
return superclass::allocate(a, n, hint);
}
__thrust_exec_check_disable__
__host__ __device__
static void deallocate(allocator_type &a, pointer p, size_type n)
{
superclass::deallocate(a, p, n);
}
__thrust_exec_check_disable__
template <typename U, typename ...Args>
__host__ __device__
static void construct(allocator_type &a, U *p, Args&&... args)
{
superclass::construct(a, p, THRUST_FWD(args)...);
}
__thrust_exec_check_disable__
template <typename U>
__host__ __device__
static void destroy(allocator_type &a, U *p)
{
superclass::destroy(a, p);
}
__thrust_exec_check_disable__
__host__ __device__
static size_type max_size(const allocator_type &a)
{
return superclass::max_size(a);
}
};
#endif // C++11
namespace allocator_traits_detail
{
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_allocate_with_hint_impl, allocate)
template<typename Alloc>
class has_member_allocate_with_hint
{
typedef typename allocator_traits<Alloc>::pointer pointer;
typedef typename allocator_traits<Alloc>::size_type size_type;
typedef typename allocator_traits<Alloc>::const_void_pointer const_void_pointer;
public:
typedef typename has_member_allocate_with_hint_impl<Alloc, pointer(size_type,const_void_pointer)>::type type;
static const bool value = type::value;
};
template<typename Alloc>
__host__ __device__
typename enable_if<
has_member_allocate_with_hint<Alloc>::value,
typename allocator_traits<Alloc>::pointer
>::type
allocate(Alloc &a, typename allocator_traits<Alloc>::size_type n, typename allocator_traits<Alloc>::const_void_pointer hint)
{
return a.allocate(n,hint);
}
template<typename Alloc>
__host__ __device__
typename disable_if<
has_member_allocate_with_hint<Alloc>::value,
typename allocator_traits<Alloc>::pointer
>::type
allocate(Alloc &a, typename allocator_traits<Alloc>::size_type n, typename allocator_traits<Alloc>::const_void_pointer)
{
return a.allocate(n);
}
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_construct1_impl, construct)
template<typename Alloc, typename T>
struct has_member_construct1
: has_member_construct1_impl<Alloc, void(T*)>
{};
__thrust_exec_check_disable__
template<typename Alloc, typename T>
inline __host__ __device__
typename enable_if<
has_member_construct1<Alloc,T>::value
>::type
construct(Alloc &a, T *p)
{
a.construct(p);
}
__thrust_exec_check_disable__
template<typename Alloc, typename T>
inline __host__ __device__
typename disable_if<
has_member_construct1<Alloc,T>::value
>::type
construct(Alloc &, T *p)
{
::new(static_cast<void*>(p)) T();
}
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_construct2_impl, construct)
template<typename Alloc, typename T, typename Arg1>
struct has_member_construct2
: has_member_construct2_impl<Alloc, void(T*,const Arg1 &)>
{};
__thrust_exec_check_disable__
template<typename Alloc, typename T, typename Arg1>
inline __host__ __device__
typename enable_if<
has_member_construct2<Alloc,T,Arg1>::value
>::type
construct(Alloc &a, T *p, const Arg1 &arg1)
{
a.construct(p,arg1);
}
__thrust_exec_check_disable__
template<typename Alloc, typename T, typename Arg1>
inline __host__ __device__
typename disable_if<
has_member_construct2<Alloc,T,Arg1>::value
>::type
construct(Alloc &, T *p, const Arg1 &arg1)
{
::new(static_cast<void*>(p)) T(arg1);
}
#if THRUST_CPP_DIALECT >= 2011
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_constructN_impl, construct)
template<typename Alloc, typename T, typename... Args>
struct has_member_constructN
: has_member_constructN_impl<Alloc, void(T*, Args...)>
{};
__thrust_exec_check_disable__
template<typename Alloc, typename T, typename... Args>
inline __host__ __device__
typename enable_if<
has_member_constructN<Alloc, T, Args...>::value
>::type
construct(Alloc &a, T* p, Args&&... args)
{
a.construct(p, THRUST_FWD(args)...);
}
__thrust_exec_check_disable__
template<typename Alloc, typename T, typename... Args>
inline __host__ __device__
typename disable_if<
has_member_constructN<Alloc, T, Args...>::value
>::type
construct(Alloc &, T* p, Args&&... args)
{
::new(static_cast<void*>(p)) T(THRUST_FWD(args)...);
}
#endif
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_destroy_impl, destroy)
template<typename Alloc, typename T>
struct has_member_destroy
: has_member_destroy_impl<Alloc, void(T*)>
{};
__thrust_exec_check_disable__
template<typename Alloc, typename T>
inline __host__ __device__
typename enable_if<
has_member_destroy<Alloc,T>::value
>::type
destroy(Alloc &a, T *p)
{
a.destroy(p);
}
__thrust_exec_check_disable__
template<typename Alloc, typename T>
inline __host__ __device__
typename disable_if<
has_member_destroy<Alloc,T>::value
>::type
destroy(Alloc &, T *p)
{
p->~T();
}
__THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_max_size_impl, max_size)
template<typename Alloc>
class has_member_max_size
{
typedef typename allocator_traits<Alloc>::size_type size_type;
public:
typedef typename has_member_max_size_impl<Alloc, size_type(void)>::type type;
static const bool value = type::value;
};
template<typename Alloc>
__host__ __device__
typename enable_if<
has_member_max_size<Alloc>::value,
typename allocator_traits<Alloc>::size_type
>::type
max_size(const Alloc &a)
{
return a.max_size();
}
template<typename Alloc>
__host__ __device__
typename disable_if<
has_member_max_size<Alloc>::value,
typename allocator_traits<Alloc>::size_type
>::type
max_size(const Alloc &)
{
typedef typename allocator_traits<Alloc>::size_type size_type;
return thrust::detail::integer_traits<size_type>::const_max;
}
template<typename Alloc>
__host__ __device__
typename enable_if<
has_member_system<Alloc>::value,
typename allocator_system<Alloc>::type &
>::type
system(Alloc &a)
{
// return the allocator's system
return a.system();
}
template<typename Alloc>
__host__ __device__
typename disable_if<
has_member_system<Alloc>::value,
typename allocator_system<Alloc>::type
>::type
system(Alloc &)
{
// return a copy of a value-initialized system
return typename allocator_system<Alloc>::type();
}
} // end allocator_traits_detail
template<typename Alloc>
__host__ __device__
typename allocator_traits<Alloc>::pointer
allocator_traits<Alloc>
::allocate(Alloc &a, typename allocator_traits<Alloc>::size_type n)
{
struct workaround_warnings
{
__thrust_exec_check_disable__
static __host__ __device__
typename allocator_traits<Alloc>::pointer
allocate(Alloc &a, typename allocator_traits<Alloc>::size_type n)
{
return a.allocate(n);
}
};
return workaround_warnings::allocate(a, n);
}
template<typename Alloc>
__host__ __device__
typename allocator_traits<Alloc>::pointer
allocator_traits<Alloc>
::allocate(Alloc &a, typename allocator_traits<Alloc>::size_type n, typename allocator_traits<Alloc>::const_void_pointer hint)
{
return allocator_traits_detail::allocate(a, n, hint);
}
template<typename Alloc>
__host__ __device__
void allocator_traits<Alloc>
::deallocate(Alloc &a, typename allocator_traits<Alloc>::pointer p, typename allocator_traits<Alloc>::size_type n)
{
struct workaround_warnings
{
__thrust_exec_check_disable__
static __host__ __device__
void deallocate(Alloc &a, typename allocator_traits<Alloc>::pointer p, typename allocator_traits<Alloc>::size_type n)
{
return a.deallocate(p,n);
}
};
return workaround_warnings::deallocate(a,p,n);
}
template<typename Alloc>
template<typename T>
__host__ __device__
void allocator_traits<Alloc>
::construct(allocator_type &a, T *p)
{
return allocator_traits_detail::construct(a,p);
}
template<typename Alloc>
template<typename T, typename Arg1>
__host__ __device__
void allocator_traits<Alloc>
::construct(allocator_type &a, T *p, const Arg1 &arg1)
{
return allocator_traits_detail::construct(a,p,arg1);
}
#if THRUST_CPP_DIALECT >= 2011
template<typename Alloc>
template<typename T, typename... Args>
__host__ __device__
void allocator_traits<Alloc>
::construct(allocator_type &a, T *p, Args&&... args)
{
return allocator_traits_detail::construct(a, p, THRUST_FWD(args)...);
}
#endif
template<typename Alloc>
template<typename T>
__host__ __device__
void allocator_traits<Alloc>
::destroy(allocator_type &a, T *p)
{
return allocator_traits_detail::destroy(a,p);
}
template<typename Alloc>
__host__ __device__
typename allocator_traits<Alloc>::size_type
allocator_traits<Alloc>
::max_size(const allocator_type &a)
{
return allocator_traits_detail::max_size(a);
}
template<typename Alloc>
__host__ __device__
typename allocator_system<Alloc>::get_result_type
allocator_system<Alloc>
::get(Alloc &a)
{
return allocator_traits_detail::system(a);
}
} // end detail
THRUST_NAMESPACE_END
|