I am working with a 3rd party C API set in C++ that has two methods of concern for this discussion:
(我正在使用C ++中的第3方C API集,该讨论有两种关注的方法:)
- It's equivalent of malloc(): the_api_malloc(size) (plus a matching the_api_free())
(它等效于malloc(): the_api_malloc(size) (加上匹配的the_api_free()))
- A function in which memory created with the_api_malloc() is returned to that takes ownership of it and the_api_free()'s it internally: the_api_give_back(ptr)
(返回使用the_api_malloc()创建的内存的函数,该函数拥有该函数的所有权,而the_api_free()在内部拥有该函数: the_api_give_back(ptr))
I have created a custom allocator wrapping the_api_malloc() and the_api_free() to use with for example std::vector.
(我创建了一个自定义分配器,用于包装the_api_malloc()和the_api_free()例如与std :: vector一起使用。)
This works great. (这很好。)
What I'd like to do is have a std::vector type class that utilizes my custom allocator but also has a release() method that when called, releases ownership of it's memory and therefor will not call my custom allocators the_api_free().
(我想做的是有一个使用我的自定义分配器的std :: vector类型类,还具有一个release()方法,该方法在被调用时释放其内存所有权,因此将不会调用我的自定义分配器the_api_free()。)
pointer release() /* pointer is of T* */
Example usage:
(用法示例:)
MyClass myClass(1024); // the_api_malloc()'s 1024 bytes
// ... do something with myClass
the_api_give_back(myClass.release());
I'm not sure the best way to pull this off.
(我不确定实现这一目标的最佳方法。)
What I have right now as a experiment is rather nasty: (我现在作为实验的内容非常讨厌:)
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;
}
}
Is there a better way?
(有没有更好的办法?)
UPDATE 1 : Here is what I've tried based on suggestions below.
(更新1 :这是根据以下建议我尝试过的。)
This should also help illustrate desired behavior & where it is currently failing. (这也应有助于说明所需的行为及其当前失败的地方。)
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
UPDATE 2: Tried creating a MyOwningAllocator and a MyNonOwningAllocator then swapping from the owning to the non-owning where at "release time", but can't get swap() to work as they are different types.
(更新2:尝试创建MyOwningAllocator和MyNonOwningAllocator,然后在“发布时”从拥有者交换到非拥有者,但是由于它们是不同类型,所以它们无法使swap()正常工作。)
ask by NuSkooler translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…