1

I'm trying to create a std::shared_ptr with a custom deleter where the allocation of the control block is joined with the one of the object itself.

The constructor of std::shared_ptr which accepts a custom deleter can't be used since this causes another allocation for the control block itself:

std::shared_ptr<int> ptr(new int(0), [] (int*) {
  // ...
});

Further I came over std::allocate_shared such that I accomplished to provide a custom allocator for the control block and object:

template <typename Object>
class my_allocator {
public:
  using value_type = Object;

  template <typename O>
  explicit my_allocator(my_allocator<O> const& other) {
  }

  Object* allocate(std::size_t n, const void* hint = nullptr) {
    assert(n == 1U);
    return static_cast<value_type*>(::operator new(sizeof(value_type)));
  }

  void deallocate(Object* p, std::size_t n) {
    assert(n == 1U);

    // My problem occurs here, the allocator is rebound to
    // an internal type provided by the standard library and
    // the actual pointer to the int can't be retrieved anymore.
    // The pointer can't be stored inside the allocator because it is
    // rebound to some internal type before allocation.
  }
};

// ...

using Object = int;
auto object = std::allocate_shared<Object>(my_allocator<Object>{},
                                           std::forward<Args>(args)...);

The issue here is that it's only possible to control the allocation and deallocation of the joined control block but it's not possible to provide a custom deleter which I want to use to delay the actual deletion of the object.

Is it possible to use a custom deleter but with only one joined allocation?

Naios
  • 1,513
  • 1
  • 12
  • 26
  • 4
    As indicated by https://stackoverflow.com/questions/34243367/how-to-pass-deleter-to-make-shared a custom deleter can't be passed to `std::make_shared`. – Naios Jan 25 '19 at 16:40
  • 1
    I'm not sure what do you want to achieve, but with help of `boost::intrusive_ptr` you can define control block yourself, this may help. – Alex Guteniev Jan 26 '19 at 11:29

0 Answers0