 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Glen Low Guest
|
Posted: Tue Dec 30, 2003 12:42 pm Post subject: RFC offset_ptr |
|
|
Dear all,
I've been mulling over several things lately (no, not including the
seasonal mulled wine...), and I've got a proposal that I'd like your
comments on.
The big picture:
Dynamically allocated objects are typically strewn about the heap,
even though they are logically related. Pointers to these objects are
necessarily absolute.
This means:
1. Poor locality of reference, which stresses out today's CPU
architectures where the CPU and any cache is much faster than main
memory.
2. Serialization has to be done piecemeal, whereas if the objects were
all in one block, the block itself could be written out.
3. Transporting the objects (generalization of serialization) is
cumbersome, since the pointers are absolute. You can't easily move the
entire block to another spot in memory or over the network, since the
absolute pointers would have to be fixed up.
4. Heap allocation algorithms have to be fairly sophisicated and still
manage to be slow.
5. On some operating systems like Mach (Mac OS X) you can use VM
remapping to make COW copies of blocks of memory; however the absolute
pointers within force you to fix up the block after the copy, negating
the benefits of COW.
A proposal with two edges:
1. Define a (placement) operator new and operator delete, possibly
restricted to a class hierarchy, that allocates sequential memory from
a (heap allocated) block. When the block's memory is exhausted, the
entire block is copied over into a new block of 1.5x size, much like
the vector reallocation algorithm. Deletes are effectively ignored.
2. Define a new ptr template, to be called offset_ptr or relative_ptr,
which instead of storing an absolute address of memory, stores a
ptrdiff_t offset. When dereferenced or converted into a regular
pointer, it just takes it's own "this" pointer and adds the offset;
assignments and copy constructors have to be careful to recalculate
the offset appropriately. The semantics would be similar to regular
pointers or boost counted pointers (but w/o the reference counting).
Thus a program with some discipline, could easily create a sequential
block of memory with semantic content and with relative pointers. It's
not unprecedented for code, since some code loaders use some sort of
PC-relative mode instead of address fixups.
Any comments? Anybody tried something similar before in C++?
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Glen Low Guest
|
Posted: Wed Dec 31, 2003 1:01 am Post subject: Re: RFC offset_ptr |
|
|
| Quote: | 1. Define a (placement) operator new and operator delete, possibly
restricted to a class hierarchy, that allocates sequential memory from
a (heap allocated) block. When the block's memory is exhausted, the
entire block is copied over into a new block of 1.5x size, much like
the vector reallocation algorithm. Deletes are effectively ignored.
|
I'm thinking now that a custom standard-conforming allocator would be
good. Note that the allocator doesn't itself need to "reallocate",
since it is just dispensing parts of the block to clients. However:
1. I don't know if standard allocators are allowed to have
non-standard pointer types e.g. returning my offset_ptr.
2. Even if they were allowed, would standard-conforming containers use
them?
Is there a standard placement new syntax that uses an allocator?
Cheers,
Glen Low, Pixelglow Software
www.pixelglow.com
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|