我与C++中的第三方C API集,有讨论关注两种方式工作:自定义分配器的std :: vector <>与释放?
- 它等同的malloc():the_api_malloc(大小)(加上匹配the_api_free() )
- 其中存储器与the_api_malloc创建()返回到一个函数具有的,并the_api_free()的所有权它在内部:the_api_give_back(PTR)
我已创建自定义分配器wrapp ing 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;
}
}
有没有更好的方法?
UPDATE 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然后从所属交换到非拥有在哪里“释放时间”,但不能得到交换()来工作,因为他们是不同的类型。
分配器没有状态,所以我不知道我怎么能在一个标志? – NuSkooler 2010-08-27 03:22:36
@NuSkooler:更准确地说,标准库不是*必需*来支持具有状态的分配器。尽管如此,这是我第一次尝试。如果内存服务,C++ 0x要求他们支持有状态的分配器,所以我猜想有一个很好的机会它会工作。如果你觉得真的需要,我只会做别的事情。 – 2010-08-27 03:35:16
我在自定义分配器中创建了一个release_ownership(),用于设置成员bool的值。在deallocate()中,bool被选中,如果为true,则不执行任何操作。然而,release_ownership()和deallocate()之间的地址总是不同,所以bool总是为false(它是默认的)。 – NuSkooler 2010-08-27 16:51:35