Migrate QByteArray over QArrayData.

For the time being QByteArrayData keeps its independent existence, for
the sake of other modules. Once they have been ported to use the new
initializer macros it can be changed to:

    struct QByteArrayData { QArrayData array; };

Extra braces can then be added to the macros.

With respect to source compatibility, the only concern is with other
modules, as QByteArrayData has already changed in incompatible ways with
Qt 4.x

Done-with: João Abecasis
Change-Id: I044e2a680317431777a098feec8839a90a3d3da3
Reviewed-by: João Abecasis <joao.abecasis@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Jędrzej Nowacki 2012-03-27 13:17:46 +02:00 committed by Qt by Nokia
parent 78b780b9a1
commit d1f5a85e66
2 changed files with 40 additions and 60 deletions

View File

@ -585,7 +585,7 @@ QByteArray qUncompress(const uchar* data, int nbytes)
d->data()[len] = 0; d->data()[len] = 0;
{ {
QByteArrayDataPtr dataPtr = { d.take() }; QByteArrayDataPtr dataPtr = { reinterpret_cast<QByteArrayData *>(d.take()) };
return QByteArray(dataPtr); return QByteArray(dataPtr);
} }
@ -618,9 +618,6 @@ static inline char qToLower(char c)
return c; return c;
} }
const QStaticByteArrayData<1> QByteArray::shared_null = { Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(0), { 0 } };
const QStaticByteArrayData<1> QByteArray::shared_empty = { Q_STATIC_BYTE_ARRAY_DATA_HEADER_INITIALIZER(0), { 0 } };
/*! /*!
\class QByteArray \class QByteArray
\brief The QByteArray class provides an array of bytes. \brief The QByteArray class provides an array of bytes.
@ -897,7 +894,7 @@ QByteArray &QByteArray::operator=(const QByteArray & other)
{ {
other.d->ref.ref(); other.d->ref.ref();
if (!d->ref.deref()) if (!d->ref.deref())
free(d); Data::deallocate(d);
d = other.d; d = other.d;
return *this; return *this;
} }
@ -913,9 +910,9 @@ QByteArray &QByteArray::operator=(const char *str)
{ {
Data *x; Data *x;
if (!str) { if (!str) {
x = shared_null.data_ptr(); x = Data::sharedNull();
} else if (!*str) { } else if (!*str) {
x = shared_empty.data_ptr(); x = Data::allocate(0);
} else { } else {
int len = strlen(str); int len = strlen(str);
if (d->ref.isShared() || uint(len) + 1u > d->alloc if (d->ref.isShared() || uint(len) + 1u > d->alloc
@ -927,7 +924,7 @@ QByteArray &QByteArray::operator=(const char *str)
} }
x->ref.ref(); x->ref.ref();
if (!d->ref.deref()) if (!d->ref.deref())
free(d); Data::deallocate(d);
d = x; d = x;
return *this; return *this;
} }
@ -1322,20 +1319,16 @@ void QByteArray::chop(int n)
QByteArray::QByteArray(const char *data, int size) QByteArray::QByteArray(const char *data, int size)
{ {
if (!data) { if (!data) {
d = shared_null.data_ptr(); d = Data::sharedNull();
} else { } else {
if (size < 0) if (size < 0)
size = strlen(data); size = strlen(data);
if (!size) { if (!size) {
d = shared_empty.data_ptr(); d = Data::allocate(0);
} else { } else {
d = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u)); d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d); Q_CHECK_PTR(d);
d->ref.initializeOwned();
d->size = size; d->size = size;
d->alloc = uint(size) + 1u;
d->capacityReserved = false;
d->offset = sizeof(QByteArrayData);
memcpy(d->data(), data, size); memcpy(d->data(), data, size);
d->data()[size] = '\0'; d->data()[size] = '\0';
} }
@ -1352,15 +1345,11 @@ QByteArray::QByteArray(const char *data, int size)
QByteArray::QByteArray(int size, char ch) QByteArray::QByteArray(int size, char ch)
{ {
if (size <= 0) { if (size <= 0) {
d = shared_empty.data_ptr(); d = Data::allocate(0);
} else { } else {
d = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u)); d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d); Q_CHECK_PTR(d);
d->ref.initializeOwned();
d->size = size; d->size = size;
d->alloc = uint(size) + 1u;
d->capacityReserved = false;
d->offset = sizeof(QByteArrayData);
memset(d->data(), ch, size); memset(d->data(), ch, size);
d->data()[size] = '\0'; d->data()[size] = '\0';
} }
@ -1374,13 +1363,9 @@ QByteArray::QByteArray(int size, char ch)
QByteArray::QByteArray(int size, Qt::Initialization) QByteArray::QByteArray(int size, Qt::Initialization)
{ {
d = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u)); d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d); Q_CHECK_PTR(d);
d->ref.initializeOwned();
d->size = size; d->size = size;
d->alloc = uint(size) + 1u;
d->capacityReserved = false;
d->offset = sizeof(QByteArrayData);
d->data()[size] = '\0'; d->data()[size] = '\0';
} }
@ -1396,7 +1381,6 @@ QByteArray::QByteArray(int size, Qt::Initialization)
\sa size(), truncate() \sa size(), truncate()
*/ */
void QByteArray::resize(int size) void QByteArray::resize(int size)
{ {
if (size < 0) if (size < 0)
@ -1408,9 +1392,9 @@ void QByteArray::resize(int size)
} }
if (size == 0 && !d->capacityReserved) { if (size == 0 && !d->capacityReserved) {
Data *x = shared_empty.data_ptr(); Data *x = Data::allocate(0);
if (!d->ref.deref()) if (!d->ref.deref())
free(d); Data::deallocate(d);
d = x; d = x;
} else if (d->size == 0 && d->ref.isStatic()) { } else if (d->size == 0 && d->ref.isStatic()) {
// //
@ -1421,13 +1405,9 @@ void QByteArray::resize(int size)
// which is used in place of the Qt 3 idiom: // which is used in place of the Qt 3 idiom:
// QByteArray a(sz); // QByteArray a(sz);
// //
Data *x = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u)); Data *x = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(x); Q_CHECK_PTR(x);
x->ref.initializeOwned();
x->size = size; x->size = size;
x->alloc = uint(size) + 1u;
x->capacityReserved = false;
x->offset = sizeof(QByteArrayData);
x->data()[size] = '\0'; x->data()[size] = '\0';
d = x; d = x;
} else { } else {
@ -1467,17 +1447,13 @@ void QByteArray::reallocData(uint alloc, bool grow)
alloc = qAllocMore(alloc, sizeof(Data)); alloc = qAllocMore(alloc, sizeof(Data));
if (d->ref.isShared() || IS_RAW_DATA(d)) { if (d->ref.isShared() || IS_RAW_DATA(d)) {
Data *x = static_cast<Data *>(malloc(sizeof(Data) + alloc)); Data *x = Data::allocate(alloc);
Q_CHECK_PTR(x); Q_CHECK_PTR(x);
x->ref.initializeOwned();
x->size = qMin(int(alloc) - 1, d->size); x->size = qMin(int(alloc) - 1, d->size);
x->alloc = alloc;
x->capacityReserved = d->capacityReserved;
x->offset = sizeof(QByteArrayData);
::memcpy(x->data(), d->data(), x->size); ::memcpy(x->data(), d->data(), x->size);
x->data()[x->size] = '\0'; x->data()[x->size] = '\0';
if (!d->ref.deref()) if (!d->ref.deref())
free(d); Data::deallocate(d);
d = x; d = x;
} else { } else {
Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc)); Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc));
@ -2733,8 +2709,8 @@ QByteArray QByteArray::toUpper() const
void QByteArray::clear() void QByteArray::clear()
{ {
if (!d->ref.deref()) if (!d->ref.deref())
free(d); Data::deallocate(d);
d = shared_null.data_ptr(); d = Data::sharedNull();
} }
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE)) #if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
@ -3176,7 +3152,7 @@ QByteArray QByteArray::trimmed() const
} }
int l = end - start + 1; int l = end - start + 1;
if (l <= 0) { if (l <= 0) {
QByteArrayDataPtr empty = { shared_empty.data_ptr() }; QByteArrayDataPtr empty = { reinterpret_cast<QByteArrayData *>(Data::allocate(0)) };
return QByteArray(empty); return QByteArray(empty);
} }
return QByteArray(s+start, l); return QByteArray(s+start, l);
@ -3256,7 +3232,7 @@ QByteArray QByteArray::rightJustified(int width, char fill, bool truncate) const
return result; return result;
} }
bool QByteArray::isNull() const { return d == &shared_null.ba; } bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); }
/*! /*!
@ -3884,19 +3860,14 @@ QByteArray QByteArray::fromRawData(const char *data, int size)
{ {
Data *x; Data *x;
if (!data) { if (!data) {
x = shared_null.data_ptr(); x = Data::sharedNull();
} else if (!size) { } else if (!size) {
x = shared_empty.data_ptr(); x = Data::allocate(0);
} else { } else {
x = static_cast<Data *>(malloc(sizeof(Data))); x = Data::fromRawData(data, size);
Q_CHECK_PTR(x); Q_CHECK_PTR(x);
x->ref.initializeOwned();
x->size = size;
x->alloc = 0;
x->capacityReserved = false;
x->offset = data - reinterpret_cast<char *>(x);
} }
QByteArrayDataPtr dataPtr = { x }; QByteArrayDataPtr dataPtr = { reinterpret_cast<QByteArrayData *>(x) };
return QByteArray(dataPtr); return QByteArray(dataPtr);
} }

View File

@ -44,6 +44,7 @@
#include <QtCore/qrefcount.h> #include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h> #include <QtCore/qnamespace.h>
#include <QtCore/qarraydata.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -121,6 +122,8 @@ template <typename T> class QList;
struct QByteArrayData struct QByteArrayData
{ {
// Keep in sync with QArrayData
QtPrivate::RefCount ref; QtPrivate::RefCount ref;
int size; int size;
uint alloc : 31; uint alloc : 31;
@ -132,6 +135,12 @@ struct QByteArrayData
inline const char *data() const { return reinterpret_cast<const char *>(this) + offset; } inline const char *data() const { return reinterpret_cast<const char *>(this) + offset; }
}; };
Q_STATIC_ASSERT(sizeof(QArrayData) == sizeof(QByteArrayData));
Q_STATIC_ASSERT(offsetof(QArrayData, ref) == offsetof(QByteArrayData, ref));
Q_STATIC_ASSERT(offsetof(QArrayData, size) == offsetof(QByteArrayData, size));
// Can't use offsetof on bitfield members alloc, capacityReserved
Q_STATIC_ASSERT(offsetof(QArrayData, offset) == offsetof(QByteArrayData, offset));
template<int N> struct QStaticByteArrayData template<int N> struct QStaticByteArrayData
{ {
QByteArrayData ba; QByteArrayData ba;
@ -194,11 +203,10 @@ struct QByteArrayDataPtr
# define QByteArrayLiteral(str) (str) # define QByteArrayLiteral(str) (str)
#endif #endif
class Q_CORE_EXPORT QByteArray class Q_CORE_EXPORT QByteArray
{ {
private: private:
typedef QByteArrayData Data; typedef QTypedArrayData<char> Data;
public: public:
inline QByteArray(); inline QByteArray();
@ -399,12 +407,13 @@ public:
int length() const { return d->size; } int length() const { return d->size; }
bool isNull() const; bool isNull() const;
Q_DECL_CONSTEXPR inline QByteArray(QByteArrayDataPtr dd) : d(dd.ptr) {} Q_DECL_CONSTEXPR inline QByteArray(QByteArrayDataPtr dd)
: d(reinterpret_cast<Data *>(dd.ptr))
{
}
private: private:
operator QNoImplicitBoolCast() const; operator QNoImplicitBoolCast() const;
static const QStaticByteArrayData<1> shared_null;
static const QStaticByteArrayData<1> shared_empty;
Data *d; Data *d;
void reallocData(uint alloc, bool grow = false); void reallocData(uint alloc, bool grow = false);
void expand(int i); void expand(int i);
@ -418,8 +427,8 @@ public:
inline DataPtr &data_ptr() { return d; } inline DataPtr &data_ptr() { return d; }
}; };
inline QByteArray::QByteArray(): d(shared_null.data_ptr()) { } inline QByteArray::QByteArray(): d(Data::sharedNull()) { }
inline QByteArray::~QByteArray() { if (!d->ref.deref()) free(d); } inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
inline int QByteArray::size() const inline int QByteArray::size() const
{ return d->size; } { return d->size; }