aligned_variable.hpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2020 Andrey Semashev
  7. */
  8. /*!
  9. * \file atomic/detail/aligned_variable.hpp
  10. *
  11. * This header defines a convenience macro for declaring aligned variables
  12. */
  13. #ifndef BOOST_ATOMIC_DETAIL_ALIGNED_VARIABLE_HPP_INCLUDED_
  14. #define BOOST_ATOMIC_DETAIL_ALIGNED_VARIABLE_HPP_INCLUDED_
  15. #include <boost/atomic/detail/config.hpp>
  16. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  17. #include <boost/config/helper_macros.hpp>
  18. #include <boost/type_traits/type_with_alignment.hpp>
  19. #endif
  20. #include <boost/atomic/detail/header.hpp>
  21. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  25. #define BOOST_ATOMIC_DETAIL_ALIGNED_VAR(var_alignment, var_type, var_name) \
  26. alignas(var_alignment) var_type var_name
  27. #define BOOST_ATOMIC_DETAIL_ALIGNED_VAR_TPL(var_alignment, var_type, var_name) \
  28. alignas(var_alignment) var_type var_name
  29. #else // !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  30. // Note: Some compilers cannot use constant expressions in alignment attributes or alignas, so we have to use the union trick
  31. #define BOOST_ATOMIC_DETAIL_ALIGNED_VAR(var_alignment, var_type, var_name) \
  32. union \
  33. { \
  34. var_type var_name; \
  35. boost::type_with_alignment< var_alignment >::type BOOST_JOIN(var_name, _aligner); \
  36. }
  37. #define BOOST_ATOMIC_DETAIL_ALIGNED_VAR_TPL(var_alignment, var_type, var_name) \
  38. union \
  39. { \
  40. var_type var_name; \
  41. typename boost::type_with_alignment< var_alignment >::type BOOST_JOIN(var_name, _aligner); \
  42. }
  43. #endif // !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  44. #include <boost/atomic/detail/footer.hpp>
  45. #endif // BOOST_ATOMIC_DETAIL_ALIGNED_VARIABLE_HPP_INCLUDED_