Files
XCEngine/docs/api/memory/pool-allocator/allocate.md
ssdfasd 98c764bab9 docs: Fix memory module documentation discrepancies
- Add missing PoolAllocator class overview with methods table
- Add missing LinearAllocator class overview with methods table
- Add missing ProxyAllocator class overview with methods table
- Fix PoolAllocator::Allocate example code and comments
- Clarify ProxyAllocator::Free totalFreed calculation behavior
- Fix CreateLinearAllocator complexity from O(size) to O(1)
- Add note about Reallocate thread safety in ProxyAllocator
2026-03-19 00:48:44 +08:00

1.1 KiB
Raw Blame History

PoolAllocator::Allocate

void* Allocate(size_t size, size_t alignment = 0) override;

从内存池中分配一个空闲块。每次分配一个固定大小的块。如果 size 超过块大小或池中没有空闲块,返回 nullptr。分配操作从空闲链表头部取出一个块。

参数:

  • size - 请求的大小,如果超过构造时指定的块大小则分配失败
  • alignment - 被忽略(构造时确定)

返回: 分配成功返回块指针,池空返回 nullptr

复杂度: O(1)

示例:

#include <XCEngine/Memory/PoolAllocator.h>

// 创建能容纳 1000 个整数的内存池
PoolAllocator pool(sizeof(int), 1000, alignof(int));

// 分配(忽略 size 参数,只分配固定大小的块)
void* block1 = pool.Allocate(1);     // 分配一块(实际大小为 sizeof(int)
void* block2 = pool.Allocate(10000); // 也分配一块(实际大小为 sizeof(int)

if (block1 && block2) {
    // 使用分配的内存块
    int* arr = static_cast<int*>(block1);
    arr[0] = 42;
}

相关文档