From 391b728a99da75d297437512d0b34f4e502181b9 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 17 Oct 2007 20:56:47 +0000 Subject: [PATCH] Added llvm::AlignOf, a template class whose purpose is to portably compute the minimum memory alignment of arbitrary types. llvm-svn: 43086 --- llvm/include/llvm/Support/AlignOf.h | 43 +++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 llvm/include/llvm/Support/AlignOf.h diff --git a/llvm/include/llvm/Support/AlignOf.h b/llvm/include/llvm/Support/AlignOf.h new file mode 100644 index 000000000000..f27ecdd7130f --- /dev/null +++ b/llvm/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 + +namespace llvm { + +template +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::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 +struct AlignOf { + enum { Alignment = sizeof(AlignmentCalcImpl) - sizeof(T) }; +}; + +} // end namespace llvm +#endif