diff options
author | Ted Kremenek <kremenek@apple.com> | 2007-10-17 20:56:47 +0000 |
---|---|---|
committer | Ted Kremenek <kremenek@apple.com> | 2007-10-17 20:56:47 +0000 |
commit | a4d9869cf291e6f87bf9e7adfcd9a2c4e4518172 (patch) | |
tree | 7dcafba7c12e1a9e85593a7fba34c3f2e0a67e27 | |
parent | 84458323a254c4ee31aae553f1a8b66742cd0562 (diff) |
Added llvm::AlignOf, a template class whose purpose is to portably
compute the minimum memory alignment of arbitrary types.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@43086 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | include/llvm/Support/AlignOf.h | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/include/llvm/Support/AlignOf.h b/include/llvm/Support/AlignOf.h new file mode 100644 index 0000000000..f27ecdd713 --- /dev/null +++ b/include/llvm/Support/AlignOf.h @@ -0,0 +1,43 @@ +//===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Ted Kremenek and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the AlignOf function that computes alignments for +// arbitrary types. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_SUPPORT_ALIGNOF_H +#define LLVM_SUPPORT_ALIGNOF_H + +#include <cassert> + +namespace llvm { + +template <typename T> +struct AlignmentCalcImpl { + char x; + T t; +private: + AlignmentCalcImpl() {} // Never instantiate. +}; + +/// AlignOf - A templated class that contains an enum value representing +/// the alignment of the template argument. For example, +/// AlignOf<int>::Alignment represents the alignment of type "int". The +/// alignment calculated is the minimum alignment, and not necessarily +/// the "desired" alignment returned by GCC's __alignof__ (for example). Note +/// that because the alignment is an enum value, it can be used as a +/// compile-time constant (e.g., for template instantiation). +template <typename T> +struct AlignOf { + enum { Alignment = sizeof(AlignmentCalcImpl<T>) - sizeof(T) }; +}; + +} // end namespace llvm +#endif |