如何实现像 std::function 这样的自毁类型擦除类



我想了解std::function的实现是如何工作的。为简单起见,让我们考虑不带参数的仅移动函数。

我知道std::function通过典型的类型擦除技术擦除其目标的类型:

template<class Result>
struct function
{
  public:
    template<class Function>
    function(Function&& f)
      : f_(std::make_unique<callable_base>(std::forward<Function>(f)))
    {}
    // XXX how to implement constructor with allocator?
    template<class Alloc, class Function>
    function(const Alloc& alloc, Function&& f);
    Result operator()() const
    {
      return (*f_)();
    }
  private:
    struct callable_base
    {
      virtual Result operator()() const = 0;
      virtual ~callable_base(){}
    };
    template<class Function>
    struct callable
    {
      mutable Function f;
      virtual Result operator()() const
      {
        return f;
      }
    };
    // XXX what should the deleter used below do?
    struct deleter;
    std::unique_ptr<callable_base, deleter> f_;
};

我想扩展这种类型的功能以支持自定义分配。我需要擦除分配器的类型,但这很难与使用std::unique_ptr一起使用.提供给unique_ptr的自定义删除器需要知道提供给构造函数的Function的具体类型,以便能够正确释放其存储。我可以使用另一种unique_ptr来键入擦除删除器,但该解决方案是循环的。

似乎callable<Function>需要自行分配。正确的方法是什么?如果我在callable<Function>的析构函数内部释放,那似乎还为时过早,因为它的成员还活着。

我认为不可能

以可移植的方式执行此操作,依靠提供的分配器进行内存管理。

我一直在寻找 std::shared_ptr 的实现,因为它也支持它的删除器和分配器的类型擦除(参见重载 6): 在我在这里找到的草图实现中,有一个辅助对象用于存储这些副本,但该对象是使用 operator new 分配的,并使用 operator delete 释放,从而绕过提供的分配器和删除器。

我正在考虑使用分配器的临时副本(在堆栈上),以便释放存储的分配器(从中制作副本)以及存储的对象。问题是:当您不知道类型时,如何在不使用 new/delete 的情况下获得副本?不幸的是,协方差被排除在外(需要返回指针)。

现在,我们得到了非标准的解决方案:如果您习惯使用alloca或可变长度数组,那么您可以使用一个删除器,在堆栈上创建一个足够大的内存区域,并让存储的分配器在该内存中创建自己的副本。然后,这个分配的堆栈(因此自动存储持续时间)副本可以释放存储的分配器和存储的对象,并最终被删除器函数破坏(这是所有这些的重点,它不知道分配器的具体类型)。粗略的草图:

struct aux_base {
  // also provide access to stored function
  virtual size_t my_size(void) const = 0;
  virtual aux_base * copy_in(void * memory) const = 0;
  virtual void free(void * ptr) = 0;
  virtual ~aux_base() {}
};
template<class Alloc, class Function>
struct aux : public aux_base {
  // Store allocator and function here
  size_t my_size(void) const {
    return sizeof(*this);
  }
  aux_base * copy_in(void * memory) const {
    // attention for alignment issues!
    return new (memory) aux(*this);
  }
  void free(void * ptr) {
    aux * stored = reinterpret_cast<aux *>(ptr);
    // do your stuff
  }
};
void deleter_for_aux(aux_base * ptr) {
  char memory[ptr->my_size()];
  aux_base * copy = ptr->copy_in(memory);
  copy->free(ptr);
  copy->~aux_base(); // call destructor
}

也就是说,如果有一种方法可以在标准C++中做到这一点,而不依赖于提供的分配器以外的其他动态内存源,我将非常乐意知道! :)

std::function在 C++17 中丢失了分配器,部分原因是类型擦除分配器的问题。但是,一般模式是将分配器重新绑定到用于执行类型擦除的任何类型,将原始分配器存储在类型擦除的东西中,并在删除类型擦除的东西时再次重新绑定分配器。

