自定义内存分配器用于控制c++容器内存分配行为,通过实现标准接口如allocate、deallocate、construct等,可优化性能,常用于内存池、共享内存等场景,提升高频分配效率。

在C++中,自定义内存分配器(allocator)主要用于控制容器(如std::vector、std::list等)的内存分配行为。标准库中的容器都支持通过模板参数传入自定义的allocator,从而实现更高效的内存管理,比如用于内存池、共享内存、栈上分配或性能优化场景。
理解标准分配器接口
C++标准要求一个合法的分配器必须满足一定的接口规范。最基本的成员包括:
- value_type:被分配对象的类型
- pointer:指向
value_type的指针 - const_pointer:常量指针
- reference:引用类型
- const_reference:常量引用
- size_type:无符号整数类型,表示大小
- difference_type:有符号整数类型,表示指针差值
- allocate(n):分配未初始化的内存,可容纳n个
value_type - deallocate(p, n):释放由allocate分配的内存
- construct(p, args…):在已分配内存p上构造对象
- destroy(p):析构p指向的对象
- rebind<U>:允许分配器适配不同类型
从C++17起,construct和destroy不再是强制要求,而是推荐使用std::allocator_traits来统一调用。
实现一个简单的自定义分配器
下面是一个基于堆的简单分配器示例,功能类似std::allocator,但便于扩展:
立即学习“C++免费学习笔记(深入)”;
template <typename T> struct MyAllocator { using value_type = T; using pointer = T*; using const_pointer = const T*; using reference = T&; using const_reference = const T&; using size_type = std::size_t; using difference_type = std::ptrdiff_t; <pre class='brush:php;toolbar:false;'>template <typename U> struct rebind { using other = MyAllocator<U>; }; MyAllocator() = default; template <typename U> MyAllocator(const MyAllocator<U>&) {} pointer allocate(size_type n) { if (n == 0) return nullptr; pointer p = static_cast<pointer>(::operator new(n * sizeof(T))); // 可添加日志或统计 return p; } void deallocate(pointer p, size_type n) { ::operator delete(p); // 可记录释放信息 } template <typename U, typename... Args> void construct(U* p, Args&&... args) { new(p) U(std::forward<Args>(args)...); } template <typename U> void destroy(U* p) { p->~U(); }
};
这个分配器可用于STL容器:
std::vector<int, MyAllocator<int>> vec; vec.push_back(10); vec.push_back(20);
高级用途:内存池分配器
实际应用中,自定义分配器常用于减少频繁调用new/delete带来的开销。例如,实现一个简单的内存池:
template <typename T, size_t BlockSize = 4096> class PoolAllocator { private: struct Block { std::aligned_storage_t<BlockSize> data; }; <pre class='brush:php;toolbar:false;'>union node { T data; Node* next; }; Node* free_list = nullptr; std::vector<Block> blocks; size_t current_offset = 0;
public: using value_type = T; using pointer = T; using const_pointer = const T; using size_type = std::size_t;
template <typename U> struct rebind { using other = PoolAllocator<U, BlockSize>; }; PoolAllocator() = default; ~PoolAllocator() = default; pointer allocate(size_type n) { if (n != 1) { throw std::bad_alloc{}; } if (!free_list) { refill_pool(); } Node* p = free_list; free_list = free_list->next; return reinterpret_cast<pointer>(p); } void deallocate(pointer p, size_type n) { if (n != 1 || !p) return; Node* node = reinterpret_cast<Node*>(p); node->next = free_list; free_list = node; } template <typename U, typename... Args> void construct(U* p, Args&&... args) { new(p) U(std::forward<Args>(args)...); } template <typename U> void destroy(U* p) { p->~U(); }
private: void refill_pool() { Block& block = blocks.emplace_back(); char start = reinterpret_cast<char>(&block.data); const size_t nodes_per_block = BlockSize / sizeof(Node); Node nodes = reinterpret_cast<Node>(start);
for (size_t i = 0; i < nodes_per_block - 1; ++i) { nodes[i].next = &nodes[i + 1]; } nodes[nodes_per_block - 1].next = nullptr; free_list = &nodes[0]; }
};
该分配器预先分配大块内存,并将对象以链表形式管理空闲节点,显著提升小对象频繁分配/释放的性能。
注意事项与最佳实践
编写自定义分配器时需注意以下几点:
- 确保
allocate返回的是未初始化的原始内存,不要调用构造函数 -
deallocate不应调用析构,只负责释放内存 - 分配器应为无状态(stateless)以便拷贝赋值安全;若需状态(如内存池),要保证线程安全
- 不同实例间的分配器应能相互释放内存(即“可互换”),否则可能导致未定义行为
- 避免在
allocate/deallocate中抛出异常,除非确实无法分配
使用std::allocator_traits可以增强分配器的通用性,它提供了默认的construct和destroy实现,兼容旧式和新式分配器。
基本上就这些。自定义分配器是C++高性能编程的重要工具,合理使用可显著改善程序性能,尤其在高频分配场景下。