1 /*
2  *  Copyright 2008-2018 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 /*! \file thrust/system/cpp/memory.h
18  *  \brief Managing memory associated with Thrust's standard C++ system.
19  */
20 
21 #pragma once
22 
23 #include <thrust/detail/config.h>
24 #include <thrust/system/cpp/memory_resource.h>
25 #include <thrust/memory.h>
26 #include <thrust/detail/type_traits.h>
27 #include <thrust/mr/allocator.h>
28 #include <ostream>
29 
30 namespace thrust
31 {
32 namespace system
33 {
34 namespace cpp
35 {
36 /*! Allocates an area of memory available to Thrust's <tt>cpp</tt> system.
37  *  \param n Number of bytes to allocate.
38  *  \return A <tt>cpp::pointer<void></tt> pointing to the beginning of the newly
39  *          allocated memory. A null <tt>cpp::pointer<void></tt> is returned if
40  *          an error occurs.
41  *  \note The <tt>cpp::pointer<void></tt> returned by this function must be
42  *        deallocated with \p cpp::free.
43  *  \see cpp::free
44  *  \see std::malloc
45  */
46 inline pointer<void> malloc(std::size_t n);
47 
48 /*! Allocates a typed area of memory available to Thrust's <tt>cpp</tt> system.
49  *  \param n Number of elements to allocate.
50  *  \return A <tt>cpp::pointer<T></tt> pointing to the beginning of the newly
51  *          allocated elements. A null <tt>cpp::pointer<T></tt> is returned if
52  *          an error occurs.
53  *  \note The <tt>cpp::pointer<T></tt> returned by this function must be
54  *        deallocated with \p cpp::free.
55  *  \see cpp::free
56  *  \see std::malloc
57  */
58 template<typename T>
59 inline pointer<T> malloc(std::size_t n);
60 
61 /*! Deallocates an area of memory previously allocated by <tt>cpp::malloc</tt>.
62  *  \param ptr A <tt>cpp::pointer<void></tt> pointing to the beginning of an area
63  *         of memory previously allocated with <tt>cpp::malloc</tt>.
64  *  \see cpp::malloc
65  *  \see std::free
66  */
67 inline void free(pointer<void> ptr);
68 
69 // XXX upon c++11
70 // template<typename T>
71 // using allocator = thrust::mr::stateless_resource_allocator<T, memory_resource>;
72 
73 /*! \p cpp::allocator is the default allocator used by the \p cpp system's containers such as
74  *  <tt>cpp::vector</tt> if no user-specified allocator is provided. \p cpp::allocator allocates
75  *  (deallocates) storage with \p cpp::malloc (\p cpp::free).
76  */
77 template<typename T>
78   struct allocator
79     : thrust::mr::stateless_resource_allocator<
80         T,
81         memory_resource
82     >
83 {
84 private:
85     typedef thrust::mr::stateless_resource_allocator<
86         T,
87         memory_resource
88     > base;
89 
90 public:
91   /*! The \p rebind metafunction provides the type of an \p allocator
92    *  instantiated with another type.
93    *
94    *  \tparam U The other type to use for instantiation.
95    */
96   template<typename U>
97     struct rebind
98   {
99     /*! The typedef \p other gives the type of the rebound \p allocator.
100      */
101     typedef allocator<U> other;
102   };
103 
104   /*! No-argument constructor has no effect.
105    */
106   __host__ __device__
allocatorallocator107   inline allocator() {}
108 
109   /*! Copy constructor has no effect.
110    */
111   __host__ __device__
allocatorallocator112   inline allocator(const allocator & other) : base(other) {}
113 
114   /*! Constructor from other \p allocator has no effect.
115    */
116   template<typename U>
117   __host__ __device__
allocatorallocator118   inline allocator(const allocator<U> & other) : base(other) {}
119 
120   /*! Destructor has no effect.
121    */
122   __host__ __device__
~allocatorallocator123   inline ~allocator() {}
124 }; // end allocator
125 
126 } // end cpp
127 
128 /*! \}
129  */
130 
131 } // end system
132 
133 /*! \namespace thrust::cpp
134  *  \brief \p thrust::cpp is a top-level alias for thrust::system::cpp.
135  */
136 namespace cpp
137 {
138 
139 using thrust::system::cpp::malloc;
140 using thrust::system::cpp::free;
141 using thrust::system::cpp::allocator;
142 
143 } // end cpp
144 
145 } // end thrust
146 
147 #include <thrust/system/cpp/detail/memory.inl>
148 
149