On the richness of C++

Kevin Bealer kevinbealer at gmail.com
Fri Apr 11 15:38:54 PDT 2008


Sean Kelly Wrote:

> == Quote from Kevin Bealer (kevinbealer at gmail.com)'s article
> > Out of curiosity, what motivates your desire for placement new?
> 
> Constructing D objects in shared memory.  That's also an underlying
> reason why I added a way to override object monitors in Tango.
> 
> 
> Sean

I wondered about this myself -- at one point I sat down and designed a bunch of code
in C++ that used relative pointers and memory mapped areas.  The idea was to have
a complete set of tools for created "tied" containers and so on in memory mapped
files, so that you could create documents and random data structures without considering
the data format itself.

The relative pointer idea is that a smart pointer could be defined that actually stored the pointed to value minus the pointer's address.  This allows you to point to other objects in
the same memory mapped arena regardless of where it gets mapped to.  It's also nearly
free to use these because the address of the pointer being dereferenced is always itself
in a register, so the cost of dereferencing is exactly one subtraction with no IO overhead.

You can also use 32 bit pointers regardless of platform as long as your arena is <= 2GB,
but you need to worry about endianness.  Alternately, you could use 64 bit pointers with
a 32 bit arena# and 32 bit offset in that arena -- it's more flexible for multi-document systems but takes more computation.

Of course you need to redefine every type you use with this system -- even C++ classes that use allocator template parameters rarely come in a form that is templatized on smart-pointer type.  (And if you use virtual pointers, you need to do something about that -- I had some ideas but I never finished that part of it.)

Kevin




More information about the Digitalmars-d mailing list