Boost logo

Boost :

From: Philippe A. Bouchard (philippe_at_[hidden])
Date: 2003-06-05 12:45:43


Gregory Colvin wrote:

[...]

>> Let's say:
>> - you can easily detect weither an object was allocated on the stack
>> or on
>> the heap;
>> - a smart pointer contained within an object can somehow access it's
>> "object
>> header" when the object was allocated on the heap with a placement
>> operator
>> new();
>
> Neither of which can be done portably.

... "portably" and _efficiently_ ... exactly. But the possibility is still
there.

Let's say the placement operator new is using malloc(), then we could do
some reverse lookup into mallinfo to find the heap block in which some
address is related to. This information could then be cached into the smart
pointer itself making sizeof(smart_pointer) == (sizeof(void *) * 2) [if
shifted_ptr<> style is to be used]:

template <typename T>
    struct smart_pointer
    {
        ...

    private:
        T * _p;
        entity_header * _q;
    };

That would be quite fast, even for the reverse lookup at construction time.
The access time would still be atomic.

Philippe


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