C ++:严格别名与联合滥用

时间:2013-03-07 15:21:46

标签: c++ pointers unions memory-alignment

提前道歉,可能是一个愚蠢的第一篇文章。虽然这个主题有很多材料,但对我来说很少是明确的和/或可理解的。

我有一个AlignedArray模板类,可以在堆上动态分配任意对齐的内存(我需要对AVX程序集例程进行32字节对齐)。这需要一些丑陋的指针操作。

Agner Fog在cppexamples.zip中提供了一个示例类,它会滥用联合(http://www.agner.org/optimize/optimization_manuals.zip)。但是,我知道写一个工会的一个成员,然后从另一个成员读取会导致UB。

AFAICT可以安全地将任何指针类型别名为char *,但只能在一个方向上。这是我理解变得模糊的地方。这是我的AlignedArray的简略版本 class(基本上是对Agner的重写,以帮助我理解):

template <typename T, size_t alignment = 32>
class AlignedArray
{
    size_t m_size;
    char * m_unaligned;
    T * m_aligned;

public:
    AlignedArray (size_t const size)
        : m_size(0)
        , m_unaligned(0)
        , m_aligned(0)
    {
        this->size(size);
    }

    ~AlignedArray ()
    {
        this->size(0);
    }

    T const & operator [] (size_t const i) const { return m_aligned[i]; }

    T & operator [] (size_t const i) { return m_aligned[i]; }

    size_t const size () { return m_size; }

    void size (size_t const size)
    {
        if (size > 0)
        {
            if (size != m_size)
            {
                char * unaligned = 0;
                unaligned = new char [size * sizeof(T) + alignment - 1];
                if (unaligned)
                {
                    // Agner:
                    /*
                    union {
                        char * c;
                        T * t;
                        size_t s;
                    } aligned;
                    aligned.c = unaligned + alignment - 1;
                    aligned.s &= ~(alignment - 1);
                    */

                    // Me:
                    T * aligned = reinterpret_cast<T *>((reinterpret_cast<size_t>(unaligned) + alignment - 1) & ~(alignment - 1));

                    if (m_unaligned)
                    {
                        // Agner:
                        //memcpy(aligned.c, m_aligned, std::min(size, m_size));

                        // Me:
                        memcpy(aligned, m_aligned, std::min(size, m_size));

                        delete [] m_unaligned;
                    }
                    m_size = size;
                    m_unaligned = unaligned;

                    // Agner:
                    //m_aligned = aligned.t;

                    // Me:
                    m_aligned = aligned;
                }
                return;
            }
            return;
        }
        if (m_unaligned)
        {
            delete [] m_unaligned;
            m_size = 0;
            m_unaligned = 0;
            m_aligned = 0;
        }
    }
};

那么哪种方法是安全的(r)?

2 个答案:

答案 0 :(得分:3)

我的代码实现了(替换)newdelete运算符,适用于SIMD(即SSE / AVX)。它使用您可能会觉得有用的以下函数:

static inline void *G0__SIMD_malloc (size_t size)
{
    constexpr size_t align = G0_SIMD_ALIGN;
    void *ptr, *uptr;

    static_assert(G0_SIMD_ALIGN >= sizeof(void *),
                  "insufficient alignment for pointer storage");

    static_assert((G0_SIMD_ALIGN & (G0_SIMD_ALIGN - 1)) == 0,
                  "G0_SIMD_ALIGN value must be a power of (2)");

    size += align; // raw pointer storage with alignment padding.

    if ((uptr = malloc(size)) == nullptr)
        return nullptr;

    // size_t addr = reinterpret_cast<size_t>(uptr);
    uintptr_t addr = reinterpret_cast<uintptr_t>(uptr);

    ptr = reinterpret_cast<void *>
        ((addr + align) & ~(align - 1));

    *(reinterpret_cast<void **>(ptr) - 1) = uptr; // (raw ptr)

    return ptr;
}


static inline void G0__SIMD_free (void *ptr)
{
    if (ptr != nullptr)
        free(*(reinterpret_cast<void **>(ptr) - 1)); // (raw ptr)
}

这应该很容易适应。显然,您将替换mallocfree,因为您使用全局newdelete来存储(char)存储。它假定size_t对于地址算法来说足够宽 - 实际上是正确的,但uintptr_t中的<cstdint>会更正确。

答案 1 :(得分:2)

要回答您的问题,这两种方法都同样安全。只有两个非常臭的操作是强制转换为size_tnew char[stuff]。您至少应该使用uintptr_t中的<cstdint>作为第一个char。第二个操作会创建唯一的指针别名问题,因为从技术上讲,char构造函数在每个char元素上运行,并构成通过malloc指针访问数据。您应该使用new代替。

另一个假设的“指针别名”不是问题。这是因为除了T *操作之外,您没有通过别名指针访问任何数据。您只能通过对齐后获得的T来访问数据。

当然,您必须记住构造所有数组元素。即使在您的版本中也是如此。谁知道什么样的memcpy人会放在那里。当然,如果你这样做,你将不得不记得调用他们的析构函数,并且必须记住在复制它们时处理异常(<memory>没有删除它)。

如果您具有特定的C ++ 11功能,则无需执行此操作。 C ++ 11具有专门用于将指针对齐到任意边界的功能。界面有点时髦,但它应该做的工作。 #include <cstdint> // For uintptr_t #include <cstdlib> // For malloc #include <algorithm> template <typename T, size_t alignment = 32> class AlignedArray { size_t m_size; void * m_unaligned; T * m_aligned; public: AlignedArray (size_t const size) : m_size(0) , m_unaligned(0) , m_aligned(0) { this->size(size); } ~AlignedArray () { this->size(0); } T const & operator [] (size_t const i) const { return m_aligned[i]; } T & operator [] (size_t const i) { return m_aligned[i]; } size_t size() const { return m_size; } void size (size_t const size) { using ::std::uintptr_t; using ::std::malloc; if (size > 0) { if (size != m_size) { void * unaligned = 0; unaligned = malloc(size * sizeof(T) + alignment - 1); if (unaligned) { T * aligned = reinterpret_cast<T *>((reinterpret_cast<uintptr_t>(unaligned) + alignment - 1) & ~(alignment - 1)); if (m_unaligned) { ::std::size_t constructed = 0; const ::std::size_t num_to_copy = ::std::min(size, m_size); try { for (constructed = 0; constructed < num_to_copy; ++constructed) { new(aligned + constructed) T(m_aligned[constructed]); } for (; constructed < size; ++constructed) { new(aligned + constructed) T; } } catch (...) { for (::std::size_t i = 0; i < constructed; ++i) { aligned[i].T::~T(); } ::std::free(unaligned); throw; } for (size_t i = 0; i < m_size; ++i) { m_aligned[i].T::~T(); } free(m_unaligned); } m_size = size; m_unaligned = unaligned; m_aligned = aligned; } } } else if (m_unaligned) { // and size <= 0 for (::std::size_t i = 0; i < m_size; ++i) { m_aligned[i].T::~T(); } ::std::free(m_unaligned); m_size = 0; m_unaligned = 0; m_aligned = 0; } } }; 中定义了::std::align来电。感谢R. Martinho Fernandes将其指出。

以下是您的函数版本,其中包含建议的修复:

{{1}}