C++ stateful allocator de-allocate issues

喜夏-厌秋 提交于 2019-12-24 06:42:12

问题


This issue is my misunderstanding of how the standard is using my custom allocator. I have a stateful allocator that keeps a vector of allocated blocks. This vector is pushed into when allocating and searched through during de-allocation.

From my debugging it appears that different instances of my object (this*'s differ) are being called on de-allocation. An example may be that MyAllocator (this* = 1) is called to allocate 20 bytes, then some time later MyAllocator (this* = 2) is called to de-allocate the 20 bytes allocated earlier. Abviously the vector in MyAllocator (this* = 2) doesn't contain the 20 byte block allocated by the other allocator so it fails to de-allocate. My understanding was that C++11 allows stateful allocators, what's going on and how do i fix this?

I already have my operator == set to only return true when this == &rhs

pseudo-code:

template<typename T>
class MyAllocator
{
    ptr allocate(int n)
    {
       ...make a block of size sizeof(T) * n
       blocks.push_back(block);
       return (ptr)block.start;
    }

    deallocate(ptr start, int n)
    {
        /*This fails because the the block array is not the
        same and so doesn't find the block it wants*/ 
        std::erase(std::remove_if(blocks.begin,blocks.end, []()
        {
            return block.start >= (uint64_t)ptr && block.end <= ((uint64_t)ptr + sizeof(T)*n);
        }), blocks.end);
    }

    bool operator==(const MyAllocator& rhs)
    {
        //my attempt to make sure internal states are same
        return this == &rhs;
    }
private:
  std::vector<MemoryBlocks> blocks;
}

Im using this allocator for an std::vector, on gcc. So as far as i know no weird rebind stuff is going on


回答1:


As @Igor mentioned, allocators must be copyable. Importantly though they must share their state between copies, even AFTER they have been copied from. In this case the fix was easy, i made the blocks vector a shared_ptr as suggested and then now on copy all the updates to that vector occur to the same vector, since they all point to the same thing.



来源:https://stackoverflow.com/questions/44739042/c-stateful-allocator-de-allocate-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!