C++:严格别名与 union 滥用

标签 c++ pointers unions memory-alignment

提前为第一个可能很愚蠢的帖子道歉。虽然有很多关于这个主题的 Material ,但其中很少有是确定的和/或对我来说可以理解的。

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

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

AFAICT 将任何指针类型别名为 char * 是安全的,但只能在一个方向上。这是我的理解变得模糊的地方。这是我的 AlignedArray 的简化版本 类(本质上是对 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)?

最佳答案

我有实现(替换)new 的代码和 delete运营商,适用于 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>会更正确。

关于C++:严格别名与 union 滥用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15274895/

相关文章:

c++ - Luci 无法启动请求的 CGI 程序 :/www/cgi-bin/sample. cgi: Exec error

pointers - 从指针中检索用户名对象 - Parse, swift

c++ - 从整数中减去指针

c++ - 在 C++ 中显式设置 union 中的事件成员

c++ - C++ 中纹理数据指针的 union

c++ - 将模板化类传递给模板化函数

c++ - C++中增量垃圾收集模拟的内存分配

c - (int*) &var 是什么意思?

c++ - 在 C++ 中静态初始化匿名 union

C++ - 模板类、头文件和重复错误