m4ster r0shi: It's an interesting approach, but in my case unusable because A's members have to remain unknown during the definition of the pointer class.
I ended up discarding the COW pointer idea because I realized making it at the same time safe and support all the required operations completely defeated the purpose, which was avoiding unnecessary dynamic allocations for an opaque pointer. It ended up involving even more allocations plus synchronization every time the class' main member is used.
Now I'm looking at the "fast pimpl" idiom and fast fixed-size allocators, and found something that didn't quite make sense to me.
In this book I'm reading, the author describes the following mechanism:
1. Allocate a sizeof(T)*256 buffer (this->data) that will be used to store allocated objects.
2. this->first*sizeof(T) is the first byte of the first unused block within this->data.
3. The first byte of every unused block is an index that multiplied by sizeof(T) is the first byte of the next unused block within this->data, thus forming a linked list of unused blocks.
The author explained his rationale for not using multibyte types for the index thusly:
We run into alignment issues. Imagine you build an allocator for 5-byte blocks. In this case, casting a pointer that points to such a 5-byte block to unsigned int engenders undefined behavior. This is the big problem. A character type has size 1 by definition and does not have any alignment problem because even the pointer to the raw memory points to unsigned char.
|
The first time I read this I though he was talking about endianness, but when I thought about it later, I realized that, regardless of endianness, a pointer to an integer points to the lowest byte of the integer.
E.g. 0x01020304 ([]=byte pointed to)
Little endian: [04] 03 02 01
Big endian: [01] 02 03 04
So, what's he talking about?