From fce2652a8471fe663239eaf94ddade1dac5f6768 Mon Sep 17 00:00:00 2001 From: "csilvers+ctemplate@google.com" Date: Fri, 7 Oct 2011 21:28:23 +0000 Subject: [PATCH] Moving aligned_char_array into manual_constructor. Revision created by MOE tool push_codebase. MOE_MIGRATION=3426 --- trunk/src/base/manual_constructor.h | 81 +++++++++++++++++++++++++++-- 1 file changed, 78 insertions(+), 3 deletions(-) diff --git a/trunk/src/base/manual_constructor.h b/trunk/src/base/manual_constructor.h index d63b0d8..bd789e6 100644 --- a/trunk/src/base/manual_constructor.h +++ b/trunk/src/base/manual_constructor.h @@ -45,10 +45,82 @@ #define UTIL_GTL_MANUAL_CONSTRUCTOR_H_ #include -#include "base/aligned_char_array.h" - _START_GOOGLE_NAMESPACE_ +namespace util { +namespace gtl { +namespace internal { + +// +// Provides a char array with the exact same alignment as another type. The +// first parameter must be a complete type, the second parameter is how many +// of that type to provide space for. +// +// UTIL_GTL_ALIGNED_CHAR_ARRAY(struct stat, 16) storage_; +// +// Because MSVC and older GCCs require that the argument to their alignment +// construct to be a literal constant integer, we use a template instantiated +// at all the possible powers of two. +#ifndef SWIG +template struct AlignType { }; +template struct AlignType<0, size> { typedef char result[size]; }; +#if defined(COMPILER_MSVC) +#define UTIL_GTL_ALIGN_ATTRIBUTE(X) __declspec(align(X)) +#define UTIL_GTL_ALIGN_OF(T) __alignof(T) +#elif defined(COMPILER_GCC3) || defined(COMPILER_ICC) +#define UTIL_GTL_ALIGN_ATTRIBUTE(X) __attribute__((aligned(X))) +#define UTIL_GTL_ALIGN_OF(T) __alignof__(T) +#endif + +#if defined(UTIL_GTL_ALIGN_ATTRIBUTE) + +#define UTIL_GTL_ALIGNTYPE_TEMPLATE(X) \ + template struct AlignType { \ + typedef UTIL_GTL_ALIGN_ATTRIBUTE(X) char result[size]; \ + } + +UTIL_GTL_ALIGNTYPE_TEMPLATE(1); +UTIL_GTL_ALIGNTYPE_TEMPLATE(2); +UTIL_GTL_ALIGNTYPE_TEMPLATE(4); +UTIL_GTL_ALIGNTYPE_TEMPLATE(8); +UTIL_GTL_ALIGNTYPE_TEMPLATE(16); +UTIL_GTL_ALIGNTYPE_TEMPLATE(32); +UTIL_GTL_ALIGNTYPE_TEMPLATE(64); +UTIL_GTL_ALIGNTYPE_TEMPLATE(128); +UTIL_GTL_ALIGNTYPE_TEMPLATE(256); +UTIL_GTL_ALIGNTYPE_TEMPLATE(512); +UTIL_GTL_ALIGNTYPE_TEMPLATE(1024); +UTIL_GTL_ALIGNTYPE_TEMPLATE(2048); +UTIL_GTL_ALIGNTYPE_TEMPLATE(4096); +UTIL_GTL_ALIGNTYPE_TEMPLATE(8192); +// Any larger and MSVC++ will complain. + +#define UTIL_GTL_ALIGNED_CHAR_ARRAY(T, Size) \ + typename util::gtl::internal::AlignType::result + +#undef UTIL_GTL_ALIGNTYPE_TEMPLATE +#undef UTIL_GTL_ALIGN_ATTRIBUTE + +#else // defined(UTIL_GTL_ALIGN_ATTRIBUTE) +#error "You must define UTIL_GTL_ALIGNED_CHAR_ARRAY for your compiler." +#endif // defined(UTIL_GTL_ALIGN_ATTRIBUTE) + +#else // !SWIG + +// SWIG can't represent alignment and doesn't care about alignment on data +// members (it works fine without it). +template +struct AlignType { typedef char result[Size]; }; +#define UTIL_GTL_ALIGNED_CHAR_ARRAY(T, Size) \ + util::gtl::internal::AlignType::result + +#endif // !SWIG + +} // namespace internal +} // namespace gtl +} // namespace util + template class ManualConstructor { public: @@ -152,9 +224,12 @@ class ManualConstructor { } private: - ALIGNED_CHAR_ARRAY(Type, 1) space_; + UTIL_GTL_ALIGNED_CHAR_ARRAY(Type, 1) space_; }; +#undef UTIL_GTL_ALIGNED_CHAR_ARRAY +#undef UTIL_GTL_ALIGN_OF + _END_GOOGLE_NAMESPACE_ #endif // UTIL_GTL_MANUAL_CONSTRUCTOR_H_