|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef TCG_ALIGNMENT_H
|
|
Toshihiro Shimizu |
890ddd |
#define TCG_ALIGNMENT_H
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "macros.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
/*! \file alignment.h
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
This file contains C++ utilities about types alignment.
|
|
Toshihiro Shimizu |
890ddd |
*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Private stuff
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// From http://stackoverflow.com/questions/6959261/how-can-i-simulate-alignast
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
union _MaxAlign {
|
|
Toshihiro Shimizu |
890ddd |
int i;
|
|
Toshihiro Shimizu |
890ddd |
long l;
|
|
Toshihiro Shimizu |
890ddd |
long long ll;
|
|
Toshihiro Shimizu |
890ddd |
long double ld;
|
|
Toshihiro Shimizu |
890ddd |
double d;
|
|
Toshihiro Shimizu |
890ddd |
void *p;
|
|
Toshihiro Shimizu |
890ddd |
void (*pf)();
|
|
Toshihiro Shimizu |
890ddd |
_MaxAlign *ps;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename bool="" t,=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct _AlignTypeDetail;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct _AlignTypeDetail<t, false=""> {</t,>
|
|
Toshihiro Shimizu |
890ddd |
typedef T type;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct _AlignTypeDetail<t, true=""> {</t,>
|
|
Toshihiro Shimizu |
890ddd |
typedef char type;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <size_t alignment,="" typename="" u=""></size_t>
|
|
Toshihiro Shimizu |
890ddd |
struct _AlignType {
|
|
Toshihiro Shimizu |
890ddd |
typedef typename _AlignTypeDetail<u, (alignment="" <="" sizeof(u))="">::type type;</u,>
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct _Aligner {
|
|
Toshihiro Shimizu |
890ddd |
char c;
|
|
Toshihiro Shimizu |
890ddd |
T t;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// TCG alignment unions
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <int alignment=""></int>
|
|
Toshihiro Shimizu |
890ddd |
union aligner_type {
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, char="">::type c;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, short="">::type s;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, int="">::type i;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, long="">::type l;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, long="">::type ll;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, float="">::type f;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, double="">::type d;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, double="" long="">::type ld;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, *="" void="">::type pc;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, *="" _maxalign="">::type ps;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
typename _AlignType<alignment, (*)()="" void="">::type pf;</alignment,>
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
union aligned_buffer {
|
|
Toshihiro Shimizu |
890ddd |
typedef aligner_type<sizeof(_aligner<t>) - sizeof(T)> aligner_type;</sizeof(_aligner<t>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
aligner_type m_aligner;
|
|
Toshihiro Shimizu |
890ddd |
char m_buf[sizeof(T)];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Toshihiro Shimizu |
890ddd |
TCG_STATIC_ASSERT(sizeof(_Aligner<t>) - sizeof(T) ==</t>
|
|
Toshihiro Shimizu |
890ddd |
sizeof(_Aligner<aligner_type>) - sizeof(aligner_type));</aligner_type>
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// TCG alignment traits
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct alignment_traits {
|
|
Toshihiro Shimizu |
890ddd |
static const int alignment = sizeof(_Aligner<t>) - sizeof(T);</t>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
typedef aligner_type<alignment> aligner_type;</alignment>
|
|
Toshihiro Shimizu |
890ddd |
typedef aligned_buffer<t> buffer_type;</t>
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif // TCG_ALIGNMENT_H
|