Search code examples
c++shared-ptrpoollock-free

Does shared_ptr allocate when constructed from raw pointer?


As far as I understand shared_ptr it contains a pointer to a control block which contains the raw pointer strong count and weak count.

I'm in a situation where I need lock-free allocation, so I can allocate a raw pointer from the pool allocated memory and then make a unique pointer with a custom deleter to put it back in the pool, so far so good.

In the case of a shared_ptr however if I construct a shared pointer with one of these raw pointers and custom allocators does it allocate the control block at that point?

If it does is there a way to provide the memory for the control block in a lock-free way?


Solution

  • In the case of a shared_ptr however if I construct a shared pointer with one of these raw pointers and custom allocators does it allocate the control block at that point?

    Yes.

    If it does is there a way to provide the memory for the control block in a lock-free way?

    shared_ptr's constructor has overloads that accept a custom allocator.

    Refer to some shared_ptr documentation.

    (I'm not entirely clear whether the deleter will also be allocated by your custom allocator.)