Re: Using memory pool with operators new/delete




"vtstmn" <vtstmn@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:2DE22AEC-B683-42C3-A93F-C6B5CB34C956@xxxxxxxxxxxxxxxx
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);
}


.



Relevant Pages

  • Re: Framework 2.0 array redim unsatisfactory performance
    ... implementation details of Redim, Redim Preserve, and List. ... significantly higher object allocated overhead then List. ... ReDim simply allocates a new ... but I do not even attempt to test its memory ...
    (microsoft.public.dotnet.languages.vb)
  • Re: CE6.0 Driver Pointer Marshalling - passing pointers out only?
    ... This is an array of pointers to buffers that get set up ... the driver allocates the buffers (via an internal allocation ... routine from its own block of reserved memory). ... So I can map ...
    (microsoft.public.windowsce.platbuilder)
  • Re: CE6.0 Driver Pointer Marshalling - passing pointers out only?
    ... I've got a pretty complex driver I'm porting up at the moment ... This is an array of pointers to buffers that get set up ... the driver allocates the buffers (via an internal allocation ... routine from its own block of reserved memory). ...
    (microsoft.public.windowsce.platbuilder)
  • Re : Re : memory allocation and freeing memory
    ... This means that the caller ... >> never frees memory, and the callee always frees all its ... then the caller allocates it and pass it ... > void ExprDestroy(Expr *expr) { ...
    (comp.lang.c)
  • Re: simplest dynamic memory manager / allocator
    ... You might find that 4tH doesn't reserve much memory for dynamic ... memory is allocated at the heap, ... 768 allocates 64 bytes. ... Every fragment is represented by a cell in the HAT. ...
    (comp.lang.forth)

Loading