How to use parameter in operator delete?

N

Nephi Immortal

I implement operator new and operator delete by adding an extra parameter. If I want to implement one heap class and create more than one heap object, then both operator new and operator delete are able to select correctheap object. How can I do that?
I do not want to create operator new and operator delete inside class body. It is independent to work with all types.

class Heap
{
public:
Heap() {}
void run() {}
};

void* operator new(
size_t size,
Heap& heap
)
{
heap.run();
return 0;
}

void operator delete(
void* p,
Heap& heap
)
{
heap.run();
}

int main()
{
Heap heap;
char* byte = new( heap ) char;
delete( heap ) byte; // ? does not compile -- how?

return 0;
}
 
V

Victor Bazarov

class Heap
{
public:
Heap() {}
void run() {}
};

void* operator new(
size_t size,
Heap& heap
)
{
heap.run();
return 0;
}

void operator delete(
void* p,
Heap& heap
)

While your compiler allows you to define this form, you can't call it
directly. There is no syntax for that.
{
heap.run();
}

int main()
{
Heap heap;
char* byte = new( heap ) char;
delete( heap ) byte; // ? does not compile -- how?

There is no "placement delete". The special operator delete can only be
defined to be called by the compiler in case the constructor that was
called in the placement new form throws an exception.
return 0;
}

You can't pass an unrelated object to the operator delete function. You
will only pass your object to the operator delete function that has been
defined in the class of that object, not the global one. If you can
live with your own global heap, though, this works:

#include <iostream>
#include <cstdlib>

class Heap
{
public:
Heap() {}
void run(void* p = 0) {
std::cout << "Heap::run was called with p=" << p << "\n";
}
};

Heap heap;

void* operator new(
size_t size,
Heap& heap
)
{
heap.run();
return std::malloc(size);
}

void operator delete(
void* p)
{
heap.run(p);
std::free(p);
}

int main()
{
char* byte = new( heap ) char;
delete byte;

return 0;
}

V
 
M

Marcel Müller

I implement operator new and operator delete by adding an extra parameter. If I want to implement one heap class and create more than one heap object, then both operator new and operator delete are able to select correct heap object. How can I do that?

For operator new this is straight forward, as you have seen.
For operator delete the important question is in which heap is the
object to be deleted and where do you get this information from. You do
not want to do this manually, i.e. by passing the (hopefully) correct
heap to operator delete. This would be very error prone.

The heap needs to be tied to the object at the invocation of operator
new. You could do this intrusive or non-intrusive.
You eliminated the intrusive Version with class individual new/delete
operators already.
So the non-intrusive version is left:

class Heap
{public:
void* allocate(size_t size);
void free(void*);
};

void* operator new(size_t size, Heap& heap)
{ Heap** ptr = heap.allocate(size + sizeof(Heap*));
*ptr = &heap;
return ptr + 1;
}

void* operator new(size_t size)
{ Heap** ptr = (Heap**)std::malloc(size + sizeof(Heap*));
*ptr = NULL;
return ptr + 1;
}

void operator delete(void* p)
{ Heap** hp = (Heap**)p;
if (*hp)
(*hp)->free(hp);
else
std::free(hp);
}

Optionally the same for new[]/delete[].

The price of the non-intrusive solution is that /every/ allocation takes
space for an additional pointer. Unless you have an embedded environment
with low memory this should not cause much concern.

I do not want to create operator new and operator delete inside class body. It is independent to work with all types.

A common base class could do the job.


Marcel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top