template<class Ret, class... Args>
struct Call_base {
    virtual Ret Call(Args&&...);
    virtual void DeleteThis();
protected:
    ~Call_base() {}
};
template<class Allocator, class Fx, class Ret, class... Args>
struct Call_fn : Call_base<Ret, Args...> {
    Allocator a;
    decay_t<Fx> fn;
    Call_fn(Allocator a_, Fx&& fn_)
        : a(a_), fn(forward<Fx>(fn_))
        {}
    virtual Ret Call(Args&& vals) override {
        return invoke(fn, forward<Args>(vals)...);
    }
    virtual void DeleteThis() override {
        // Rebind the allocator to an allocator to Call_fn:
        using ReboundAllocator = typename allocator_traits<Allocator>::
            template rebind_alloc<Call_fn>;
        ReboundAllocator aRebound(a);
        allocator_traits<ReboundAllocator>::destroy(aRebound, this);
        aRebound.deallocate(this, 1);
    }
};
template<class Allocator, class Fx, class Ret, class... Args>
Call_base<Ret, Args...> * Make_call_fn(Allocator a, Fx&& fn) {
    using TypeEraseType = Call_fn<Allocator, Fx, Ret, Args...>;
    using ReboundAllocator = typename allocator_traits<Allocator>::
        template rebind_alloc<TypeEraseType>;
    ReboundAllocator aRebound(a);
    auto ptr = aRebound.allocate(1); // throws
    try {
        allocator_traits<ReboundAllocator>::construct(aRebound, ptr, a, forward<Fx>(fn));
    } catch (...) {
        aRebound.deallocate(ptr, 1);
        throw;
    }
    return ptr;
}

这是我想出的一个近似值。我不相信这是完全正确的,但它适用于我的用例。

这个想法是使用带有unique_ptr的"无操作"删除器。删除程序调用对象的析构函数,但不释放其存储。对象通过回调在其析构函数中自解除分配。

template<class Result>
struct function
{
  public:
    template<class Function>
    function(Function&& f)
      : f_(std::make_unique<callable_base>(std::forward<Function>(f)))
    {}
    template<class Alloc, class Function>
    function(const Alloc& alloc, Function&& f)
      : f_(allocate_unique(alloc, std::forward<Function>(f)))
    {}
    Result operator()() const
    {
      return (*f_)();
    }
  private:
    struct callable_base
    {
      // a deallocation callback to use within our destructor
      using deallocate_function_type = void(*)(callable_base*);
      deallocate_function_type deallocate_function;
      template<class Function>
      callable_base(Function callback)
        : deallocate_function(callback)
      {}
      virtual Result operator()() const = 0;
      virtual ~callable_base()
      {
        // deallocate this object's storage with the callback
        deallocate_function(this);
      }
    };
    template<class Alloc, class Function>
    struct callable : callable_base
    {
      mutable Function f;
      callable(Function&& f)
        : callable_base(deallocate),
          f(std::forward<Function>(f))
      {}
      virtual Result operator()() const
      {
        return f;
      }
      static void deallocate(callable_base* ptr)
      {
        // upcast to the right type of pointer
        callable* self = static_cast<callable*>(ptr);
        // XXX it seems like creating a new allocator here is cheating
        //     instead, we should use some member allocator, but it's
        //     not clear where to put it
        Alloc alloc;
        alloc.deallocate(self);
      }
    };
    struct self_deallocator_deleter
    {
      template<class T>
      void operator()(T* ptr) const
      {
        // call T's destructor but do not deallocate ptr
        ptr->~T();
      }
    };
    template<class Alloc, class Function>
    static std::unique_ptr<callable_base, self_deallocator_deleter>
      allocate_unique(const Alloc& alloc, Function&& f)
    {
      // allocate and construct the concrete callable object
      auto f_ptr = std::allocator_traits<Alloc>::allocate(alloc, 1);
      std::allocator_traits<Alloc>::construct(f_ptr, std::forward<Function>(f));
      // return the pointer through a unique_ptr
      return std::unique_ptr<callable_base,self_deallocator_deleter>(f_ptr);
    }
    std::unique_ptr<callable_base, self_deallocator_deleter> f_;
};

如果给定的分配器成为callable对象的成员,而不是在callable::deallocate内动态创建新的分配器对象,则解决方案会更好。问题是我们不能使分配器成为callable的成员,因为callable对象在调用callable::deallocate时不再处于活动状态。

最新更新