/usr/include/dune/common/alignment.hh is in libdune-common-dev 2.3.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// $Id$
#ifndef DUNE_ALIGNMENT_HH
#define DUNE_ALIGNMENT_HH
#include <cstddef>
#if HAVE_TYPE_TRAITS
#include <type_traits>
#elif HAVE_TR1_TYPE_TRAITS
#include <tr1/type_traits>
#endif
namespace Dune
{
/** @addtogroup Common
*
* @{
*/
/**
* @file
* @brief This file implements a template class to determine alignment
* requirements of types at compile time.
* @author Markus Blatt
*/
namespace
{
/**
* @brief Helper class to measure alignment requirement.
* @tparam T The type we want to measure the alignment requirement for.
*/
template<class T>
struct AlignmentStruct
{
char c;
T t;
void hack();
};
/**
* @brief Helper class to measure alignment requirement.
* @tparam T The type we want to measure the alignment requirement for.
*/
template<class T, std::size_t N>
struct AlignmentHelper
{
enum { N2 = sizeof(AlignmentStruct<T>) - sizeof(T) - N };
char padding1[N];
T t;
char padding2[N2];
};
#define ALIGNMENT_MODULO(a, b) (a % b == 0 ? \
static_cast<std::size_t>(b) : \
static_cast<std::size_t>(a % b))
#define ALIGNMENT_MIN(a, b) (static_cast<std::size_t>(a) < \
static_cast<std::size_t>(b) ? \
static_cast<std::size_t>(a) : \
static_cast<std::size_t>(b))
/** @brief does the actual calculations. */
template <class T, std::size_t N>
struct AlignmentTester
{
typedef AlignmentStruct<T> s;
typedef AlignmentHelper<T, N> h;
typedef AlignmentTester<T, N - 1> next;
enum
{
a1 = ALIGNMENT_MODULO(N , sizeof(T)),
a2 = ALIGNMENT_MODULO(h::N2 , sizeof(T)),
a3 = ALIGNMENT_MODULO(sizeof(h), sizeof(T)),
a = sizeof(h) == sizeof(s) ? ALIGNMENT_MIN(a1, a2) : a3,
result = ALIGNMENT_MIN(a, next::result)
};
};
/** @brief does the actual calculations. */
template <class T>
struct AlignmentTester<T, 0>
{
enum
{
result = ALIGNMENT_MODULO(sizeof(AlignmentStruct<T>), sizeof(T))
};
};
} //end anonymous namespace
/**
* @brief Calculates the alignment requirement of a type.
*
* @see http://en.wikipedia.org/wiki/Data_structure_alignment
*
* This will be a safe value and not an optimal one.
* If TR1 is available it falls back to std::alignment_of.
*/
template <class T>
struct AlignmentOf
{
enum
{
/** @brief The alignment requirement. */
#ifdef HAVE_TYPE_TRAITS
value = std::alignment_of<T>::value
#elif HAVE_TR1_TYPETRAITS
value = std::tr1::alignment_of<T>::value
#else
value = AlignmentTester<T, sizeof(AlignmentStruct<T>) - sizeof(T) -1>::result
#endif
};
};
/** @} */
}
#endif
|