我有一个自定义池分配器,我希望它与std :: vector一起使用
#include <iostream>
#include <memory>
#include <vector>
#include <type_traits>
template<typename T, uint Size>
struct ObjectPool
{
using value_type = T;
using pointer = value_type *;
ObjectPool()
{
for (auto i = 1; i < Size; ++i)
buffer[i - 1].next = &buffer[i];
nextFreeItem = &buffer[0];
}
ObjectPool(const ObjectPool&) = delete;
ObjectPool(ObjectPool&& other) noexcept
: buffer{ std::move(other.buffer) }
, nextFreeItem{ other.nextFreeItem }
{
other.nextFreeItem = nullptr;
}
~ObjectPool() = default;
template<typename U>
struct rebind
{
typedef ObjectPool<U, Size> other;
};
template<typename U, uint other_capacity>
ObjectPool(const ObjectPool<U, other_capacity>& other) {}
[[nodiscard]] pointer allocate(uint size = 0)
{
std::cout << "ObjectPool: allocate " << size << "\n";
if (nextFreeItem == nullptr)
throw std::bad_alloc{};
const auto item = nextFreeItem;
nextFreeItem = item->next;
return reinterpret_cast<pointer>(&item->storage);
}
void deallocate(pointer p, uint = 0) noexcept
{
std::cout << "ObjectPool: deallocate\n";
const auto item = reinterpret_cast<Item*>(p);
item->next = nextFreeItem;
nextFreeItem = item;
}
template<typename U, typename ...Args>
void construct(U* mem, Args&& ...args)
{
std::cout << "ObjectPool: construct\n";
new (mem) value_type(std::forward<Args>(args)...);
}
template<typename U>
void destroy(U* mem) noexcept
{
std::cout << "ObjectPool: destroy\n";
if (mem == nullptr)
return;
mem->~value_type();
}
ObjectPool& operator =(const ObjectPool&) = delete;
ObjectPool& operator =(ObjectPool&& other) noexcept
{
if (this == &other)
return *this;
buffer = std::move(other.buffer);
nextFreeItem = other.nextFreeItem;
other.nextFreeItem = nullptr;
return *this;
}
private:
union Item
{
std::aligned_storage_t<sizeof(value_type), alignof(value_type)> storage;
Item* next;
};
std::unique_ptr<Item[]> buffer = std::make_unique<Item[]>(Size);
Item* nextFreeItem = nullptr;
};
int main()
{
std::vector<int, ObjectPool<int, 5>> pool;
pool.push_back(5);
pool.push_back(3);
pool.push_back(523);
for(const auto& p : pool) {
std::cout << p << std::endl;
}
pool.pop_back();
for(const auto& p : pool) {
std::cout << p << std::endl;
}
}
该程序的输出是
我希望是
ObjectPool: allocate whatever // this is space for 5
ObjectPool: construct // constructs 5
ObjectPool: allocate whatever // this is space for 3
ObjectPool: construct // constructs 3
ObjectPool: allocate whatever // this is space for 523
ObjectPool: construct // constructs 523, but actual output gives garbage value
ObjectPool: destroy // destroys 523
ObjectPool: deallocate // deallocates 523
ObjectPool: destroy // destroys 3
ObjectPool: destroy // destroys 5
ObjectPool: deallocate // deallocates 3 and 5
您可以看到,只要只需调用一次,construct方法就被调用3次。
为什么523是垃圾?不执行pool.reserve(5)
,如何获得预期的输出?可能吗?
传递给ObjectPool::allocate
的值是将连续存储在内存中的对象数。这意味着在调用ObjectPool::allocate
时,您需要返回一个指针,该指针至少包含allocator(2)
个块。您的分配器仅返回指向单个块的指针。向量构造函数将第2个(或第3个)元素添加到新生成的向量中时,它将覆盖尚未明确分配的内存。下一次对2 * sizeof(T)
的调用将分配该内存,从而导致向量损坏。
向量的分配内存是连续的。首次调用allocator
时,将为该向量分配一个元素(其容量为1)。这将生成输出的第1-2行。
在第二次调用push_back
时,由于向量的容量已满,因此将请求一个新块。这将生成输出的第2-7行。第4行将现有元素复制到新的存储块中,第5行构建刚刚添加的新元素,第6行从原始存储块中销毁该元素。第7行是释放原始内存块(返回到分配器)的时间。该向量的容量为2。
push_back
的下一次调用将再次调整向量的大小,从而生成输出的第8-14行。第9-10行将现有元素复制到新的内存块中,第11行构造了新添加的元素,第12-13行在旧存储块中销毁了它们,而第14行将旧存储块返回给了分配器。
下一行的输出已损坏,因为以后对分配器的调用将返回指向矢量对象正在使用的内存的指针。结果数据复制将移动不正确或损坏的数据。
解决方案是让您的push_back
函数保留适当数量的块。 (因此,allocate
应该将allocate(2)
向前推进两个块,假设向前推进的两个块是连续的。)