On Sunday, 16 de October de 2011 16:30:39 Thiago Macieira wrote: > - size always 16 bytes > - contains a qptrdiff offset, to still allow for fromRawData (which we'd > then introduce to QVector too, but not QList) > - can store at least one 1-bit flag, but 2 would be preferred > - size calculation reasonably fast
Here's an idea:
QAtomicInt ref;
int alloc;
union {
qptrdiff offset;
struct { int begin; int end; };
};
// size = 16 bytes
Allocation sizes are always positive, so the sign bit in alloc is free for our
use. If the sign bit is set (alloc < 0) then we're dealing with fromRawData.
If it's not set, we're dealing with data we allocated ourselves.
The two 1-bit flags can be stored in the sign bits of begin and end, but only
for own-allocated data. Currently, the two flags we use are sharable and
capacity, neither of which makes sense for raw data.
We'd have:
int size() const { return d->alloc < 0 ? -d->alloc : d->end - d->begin; }
T *data() { return d->array() + (d->alloc < 0 ? d->offset : d->begin); }
Advantages:
* 16 bytes on all platforms, regardless of pointer size
* 2 bits available for flags
* raw data is possible
* size calculation involves one extra comparison (the subtraction would have
been there anyway)
* the data() function above does not actually do any comparison on 32-bit
platforms, as offset and begin always occupy the same space
* the calculation to increase the size upon reallocation like:
if (newAlloc > d->alloc)
does not require special code to deal with raw data
Disadvantages:
* No wiggle room for *any* future expansion
* Storing the flags in the sign bits require bit fields or other manipulations
--
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
Software Architect - Intel Open Source Technology Center
PGP/GPG: 0x6EF45358; fingerprint:
E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358
signature.asc
Description: This is a digitally signed message part.
_______________________________________________ Qt5-feedback mailing list [email protected] http://lists.qt.nokia.com/mailman/listinfo/qt5-feedback
