flexibility of operator new

G

Glen Able

I'd like to use a few different memory arenas in my current application and
to be able to specify for each allocation where it comes from. Is there any
way I can make operator new take another argument to specify which arena an
object gets allocated from. Or fake it?

e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;
 
V

Victor Bazarov

Glen said:
I'd like to use a few different memory arenas in my current application and
to be able to specify for each allocation where it comes from. Is there any
way I can make operator new take another argument to specify which arena an
object gets allocated from. Or fake it?

e.g. I'd like to be able to write something like this:

A* myA = new_bigheap A;
A* otherA = new_fastheap A;

Yes. If you overload operator new for your class 'A', you may give
it different arguments (in addition to the size it usually takes) and
this will allow you to invoke different operator new functions as you
desire. Something like

A* myA = new (A::bigheap) A;
A* otherA = new (A::fastheap) A;

where 'bigheap' and 'fastheap' are different constants of, say, two
different nested in A types:


class A {
...
class BigHeap {};
static const BigHeap bigheap;
class FastHeap {};
static const FastHeap fastheap;

void* operator new(size_t s, BigHeap const&);
void* operator new(size_t s, FastHeap const&);
};

Disclaimer: this is just to give you an idea; please refer to a good
book for more information.

Victor
 
G

Glen Able

e.g. I'd like to be able to write something like this:
Yes. If you overload operator new for your class 'A', you may give
it different arguments (in addition to the size it usually takes) and
this will allow you to invoke different operator new functions as you
desire. Something like

A* myA = new (A::bigheap) A;
A* otherA = new (A::fastheap) A;

where 'bigheap' and 'fastheap' are different constants of, say, two
different nested in A types:


class A {
...
class BigHeap {};
static const BigHeap bigheap;
class FastHeap {};
static const FastHeap fastheap;

void* operator new(size_t s, BigHeap const&);
void* operator new(size_t s, FastHeap const&);
};

Disclaimer: this is just to give you an idea; please refer to a good
book for more information.

Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?

Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?

Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bigheap" ?

thanks,
G.A.
 
V

Victor Bazarov

Glen said:
Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?

I suppose that should do.
Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?

I am not sure I understand, but you're probably right.
Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bigheap" ?

'new' is a keyword. In most modern editors it would be highlighted. You
would have to edit the set of keywords your editor recognises in order to
get 'new_bigheap' to highlight. If we're talking readability, of course.

Victor
 
T

Tom Widmer

Ah nice, thanks.

Now, I assume I have to either add matching operator deletes and always
remember to call the correct one, or alternatively embed some sort of
id/reference to the correct heap object in the memory returned from new?

The latter. If you do:

delete ptr;

then the basic version of operator delete is called. The only time
your overloaded version gets called is in order to release memory
allocated during a new for an object whose constructor threw an
exception.
Also, I guess I could also do this using a global new instead, so all my
classes can be allocated with this scheme?

Yup. You just want to override the basic global new and delete, and an
overload to use your pool system (note global operator new overloads
must be in the global namespace).

You can also put the allocation functions in a base class that you can
derive your objects from if you want to make them allocable using your
special pool system. I agree that the global new overload does seem
like a less invasive method.
Finally, does anyone have a opinion (aesthetic or otherwise) about using a
macro so "new (A::bigheap)" could be the easier-on-the-eye "new_bigheap" ?

The former is namespace aware, etc. Macros are generally considered
evil.

Tom
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top