2
* Copyright 2008-2011 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_malloc_allocator.h
19
* \brief Defines the interface to a
20
* standard C++ allocator class for
21
* allocating device memory with device_malloc.
26
#include <thrust/detail/config.h>
27
#include <thrust/device_ptr.h>
28
#include <thrust/device_reference.h>
29
#include <thrust/device_malloc.h>
30
#include <thrust/device_free.h>
37
// forward declarations to WAR circular #includes
38
template<typename> class device_ptr;
39
template<typename T> device_ptr<T> device_malloc(const std::size_t n);
41
/*! \addtogroup memory_management Memory Management
42
* \addtogroup memory_management_classes Memory Management Classes
43
* \ingroup memory_management
47
/*! \p device_malloc_allocator is a device memory allocator that employs the
48
* \p device_malloc function for allocation.
52
* \see http://www.sgi.com/tech/stl/Allocators.html
55
class device_malloc_allocator
59
typedef device_ptr<T> pointer;
60
typedef device_ptr<const T> const_pointer;
61
typedef device_reference<T> reference;
62
typedef device_reference<const T> const_reference;
63
typedef std::size_t size_type;
64
typedef typename pointer::difference_type difference_type;
66
// convert a device_malloc_allocator<T> to device_malloc_allocator<U>
70
typedef device_malloc_allocator<U> other;
74
inline device_malloc_allocator() {}
77
inline ~device_malloc_allocator() {}
80
inline device_malloc_allocator(device_malloc_allocator const&) {}
84
inline device_malloc_allocator(device_malloc_allocator<U> const&) {}
88
inline pointer address(reference r) { return &r; }
91
inline const_pointer address(const_reference r) { return &r; }
95
inline pointer allocate(size_type cnt,
96
const_pointer = const_pointer(static_cast<T*>(0)))
98
if(cnt > this->max_size())
100
throw std::bad_alloc();
103
return pointer(device_malloc<T>(cnt));
107
inline void deallocate(pointer p, size_type cnt)
110
} // end deallocate()
112
inline size_type max_size() const
114
return (std::numeric_limits<size_type>::max)() / sizeof(T);
118
inline bool operator==(device_malloc_allocator const&) { return true; }
121
inline bool operator!=(device_malloc_allocator const &a) {return !operator==(a); }
122
}; // end device_malloc_allocator