Re: Using memory pool with operators new/delete
"vtstmn" <vtstmn@discussions.microsoft.com> wrote in message
news:2DE22AEC-B683-42C3-A93F-C6B5CB34C956@microsoft.com...
Hi all,
I want to create a class that manages a list of objects. The memory for
those objects has (for real-time performance reasons) to be allocated from
a
pre-allocated memory pool that manages only blocks of the same size (the
size
of the objects in the list).
Inside the class that specifies the objects to be inserted in the list, I
created the following operators:
inline void* node<T>::operator new(size_t size, pool< node<T> >* pool)
{
return pool->allocate();
}
inline void operator delete(void* pointer, pool< node<T> >* pool)
{
pool->release(pointer);
return;
}
When I manage the list of objects, I want to be able to create an object
with the following call:
[1] node<T>* tmp = new(_pool) node<T>();
and to delete an object with the following call:
[2] delete(tmp, _pool);
The call number [1] works correctly (it allocates the memory and calls the
constructor for object node<T>), but the call number [2] does not work (no
release called, no destructor called).
The compiler only inserts constructor and destructor calls when you use the
new and delete keywords, your line [1] is placement syntax for the new
keyword, but [2] is an ordinary function call.
.... wait a sec, you used "operator delete" to declare the function... it
can't be an ordinary function call but it also won't be called by the syntax
[2]. Use of the delete keyword never calls operator delete with multiple
arguments, only an exception thrown in the constructor will do that.
Oh... here's what is really going on:
The delete keyword has the following syntax (there's a second form for
arrays with the brackets thrown in):
delete cast-expression
Does this match [2]? Only if (tmp, _pool) is cast-expression. But sure
enough, that is an expression, involving the comma operator, which evaluates
the left side, then returns the right side. You've deleted _pool.
Try this:
template <typename T>
class node<T> {
// other stuff
public:
inline void operator delete(void* pointer, pool< node<T> >* pool){
pool->release(pointer);}
private:
inline void operator delete(void* pointer) {}
friend void ::delnode<T>(T* pointer, pool< node<T> >* pool);
}
template <typename T>
void ::delnode(T* pointer, pool< node<T> >* pool)
{
delete pointer;
pool->release(pointer);
}