malloc.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
  3. //
  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
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_EXPERIMENTAL_MALLOC_HPP
  11. #define BOOST_COMPUTE_EXPERIMENTAL_MALLOC_HPP
  12. #include <boost/compute/buffer.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/context.hpp>
  15. #include <boost/compute/detail/device_ptr.hpp>
  16. namespace boost {
  17. namespace compute {
  18. namespace experimental {
  19. // bring device_ptr into the experimental namespace
  20. using detail::device_ptr;
  21. template<class T>
  22. inline device_ptr<T>
  23. malloc(std::size_t size, const context &context = system::default_context())
  24. {
  25. buffer buf(context, size * sizeof(T));
  26. clRetainMemObject(buf.get());
  27. return device_ptr<T>(buf);
  28. }
  29. inline device_ptr<char>
  30. malloc(std::size_t size, const context &context = system::default_context())
  31. {
  32. return malloc<char>(size, context);
  33. }
  34. template<class T>
  35. inline void free(device_ptr<T> &ptr)
  36. {
  37. clReleaseMemObject(ptr.get_buffer().get());
  38. }
  39. } // end experimental namespace
  40. } // end compute namespace
  41. } // end boost namespace
  42. #endif // BOOST_COMPUTE_EXPERIMENTAL_MALLOC_HPP