Boost logo

Boost :

From: scleary_at_[hidden]
Date: 2001-10-11 15:33:40


-----Original Message-----
From: Gary.Zhang_at_[hidden] <Gary.Zhang_at_[hidden]>
>
> Hi I am trying to add an interface to pool using overloaded
> new/delete operators. The benefits are that
> 1) users do not need to be aware of the source of memory,
> 2) users can switch between various new/delete and compare
> their performance.
>
> I have the following problems.
> 1) the overloaded operator new [] does not provide
> element size or the number of elements, instead it provides
> size_t for the total number of bytes: sizeof(element) * N.
>
> I am aware system actually keeps such information in a compiler
> dependent way (through over allocation or associative array).
> How do I get the information in a standard way ?

There is no way to do this. We may just have to not support array
allocations with overloaded new from pools.

> 2) I can't somehow put new/delete operator inside a
> namespace though the global new/delete can be used as
> ::new and ::delete.
> For example,
> namespace pool {
> void * operator new (size_t s)
> }
>
> Widget *p = pool::new Widget // compilation error !
>
> What is the correct way to do this ?

I think you're trying to do two different things. If you want to overload
new, you have to pass it some extra arguments, i.e., a pool type from which
to allocate the memory:

class Widget { ... };
boost::pool pl;
Widget * p = new (pl) Widget;

There's another way to override new, though:

class Widget: public boost::pool_base { ... };
Widget * p = new Widget; // allocated from a global pool

I've thought of both these interfaces (they're totally different, though
they both "override new"), but haven't had any time to make them work. If
you're willing to give it a shot, I'd take a look at Scott Meyers' Effective
C++, items 8-10, and find a few other references, too -- it's not as easy as
it seems at first.

        -Steve


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk