operator new

S

S S

Hi

I have a library which is using 'new' at many places. I want to
overload global new operator, but at the same time I do not want to
overload it for the users of this library. Specifically, I want to
overload it only in my code, not in user's code.

Class specific overloading is not option for me. (Because of heavy
usage of basic data types too)

I thought of defining 'new' in a namespace, and I could have used
'using namespace new_n' (something like that in some top level header
file to include in all over my code). But that is conflicting with
global new, hence I am not able to compile. Hence namespace seems not
good option??

Any help is greatly appreciated.

Thanks
 
B

Balog Pal

I have a library which is using 'new' at many places. I want to
overload global new operator, but at the same time I do not want to
overload it for the users of this library. Specifically, I want to
overload it only in my code, not in user's code.

What is that good for? What your overloaded version does different?
 
S

S S

What is that good for? What your overloaded version does different?

My allocator use different heap which ensures small pointer size for
some optimization purpose.
 
B

Balog Pal

I have a library which is using 'new' at many places. I want to
My allocator use different heap which ensures small pointer size for
some optimization purpose.

So you need delete to be perdfectly matched -- if you managed to have
several distinct new versions your application was to face high chance of
meltdown when the user calls delete...
 
S

S S

some optimization purpose.

So you need delete to be perdfectly matched -- if you managed to have
several distinct new versions your application was to face high chance of
meltdown when the user calls delete...

Of course. When I say 'new', I mean 'delete' too.
 
H

Howard Hinnant

My allocator use different heap which ensures small pointer size for
some optimization purpose.

The first thing I would do is to avoid "exposed" calls to new as often
as I could. I.e. wrap as many calls to new/delete into my class
constructors/destructors as possible.

Next, I would build some other API for allocating memory for myself,
and for constructing my objects, and then use that API. If I were
using standard containers (for example) I might choose to build an
allocator which calls my private heap, and then use my allocator in my
standard container use.

I might build or use smart pointers to help manage my heap in any
places where allocations still happen outside of my constructors and
destructors. shared_ptr is available on many platforms and allows a
custom deleter to be supplied at construction time. There are also
several unique_ptr implementations floating around. Both of these
smart pointers will become available in the next C++ standard.

One must ensure that any objects which use a custom heap do not
propagate outside of this library to the client's code, or if they do,
that their deallocation continues to point back into this library. If
one does transfer ownership of memory to other libraries (which points
back to the custom heap), one must warn clients that this library is
not dynamically unloadable (lest a crash will result when the
deallocation attempts to access the no-longer-existent heap).

Good luck.

-Howard
 
B

Balog Pal

S S said:
Of course. When I say 'new', I mean 'delete' too.

No you don't. new can be tied to a place of call, but delete gets the thing
to delete as parameter.
 
S

S S

No you don't.  new can be tied to a place of call, but delete gets the thing
to delete as parameter.

You are right. But I ensure user application is not getting anything
allocated from my heap. Hence now the question is, is it possible in
some way? I need it urgent but not finding anyway to do it.
 
J

Joe Smith

S S said:
You are right. But I ensure user application is not getting anything
allocated from my heap. Hence now the question is, is it possible in
some way? I need it urgent but not finding anyway to do it.

If you can change all the calls to new in your source to use a parameter,
you can create a new-with-paramters (like placement new, or nothrow new)
that allocates from your seperate heap.

The problem is delete. There is no equivlent delete-with-parameters format.
But delete is not magic like new. You can define a template function that
takes a pointer destroys the object, and deallocates the memory from your
heap. So as long as you are willing to replace all calls to delete with a
call to a template function things are easy enough.

So here is what i would do:
------------------------
struct libnew_t {} libnew; //tag object

void* operator new(std::size_t size, libnew_t) {
// return a pointer to size bytes allocated from your heap
}

//this function is called automatically if a constructor fails,
//and will be called manually from the libDelete template function.

void operator delete(void* pntr, libnew_t) {
// free memory pointed to by pntr from your heap
}

template<typename T>
void libDelete(T* pntr)
{
pntr->~T(); //destroy the object in place.
operator delete(pntr,libnew); //deallocate memory from heap.
}
-----------------------------


Then code like the following is what is needed instead of new and delete:
-----
class widget{/*...*/};
/*...*/

widget* mywidget= new (libnew) widget;

//use mywidget

libDelete(mywidget);
 
S

S S

If you can change all the calls to new in your source to use a parameter,
you can create a new-with-paramters (like placement new, or nothrow new)
that allocates from your seperate heap.

The problem is delete. There is no equivlent delete-with-parameters format.
But delete is not magic like new. You can define a template function that
takes a pointer destroys the object, and deallocates the memory from your
heap. So as long as you are willing to replace all calls to delete with a
call to a template function things are easy enough.

So here is what i would do:
------------------------
struct libnew_t {} libnew; //tag object

void* operator new(std::size_t size, libnew_t) {
// return a pointer to size bytes allocated from your heap

}

//this function is called automatically if a constructor fails,
//and will be called manually from the libDelete template function.

void operator delete(void* pntr, libnew_t) {
// free memory pointed to by pntr from your heap

}

template<typename T>
void libDelete(T* pntr)
{
   pntr->~T(); //destroy the object in place.
   operator delete(pntr,libnew); //deallocate memory from heap.}

-----------------------------

Then code like the following is what is needed instead of new and delete:
-----
class widget{/*...*/};
/*...*/

widget* mywidget= new (libnew) widget;

//use mywidget

libDelete(mywidget);

Thanks, but I can not change all new calls to placement calls. It
would be a big task.
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top