2 * Copyright 2008-2013 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 /*! \file device_new_allocator.h
19 * \brief An allocator which allocates storage with \p device_new
24 #include <thrust/detail/config.h>
25 #include <thrust/device_ptr.h>
26 #include <thrust/device_reference.h>
27 #include <thrust/device_new.h>
28 #include <thrust/device_delete.h>
35 /*! \addtogroup memory_management Memory Management
36 * \addtogroup memory_management_classes Memory Management Classes
37 * \ingroup memory_management
41 /*! \p device_new_allocator is a device memory allocator that employs the
42 * \p device_new function for allocation.
46 * \see http://www.sgi.com/tech/stl/Allocators.html
49 class device_new_allocator
52 /*! Type of element allocated, \c T. */
55 /*! Pointer to allocation, \c device_ptr<T>. */
56 typedef device_ptr<T> pointer;
58 /*! \c const pointer to allocation, \c device_ptr<const T>. */
59 typedef device_ptr<const T> const_pointer;
61 /*! Reference to allocated element, \c device_reference<T>. */
62 typedef device_reference<T> reference;
64 /*! \c const reference to allocated element, \c device_reference<const T>. */
65 typedef device_reference<const T> const_reference;
67 /*! Type of allocation size, \c std::size_t. */
68 typedef std::size_t size_type;
70 /*! Type of allocation difference, \c pointer::difference_type. */
71 typedef typename pointer::difference_type difference_type;
73 /*! The \p rebind metafunction provides the type of a \p device_new_allocator
74 * instantiated with another type.
76 * \tparam U The other type to use for instantiation.
81 /*! The typedef \p other gives the type of the rebound \p device_new_allocator.
83 typedef device_new_allocator<U> other;
86 /*! No-argument constructor has no effect. */
88 inline device_new_allocator() {}
90 /*! No-argument destructor has no effect. */
92 inline ~device_new_allocator() {}
94 /*! Copy constructor has no effect. */
96 inline device_new_allocator(device_new_allocator const&) {}
98 /*! Constructor from other \p device_malloc_allocator has no effect. */
101 inline device_new_allocator(device_new_allocator<U> const&) {}
103 /*! Returns the address of an allocated object.
104 * \return <tt>&r</tt>.
107 inline pointer address(reference r) { return &r; }
109 /*! Returns the address an allocated object.
110 * \return <tt>&r</tt>.
113 inline const_pointer address(const_reference r) { return &r; }
115 /*! Allocates storage for \p cnt objects.
116 * \param cnt The number of objects to allocate.
117 * \return A \p pointer to uninitialized storage for \p cnt objects.
118 * \note Memory allocated by this function must be deallocated with \p deallocate.
121 inline pointer allocate(size_type cnt,
122 const_pointer = const_pointer(static_cast<T*>(0)))
124 if(cnt > this->max_size())
126 throw std::bad_alloc();
129 // use "::operator new" rather than keyword new
130 return pointer(device_new<T>(cnt));
133 /*! Deallocates storage for objects allocated with \p allocate.
134 * \param p A \p pointer to the storage to deallocate.
135 * \param cnt The size of the previous allocation.
136 * \note Memory deallocated by this function must previously have been
137 * allocated with \p allocate.
140 inline void deallocate(pointer p, size_type cnt)
142 // use "::operator delete" rather than keyword delete
144 } // end deallocate()
146 /*! Returns the largest value \c n for which <tt>allocate(n)</tt> might succeed.
147 * \return The largest value \c n for which <tt>allocate(n)</tt> might succeed.
150 inline size_type max_size() const
152 return std::numeric_limits<size_type>::max THRUST_PREVENT_MACRO_SUBSTITUTION () / sizeof(T);
155 /*! Compares against another \p device_malloc_allocator for equality.
159 inline bool operator==(device_new_allocator const&) { return true; }
161 /*! Compares against another \p device_malloc_allocator for inequality.
165 inline bool operator!=(device_new_allocator const &a) {return !operator==(a); }
166 }; // end device_new_allocator