align.hpp 906 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /*
  2. Copyright 2014-2020 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_ALIGN_HPP
  8. #define BOOST_ALIGN_DETAIL_ALIGN_HPP
  9. #include <boost/align/detail/is_alignment.hpp>
  10. #include <boost/assert.hpp>
  11. namespace boost {
  12. namespace alignment {
  13. inline void*
  14. align(std::size_t alignment, std::size_t size, void*& ptr,
  15. std::size_t& space)
  16. {
  17. BOOST_ASSERT(boost::alignment::detail::is_alignment(alignment));
  18. if (size <= space) {
  19. char* p = reinterpret_cast<char*>(~(alignment - 1) &
  20. (reinterpret_cast<std::size_t>(ptr) + alignment - 1));
  21. std::size_t n = p - static_cast<char*>(ptr);
  22. if (n <= space - size) {
  23. ptr = p;
  24. space -= n;
  25. return p;
  26. }
  27. }
  28. return 0;
  29. }
  30. } /* alignment */
  31. } /* boost */
  32. #endif