aligned_alloc_macos.hpp 880 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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_MACOS_HPP
  8. #define BOOST_ALIGN_DETAIL_ALIGNED_ALLOC_MACOS_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 (size == 0) {
  19. return 0;
  20. }
  21. if (alignment < sizeof(void*)) {
  22. alignment = sizeof(void*);
  23. }
  24. void* p;
  25. if (::posix_memalign(&p, alignment, size) != 0) {
  26. p = 0;
  27. }
  28. return p;
  29. }
  30. inline void
  31. aligned_free(void* ptr) BOOST_NOEXCEPT
  32. {
  33. ::free(ptr);
  34. }
  35. } /* alignment */
  36. } /* boost */
  37. #endif