mfbt/Alignment.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mfbt/Alignment.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +/* Functionality related to memory alignment. */
    1.11 +
    1.12 +#ifndef mozilla_Alignment_h
    1.13 +#define mozilla_Alignment_h
    1.14 +
    1.15 +#include <stddef.h>
    1.16 +#include <stdint.h>
    1.17 +
    1.18 +namespace mozilla {
    1.19 +
    1.20 +/*
    1.21 + * This class, and the corresponding macro MOZ_ALIGNOF, figures out how many
    1.22 + * bytes of alignment a given type needs.
    1.23 + */
    1.24 +template<typename T>
    1.25 +class AlignmentFinder
    1.26 +{
    1.27 +    struct Aligner
    1.28 +    {
    1.29 +        char c;
    1.30 +        T t;
    1.31 +    };
    1.32 +
    1.33 +  public:
    1.34 +    static const size_t alignment = sizeof(Aligner) - sizeof(T);
    1.35 +};
    1.36 +
    1.37 +#define MOZ_ALIGNOF(T) mozilla::AlignmentFinder<T>::alignment
    1.38 +
    1.39 +/*
    1.40 + * Declare the MOZ_ALIGNED_DECL macro for declaring aligned types.
    1.41 + *
    1.42 + * For instance,
    1.43 + *
    1.44 + *   MOZ_ALIGNED_DECL(char arr[2], 8);
    1.45 + *
    1.46 + * will declare a two-character array |arr| aligned to 8 bytes.
    1.47 + */
    1.48 +
    1.49 +#if defined(__GNUC__)
    1.50 +#  define MOZ_ALIGNED_DECL(_type, _align) \
    1.51 +     _type __attribute__((aligned(_align)))
    1.52 +#elif defined(_MSC_VER)
    1.53 +#  define MOZ_ALIGNED_DECL(_type, _align) \
    1.54 +     __declspec(align(_align)) _type
    1.55 +#else
    1.56 +#  warning "We don't know how to align variables on this compiler."
    1.57 +#  define MOZ_ALIGNED_DECL(_type, _align) _type
    1.58 +#endif
    1.59 +
    1.60 +/*
    1.61 + * AlignedElem<N> is a structure whose alignment is guaranteed to be at least N
    1.62 + * bytes.
    1.63 + *
    1.64 + * We support 1, 2, 4, 8, and 16-bit alignment.
    1.65 + */
    1.66 +template<size_t Align>
    1.67 +struct AlignedElem;
    1.68 +
    1.69 +/*
    1.70 + * We have to specialize this template because GCC doesn't like __attribute__((aligned(foo))) where
    1.71 + * foo is a template parameter.
    1.72 + */
    1.73 +
    1.74 +template<>
    1.75 +struct AlignedElem<1>
    1.76 +{
    1.77 +    MOZ_ALIGNED_DECL(uint8_t elem, 1);
    1.78 +};
    1.79 +
    1.80 +template<>
    1.81 +struct AlignedElem<2>
    1.82 +{
    1.83 +    MOZ_ALIGNED_DECL(uint8_t elem, 2);
    1.84 +};
    1.85 +
    1.86 +template<>
    1.87 +struct AlignedElem<4>
    1.88 +{
    1.89 +    MOZ_ALIGNED_DECL(uint8_t elem, 4);
    1.90 +};
    1.91 +
    1.92 +template<>
    1.93 +struct AlignedElem<8>
    1.94 +{
    1.95 +    MOZ_ALIGNED_DECL(uint8_t elem, 8);
    1.96 +};
    1.97 +
    1.98 +template<>
    1.99 +struct AlignedElem<16>
   1.100 +{
   1.101 +    MOZ_ALIGNED_DECL(uint8_t elem, 16);
   1.102 +};
   1.103 +
   1.104 +/*
   1.105 + * This utility pales in comparison to Boost's aligned_storage. The utility
   1.106 + * simply assumes that uint64_t is enough alignment for anyone. This may need
   1.107 + * to be extended one day...
   1.108 + *
   1.109 + * As an important side effect, pulling the storage into this template is
   1.110 + * enough obfuscation to confuse gcc's strict-aliasing analysis into not giving
   1.111 + * false negatives when we cast from the char buffer to whatever type we've
   1.112 + * constructed using the bytes.
   1.113 + */
   1.114 +template<size_t Nbytes>
   1.115 +struct AlignedStorage
   1.116 +{
   1.117 +    union U {
   1.118 +      char bytes[Nbytes];
   1.119 +      uint64_t _;
   1.120 +    } u;
   1.121 +
   1.122 +    const void* addr() const { return u.bytes; }
   1.123 +    void* addr() { return u.bytes; }
   1.124 +};
   1.125 +
   1.126 +template<typename T>
   1.127 +struct AlignedStorage2
   1.128 +{
   1.129 +    union U {
   1.130 +      char bytes[sizeof(T)];
   1.131 +      uint64_t _;
   1.132 +    } u;
   1.133 +
   1.134 +    const T* addr() const { return reinterpret_cast<const T*>(u.bytes); }
   1.135 +    T* addr() { return static_cast<T*>(static_cast<void*>(u.bytes)); }
   1.136 +};
   1.137 +
   1.138 +} /* namespace mozilla */
   1.139 +
   1.140 +#endif /* mozilla_Alignment_h */

mercurial