Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   How to use parameter in operator delete? (http://www.velocityreviews.com/forums/t947868-how-to-use-parameter-in-operator-delete.html)

Nephi Immortal 07-06-2012 01:42 AM

How to use parameter in operator delete?
 
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;
}

Victor Bazarov 07-06-2012 11:34 AM

Re: How to use parameter in operator delete?
 
On 7/5/2012 9:42 PM, Nephi Immortal wrote:
> 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
--
I do not respond to top-posted replies, please don't ask

Marcel Müller 07-08-2012 01:29 PM

Re: How to use parameter in operator delete?
 
On 06.07.12 03.42, Nephi Immortal wrote:
> 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


All times are GMT. The time now is 04:52 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.