如何在c++中编写对齐感知的(alignment-aware)内存分配器? (SIMD优化)

new和malloc不够用,因其仅保证16字节对齐,而AVX2/AVX-512要求32/64字节对齐,否则向量指令会崩溃或数据错乱;需用std::aligned_alloc或自定义对齐分配器保障地址对齐。

为什么 newmalloc 不够用?

当你做 SIMD 计算(比如用 _mm256_load_ps_mm512_store_si512)时,CPU 会要求数据地址严格对齐:AVX2 要求 32 字节对齐,AVX-512 要求 64 字节对齐。而标准 malloc 只保证 alignof(std::max_align_t)(通常是 16 字节),new 同理。直接传给向量指令就会触发 std::bad_alloc(某些平台)或更糟——静默崩溃、数据错乱。

std::aligned_alloc 写一个基础对齐分配器

这是最轻量、可移植的起点(C++17 起可用)。它绕过 new/delete 的限制,直接请求指定对齐的原始内存。

void* aligned_malloc(size_t size, size_t alignment) {
    // alignment 必须是 2 的幂,且 >= sizeof(void*)
    if (alignment == 0 || (alignment & (alignment - 1)) != 0) return nullptr;
    void* ptr = std::aligned_alloc(alignment, size);
    if (!ptr) throw std::bad_alloc{};
    return ptr;
}

void aligned_free(void* ptr) { std::free(ptr); // 注意:必须用 free,不是 delete }

  • std::aligned_alloc 要求 alignment 是 2 的幂,且 sizealignment 的整数倍(否则行为未定义)
  • 返回的指针可直接用于 _mm256_load_ps(ptr) 等指令
  • 不能用 delete 释放 —— 必须配对用 std::free

封装成类模板:支持 std::vectorstd::allocator 接口

要让 std::vector> 正常工作,需实现标准 allocator 概念。关键不是重写所有函数,而是确保 allocate 返回足够对齐的内存。

template
struct aligned_allocator {
    using value_type = T;
    using pointer = T*;
pointer allocate(size_t n) {
    size_t bytes = n * sizeof(T);
    void* ptr = std::aligned_alloc(Alignment, bytes);
    if (!ptr) throw std::bad_alloc{};
    return static_cast(ptr);
}

void deallocate(pointer p, size_t) noexcept {
    std::free(p);
}

template
struct rebind { using other = aligned_allocator; };

};

  • 必须提供 rebind,否则容器内部类型推导会失败
  • deallocate 的第二个参数(n)在 std::free 中无用,但签名必须匹配
  • std::vector,建议用 std::vector> 配合 AVX-512

手动对齐 + 偏移管理(避免 std::aligned_alloc 的开销)

高频小块分配(如每帧分配几百个 256-bit 向量)时,std::aligned_alloc 的系统调用开销明显。更高效的做法是:一次申请大块内存(如 2MB),然后手动按对齐边界切分,并记录偏移。

class simd_arena {
    std::unique_ptr storage_;
    size_t offset_ = 0;
    static constexpr size_t kPageSize = 4096;

public: explicit simd_arena(sizet capacity) : storage(std::make_unique(capacity + kPageSize)) { // 找到第一个满足对齐要求的地址 uintptr_t addr = reinterpret_cast(storage.get()); offset = (kPageSize - (addr & (kPageSize - 1))) & (kPageSize - 1); }

template
void* allocate(size_t bytes) {
    static_assert((Align & (Align - 1)) == 0, "Align must be power of two");
    uintptr_t cur = reinterpret_cast(storage_.get()) + offset_;
    uintptr_t aligned = (cur + Align - 1) & ~(Align - 1);
    size_t needed = aligned - cur + bytes;
    if (offset_ + needed > storage_.size()) return nullptr;
    offset_ += needed;
    return reinterpret_cast(aligned);
}

};

  • 这里用 uintptr_t 做指针算术,避免未定义行为
  • 每次 allocate 返回的地址都满足 Align 对齐,且不依赖系统 allocator
  • 注意:这种 arena 不支持单个对象释放,只适合“一帧一清”或“全生命周期统一管理”的场景

对齐不是加个 alignas 就完事的——那是告诉编译器怎么放栈变量;真正决定运行时能否安全执行向量指令的,是分配器返回的地址是否落在硬件要求的边界上。最容易被忽略的一点:即使你用 aligned_alloc(64, ...),如果后续做了指针算术(比如 ptr + 1),结果很可能就失去对齐了。所以对齐感知的分配器,本质是把对齐责任从使用者手上收回来,由分配器统一保障。