OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / device_new_allocator.h
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 /*! \file device_new_allocator.h
19  *  \brief An allocator which allocates storage with \p device_new
20  */
21
22 #pragma once
23
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>
29 #include <limits>
30 #include <stdexcept>
31
32 namespace thrust
33 {
34
35 /*! \addtogroup memory_management Memory Management
36  *  \addtogroup memory_management_classes Memory Management Classes
37  *  \ingroup memory_management
38  *  \{
39  */
40
41 /*! \p device_new_allocator is a device memory allocator that employs the
42  *  \p device_new function for allocation.
43  *
44  *  \see device_new
45  *  \see device_ptr
46  *  \see http://www.sgi.com/tech/stl/Allocators.html
47  */
48 template<typename T>
49   class device_new_allocator
50 {
51   public:
52     /*! Type of element allocated, \c T. */
53     typedef T                                 value_type;
54
55     /*! Pointer to allocation, \c device_ptr<T>. */
56     typedef device_ptr<T>                     pointer;
57
58     /*! \c const pointer to allocation, \c device_ptr<const T>. */
59     typedef device_ptr<const T>               const_pointer;
60
61     /*! Reference to allocated element, \c device_reference<T>. */
62     typedef device_reference<T>               reference;
63
64     /*! \c const reference to allocated element, \c device_reference<const T>. */
65     typedef device_reference<const T>         const_reference;
66
67     /*! Type of allocation size, \c std::size_t. */
68     typedef std::size_t                       size_type;
69
70     /*! Type of allocation difference, \c pointer::difference_type. */
71     typedef typename pointer::difference_type difference_type;
72
73     /*! The \p rebind metafunction provides the type of a \p device_new_allocator
74      *  instantiated with another type.
75      *
76      *  \tparam U The other type to use for instantiation.
77      */
78     template<typename U>
79       struct rebind
80     {
81       /*! The typedef \p other gives the type of the rebound \p device_new_allocator.
82        */
83       typedef device_new_allocator<U> other;
84     }; // end rebind
85
86     /*! No-argument constructor has no effect. */
87     __host__ __device__
88     inline device_new_allocator() {}
89
90     /*! No-argument destructor has no effect. */
91     __host__ __device__
92     inline ~device_new_allocator() {}
93
94     /*! Copy constructor has no effect. */
95     __host__ __device__
96     inline device_new_allocator(device_new_allocator const&) {}
97
98     /*! Constructor from other \p device_malloc_allocator has no effect. */
99     template<typename U>
100     __host__ __device__
101     inline device_new_allocator(device_new_allocator<U> const&) {}
102
103     /*! Returns the address of an allocated object.
104      *  \return <tt>&r</tt>.
105      */
106     __host__ __device__
107     inline pointer address(reference r) { return &r; }
108     
109     /*! Returns the address an allocated object.
110      *  \return <tt>&r</tt>.
111      */
112     __host__ __device__
113     inline const_pointer address(const_reference r) { return &r; }
114
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.
119      */
120     __host__
121     inline pointer allocate(size_type cnt,
122                             const_pointer = const_pointer(static_cast<T*>(0)))
123     {
124       if(cnt > this->max_size())
125       {
126         throw std::bad_alloc();
127       } // end if
128
129       // use "::operator new" rather than keyword new
130       return pointer(device_new<T>(cnt));
131     } // end allocate()
132
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.
138      */
139     __host__
140     inline void deallocate(pointer p, size_type cnt)
141     {
142       // use "::operator delete" rather than keyword delete
143       device_delete(p);
144     } // end deallocate()
145
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.
148      */
149     __host__ __device__
150     inline size_type max_size() const
151     {
152       return std::numeric_limits<size_type>::max THRUST_PREVENT_MACRO_SUBSTITUTION () / sizeof(T);
153     } // end max_size()
154
155     /*! Compares against another \p device_malloc_allocator for equality.
156      *  \return \c true
157      */
158     __host__ __device__
159     inline bool operator==(device_new_allocator const&) { return true; }
160
161     /*! Compares against another \p device_malloc_allocator for inequality.
162      *  \return \c false
163      */
164     __host__ __device__
165     inline bool operator!=(device_new_allocator const &a) {return !operator==(a); }
166 }; // end device_new_allocator
167
168 /*! \}
169  */
170
171 } // end thrust
172