/* * Copyright 2008-2012 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. */ #include #include #include #include #include #include namespace thrust { namespace detail { namespace allocator_traits_detail { __THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_allocate_with_hint_impl, allocate) template class has_member_allocate_with_hint { typedef typename allocator_traits::pointer pointer; typedef typename allocator_traits::size_type size_type; typedef typename allocator_traits::const_void_pointer const_void_pointer; public: typedef typename has_member_allocate_with_hint_impl::type type; static const bool value = type::value; }; template typename enable_if< has_member_allocate_with_hint::value, typename allocator_traits::pointer >::type allocate(Alloc &a, typename allocator_traits::size_type n, typename allocator_traits::const_void_pointer hint) { return a.allocate(n,hint); } template typename disable_if< has_member_allocate_with_hint::value, typename allocator_traits::pointer >::type allocate(Alloc &a, typename allocator_traits::size_type n, typename allocator_traits::const_void_pointer) { return a.allocate(n); } __THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_construct1_impl, construct) template struct has_member_construct1 : has_member_construct1_impl {}; template inline __host__ __device__ typename enable_if< has_member_construct1::value >::type construct(Alloc &a, T *p) { a.construct(p); } template inline __host__ __device__ typename disable_if< has_member_construct1::value >::type construct(Alloc &a, T *p) { ::new(static_cast(p)) T(); } __THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_construct2_impl, construct) template struct has_member_construct2 : has_member_construct2_impl {}; template inline __host__ __device__ typename enable_if< has_member_construct2::value >::type construct(Alloc &a, T *p, const Arg1 &arg1) { a.construct(p,arg1); } template inline __host__ __device__ typename disable_if< has_member_construct2::value >::type construct(Alloc &, T *p, const Arg1 &arg1) { ::new(static_cast(p)) T(arg1); } __THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_destroy_impl, destroy) template struct has_member_destroy : has_member_destroy_impl {}; template inline __host__ __device__ typename enable_if< has_member_destroy::value >::type destroy(Alloc &a, T *p) { a.destroy(p); } template inline __host__ __device__ typename disable_if< has_member_destroy::value >::type destroy(Alloc &, T *p) { p->~T(); } __THRUST_DEFINE_IS_CALL_POSSIBLE(has_member_max_size_impl, max_size) template class has_member_max_size { typedef typename allocator_traits::size_type size_type; public: typedef typename has_member_max_size_impl::type type; static const bool value = type::value; }; template typename enable_if< has_member_max_size::value, typename allocator_traits::size_type >::type max_size(const Alloc &a) { return a.max_size(); } template typename disable_if< has_member_max_size::value, typename allocator_traits::size_type >::type max_size(const Alloc &a) { typedef typename allocator_traits::size_type size_type; return std::numeric_limits::max(); } __THRUST_DEFINE_HAS_MEMBER_FUNCTION(has_member_system_impl, system) template class has_member_system { typedef typename allocator_system::type system_type; public: typedef typename has_member_system_impl::type type; static const bool value = type::value; }; template typename enable_if< has_member_system::value, typename allocator_system::type & >::type system(Alloc &a) { return a.system(); } template typename disable_if< has_member_system::value, typename allocator_system::type & >::type system(Alloc &a) { // assumes the system is default-constructible static typename allocator_system::type state; return state; } } // end allocator_traits_detail template typename allocator_traits::pointer allocator_traits ::allocate(Alloc &a, typename allocator_traits::size_type n) { return a.allocate(n); } template typename allocator_traits::pointer allocator_traits ::allocate(Alloc &a, typename allocator_traits::size_type n, typename allocator_traits::const_void_pointer hint) { return allocator_traits_detail::allocate(a, n, hint); } template void allocator_traits ::deallocate(Alloc &a, typename allocator_traits::pointer p, typename allocator_traits::size_type n) { return a.deallocate(p,n); } template template void allocator_traits ::construct(allocator_type &a, T *p) { return allocator_traits_detail::construct(a,p); } template template void allocator_traits ::construct(allocator_type &a, T *p, const Arg1 &arg1) { return allocator_traits_detail::construct(a,p,arg1); } template template void allocator_traits ::destroy(allocator_type &a, T *p) { return allocator_traits_detail::destroy(a,p); } template typename allocator_traits::size_type allocator_traits ::max_size(const allocator_type &a) { return allocator_traits_detail::max_size(a); } template typename allocator_system::type & allocator_system ::get(Alloc &a) { return allocator_traits_detail::system(a); } } // end detail } // end thrust