1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@gmail.com>
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
12 #define BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP
14 #include <boost/compute/buffer.hpp>
15 #include <boost/compute/config.hpp>
16 #include <boost/compute/context.hpp>
17 #include <boost/compute/detail/device_ptr.hpp>
22 /// \class buffer_allocator
23 /// \brief The buffer_allocator class allocates memory with \ref buffer objects
27 class buffer_allocator
31 typedef detail::device_ptr<T> pointer;
32 typedef const detail::device_ptr<T> const_pointer;
33 typedef std::size_t size_type;
34 typedef std::ptrdiff_t difference_type;
36 explicit buffer_allocator(const context &context)
38 m_mem_flags(buffer::read_write)
42 buffer_allocator(const buffer_allocator<T> &other)
43 : m_context(other.m_context),
44 m_mem_flags(other.m_mem_flags)
48 buffer_allocator<T>& operator=(const buffer_allocator<T> &other)
51 m_context = other.m_context;
52 m_mem_flags = other.m_mem_flags;
58 #ifndef BOOST_COMPUTE_NO_RVALUE_REFERENCES
59 buffer_allocator(buffer_allocator<T>&& other) BOOST_NOEXCEPT
60 : m_context(std::move(other.m_context)),
61 m_mem_flags(other.m_mem_flags)
65 buffer_allocator<T>& operator=(buffer_allocator<T>&& other) BOOST_NOEXCEPT
67 m_context = std::move(other.m_context);
68 m_mem_flags = other.m_mem_flags;
72 #endif // BOOST_COMPUTE_NO_RVALUE_REFERENCES
78 pointer allocate(size_type n)
80 buffer buf(m_context, n * sizeof(T), m_mem_flags);
81 clRetainMemObject(buf.get());
82 return detail::device_ptr<T>(buf);
85 void deallocate(pointer p, size_type n)
87 BOOST_ASSERT(p.get_buffer().get_context() == m_context);
91 clReleaseMemObject(p.get_buffer().get());
94 size_type max_size() const
96 return m_context.get_device().max_memory_alloc_size() / sizeof(T);
99 context get_context() const
105 void set_mem_flags(cl_mem_flags flags)
112 cl_mem_flags m_mem_flags;
115 } // end compute namespace
116 } // end boost namespace
118 #endif // BOOST_COMPUTE_ALLOCATOR_BUFFER_ALLOCATOR_HPP