aligned_alloc_posix.hpp 835 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. Copyright 2014 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_ALIGN_DETAIL_ALIGNED_ALLOC_POSIX_HPP
  8. #define BOOST_ALIGN_DETAIL_ALIGNED_ALLOC_POSIX_HPP
  9. #include <boost/align/detail/is_alignment.hpp>
  10. #include <boost/assert.hpp>
  11. #include <stdlib.h>
  12. namespace boost {
  13. namespace alignment {
  14. inline void*
  15. aligned_alloc(std::size_t alignment, std::size_t size) BOOST_NOEXCEPT
  16. {
  17. BOOST_ASSERT(detail::is_alignment(alignment));
  18. if (alignment < sizeof(void*)) {
  19. alignment = sizeof(void*);
  20. }
  21. void* p;
  22. if (::posix_memalign(&p, alignment, size) != 0) {
  23. p = 0;
  24. }
  25. return p;
  26. }
  27. inline void
  28. aligned_free(void* ptr) BOOST_NOEXCEPT
  29. {
  30. ::free(ptr);
  31. }
  32. } /* alignment */
  33. } /* boost */
  34. #endif