File indexing completed on 2025-01-18 09:29:58
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
0012 #define BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
0013
0014 #include <boost/compute/buffer.hpp>
0015 #include <boost/compute/config.hpp>
0016 #include <boost/compute/context.hpp>
0017 #include <boost/compute/detail/device_ptr.hpp>
0018
0019 namespace boost {
0020 namespace compute {
0021
0022
0023
0024
0025
0026 template<class T>
0027 class buffer_allocator
0028 {
0029 public:
0030 typedef T value_type;
0031 typedef detail::device_ptr<T> pointer;
0032 typedef const detail::device_ptr<T> const_pointer;
0033 typedef std::size_t size_type;
0034 typedef std::ptrdiff_t difference_type;
0035
0036 explicit buffer_allocator(const context &context)
0037 : m_context(context),
0038 m_mem_flags(buffer::read_write)
0039 {
0040 }
0041
0042 buffer_allocator(const buffer_allocator<T> &other)
0043 : m_context(other.m_context),
0044 m_mem_flags(other.m_mem_flags)
0045 {
0046 }
0047
0048 buffer_allocator<T>& operator=(const buffer_allocator<T> &other)
0049 {
0050 if(this != &other){
0051 m_context = other.m_context;
0052 m_mem_flags = other.m_mem_flags;
0053 }
0054
0055 return *this;
0056 }
0057
0058 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
0059 buffer_allocator(buffer_allocator<T>&& other) BOOST_NOEXCEPT
0060 : m_context(std::move(other.m_context)),
0061 m_mem_flags(other.m_mem_flags)
0062 {
0063 }
0064
0065 buffer_allocator<T>& operator=(buffer_allocator<T>&& other) BOOST_NOEXCEPT
0066 {
0067 m_context = std::move(other.m_context);
0068 m_mem_flags = other.m_mem_flags;
0069
0070 return *this;
0071 }
0072 #endif
0073
0074 ~buffer_allocator()
0075 {
0076 }
0077
0078 pointer allocate(size_type n)
0079 {
0080 buffer buf(m_context, n * sizeof(T), m_mem_flags);
0081 clRetainMemObject(buf.get());
0082 return detail::device_ptr<T>(buf);
0083 }
0084
0085 void deallocate(pointer p, size_type n)
0086 {
0087 BOOST_ASSERT(p.get_buffer().get_context() == m_context);
0088
0089 (void) n;
0090
0091 clReleaseMemObject(p.get_buffer().get());
0092 }
0093
0094 size_type max_size() const
0095 {
0096 return m_context.get_device().max_memory_alloc_size() / sizeof(T);
0097 }
0098
0099 context get_context() const
0100 {
0101 return m_context;
0102 }
0103
0104 protected:
0105 void set_mem_flags(cl_mem_flags flags)
0106 {
0107 m_mem_flags = flags;
0108 }
0109
0110 private:
0111 context m_context;
0112 cl_mem_flags m_mem_flags;
0113 };
0114
0115 }
0116 }
0117
0118 #endif