我正在使用 C++ 中设置的第 3 方 C API,该讨论有两种关注方法:
- 它相当于 malloc():the_api_malloc(size)(加上匹配的 the_api_free())
- 使用 the_api_malloc() 创建的内存返回给该函数的函数,该函数在内部拥有它和 the_api_free() 的所有权:the_api_give_back(ptr)
我创建了一个自定义分配器,包装 the_api_malloc() 和 the_api_free() 以与例如 std::vector 一起使用。这很好用。
我想做的是有一个 std::vector 类型的类,它利用我的自定义分配器,但也有一个 release() 方法,当调用它时,释放它的内存所有权,因此不会调用我的自定义分配器 the_api_free()。
pointer release() /* pointer is of T* */
示例用法:
MyClass myClass(1024); // the_api_malloc()'s 1024 bytes
// ... do something with myClass
the_api_give_back(myClass.release());
我不确定实现这一目标的最佳方法。我现在做的实验相当讨厌:
class MyClass : public std::vector<char, MyAllocator<char> > {
public:
using typename std::vector<char, MyAllocator<char> >::pointer;
pointer release() {
// note: visual studio impl.
pointer p = this->_Myfirst;
this->_Myfirst = 0;
this->_Mylast = 0;
this->_Myend = 0;
return p;
}
}
有没有更好的办法?
更新1:这是我根据以下建议尝试过的。这也应该有助于说明所需的行为以及当前失败的位置。
template <class T>
class MyAllocator
{
public:
// types omitted for clarity
MyAllocator() : m_released(false) { }
template <class U>
MyAllocator(MyAllocator<U> const& a) : m_released(a.m_released) { }
// other ctors, dtors, etc. omitted for clarity
// note: allocate() utilizes the_api_malloc()
void deallocate(pointer p, size_type num)
{
if(!m_released) {
the_api_free(p);
}
}
void release_ownership() { m_released = true; }
bool m_released;
};
template <typename T>
char* ReleaseOwernship(T& container)
{
container.get_allocator().release_ownership();
return &container[0];
}
// usage:
{ // scope
std::vector<char, MyAllocator<char> > vec;
// ...do something to populate vec...
char* p = ReleaseOwnership(vec);
the_api_give_back(p); // this API takes ownership of p and will delete it itself
} // end scope - note that MyAllocator::deallocate() gets called here -- m_release is still false
更新 2:尝试创建一个 MyOwningAllocator 和一个 MyNonOwningAllocator 然后在“发布时间”从拥有的地方交换到非拥有的地方,但由于它们是不同的类型,所以无法让 swap() 工作。