#include <string.h>
#include <stdlib.h>
-#define IS_RAW_DATA(d) ((d)->offset != 0)
+#define IS_RAW_DATA(d) ((d)->offset != sizeof(QByteArrayData))
QT_BEGIN_NAMESPACE
}
d.take(); // realloc was successful
d.reset(p);
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
int res = ::uncompress((uchar*)d->data(), &len,
(uchar*)data+4, nbytes-4);
d->size = len;
d->alloc = len;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->data()[len] = 0;
return QByteArray(d.take(), 0, 0);
}
const QStaticByteArrayData<1> QByteArray::shared_null = { { Q_REFCOUNT_INITIALIZE_STATIC,
- 0, 0, 0, { 0 } }, { 0 } };
+ 0, 0, 0, sizeof(QByteArrayData) }, { 0 } };
const QStaticByteArrayData<1> QByteArray::shared_empty = { { Q_REFCOUNT_INITIALIZE_STATIC,
- 0, 0, 0, { 0 } }, { 0 } };
+ 0, 0, 0, sizeof(QByteArrayData) }, { 0 } };
/*!
\class QByteArray
d->size = size;
d->alloc = size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
memcpy(d->data(), data, size);
d->data()[size] = '\0';
}
d->size = size;
d->alloc = size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
memset(d->data(), ch, size);
d->data()[size] = '\0';
}
d->size = size;
d->alloc = size;
d->capacityReserved = false;
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->data()[size] = '\0';
}
if (size < 0)
size = 0;
- if (d->offset && !d->ref.isShared() && size < d->size) {
+ if (IS_RAW_DATA(d) && !d->ref.isShared() && size < d->size) {
d->size = size;
return;
}
x->size = size;
x->alloc = size;
x->capacityReserved = false;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
x->data()[size] = '\0';
d = x;
} else {
void QByteArray::realloc(int alloc)
{
- if (d->ref.isShared() || d->offset) {
+ if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = static_cast<Data *>(malloc(sizeof(Data) + alloc + 1));
Q_CHECK_PTR(x);
x->ref.initializeOwned();
x->size = qMin(alloc, d->size);
x->alloc = alloc;
x->capacityReserved = d->capacityReserved;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
::memcpy(x->data(), d->data(), x->size);
x->data()[x->size] = '\0';
if (!d->ref.deref())
Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc + 1));
Q_CHECK_PTR(x);
x->alloc = alloc;
- x->offset = 0;
+ x->offset = sizeof(QByteArrayData);
d = x;
}
}
QByteArray QByteArray::nulTerminated() const
{
// is this fromRawData?
- if (!d->offset)
+ if (!IS_RAW_DATA(d))
return *this; // no, then we're sure we're zero terminated
QByteArray copy(*this);
x->size = size;
x->alloc = 0;
x->capacityReserved = false;
- x->offset = data - (x->d + sizeof(qptrdiff));
+ x->offset = data - reinterpret_cast<char *>(x);
}
return QByteArray(x, 0, 0);
}
} else {
if (data) {
d->size = size;
- d->offset = data - (d->d + sizeof(qptrdiff));
+ d->offset = data - reinterpret_cast<char *>(d);
} else {
- d->offset = 0;
+ d->offset = sizeof(QByteArrayData);
d->size = 0;
*d->data() = 0;
}
int size;
uint alloc : 31;
uint capacityReserved : 1;
- union {
- qptrdiff offset; // will always work as we add/subtract from a ushort ptr
- char d[sizeof(qptrdiff)];
- };
- inline char *data() { return d + sizeof(qptrdiff) + offset; }
- inline const char *data() const { return d + sizeof(qptrdiff) + offset; }
+
+ qptrdiff offset;
+
+ inline char *data() { return reinterpret_cast<char *>(this) + offset; }
+ inline const char *data() const { return reinterpret_cast<const char *>(this) + offset; }
};
template<int N> struct QStaticByteArrayData
# define QByteArrayLiteral(str) ([]() -> QStaticByteArrayDataPtr<sizeof(str) - 1> { \
enum { Size = sizeof(str) - 1 }; \
static const QStaticByteArrayData<Size> qbytearray_literal = \
- { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, { 0 } }, str }; \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
return holder; }())
__extension__ ({ \
enum { Size = sizeof(str) - 1 }; \
static const QStaticByteArrayData<Size> qbytearray_literal = \
- { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, { 0 } }, str }; \
+ { { Q_REFCOUNT_INITIALIZE_STATIC, Size, 0, 0, sizeof(QByteArrayData) }, str }; \
QStaticByteArrayDataPtr<Size> holder = { &qbytearray_literal }; \
holder; })
#endif
inline const char *QByteArray::constData() const
{ return d->data(); }
inline void QByteArray::detach()
-{ if (d->ref.isShared() || d->offset) realloc(d->size); }
+{ if (d->ref.isShared() || (d->offset != sizeof(QByteArrayData))) realloc(d->size); }
inline bool QByteArray::isDetached() const
{ return !d->ref.isShared(); }
inline QByteArray::QByteArray(const QByteArray &a) : d(a.d)