d->data()[len] = 0;
{
- QByteArrayDataPtr dataPtr = { d.take() };
+ QByteArrayDataPtr dataPtr = { reinterpret_cast<QByteArrayData *>(d.take()) };
return QByteArray(dataPtr);
}
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
\brief The QByteArray class provides an array of bytes.
{
other.d->ref.ref();
if (!d->ref.deref())
- free(d);
+ Data::deallocate(d);
d = other.d;
return *this;
}
{
Data *x;
if (!str) {
- x = shared_null.data_ptr();
+ x = Data::sharedNull();
} else if (!*str) {
- x = shared_empty.data_ptr();
+ x = Data::allocate(0);
} else {
int len = strlen(str);
if (d->ref.isShared() || uint(len) + 1u > d->alloc
}
x->ref.ref();
if (!d->ref.deref())
- free(d);
+ Data::deallocate(d);
d = x;
return *this;
}
QByteArray::QByteArray(const char *data, int size)
{
if (!data) {
- d = shared_null.data_ptr();
+ d = Data::sharedNull();
} else {
if (size < 0)
size = strlen(data);
if (!size) {
- d = shared_empty.data_ptr();
+ d = Data::allocate(0);
} else {
- d = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u));
+ d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d);
- d->ref.initializeOwned();
d->size = size;
- d->alloc = uint(size) + 1u;
- d->capacityReserved = false;
- d->offset = sizeof(QByteArrayData);
memcpy(d->data(), data, size);
d->data()[size] = '\0';
}
QByteArray::QByteArray(int size, char ch)
{
if (size <= 0) {
- d = shared_empty.data_ptr();
+ d = Data::allocate(0);
} else {
- d = static_cast<Data *>(::malloc(sizeof(Data) + uint(size) + 1u));
+ d = Data::allocate(uint(size) + 1u);
Q_CHECK_PTR(d);
- d->ref.initializeOwned();
d->size = size;
- d->alloc = uint(size) + 1u;
- d->capacityReserved = false;
- d->offset = sizeof(QByteArrayData);
memset(d->data(), ch, size);
d->data()[size] = '\0';
}
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);
- d->ref.initializeOwned();
d->size = size;
- d->alloc = uint(size) + 1u;
- d->capacityReserved = false;
- d->offset = sizeof(QByteArrayData);
d->data()[size] = '\0';
}
\sa size(), truncate()
*/
-
void QByteArray::resize(int size)
{
if (size < 0)
}
if (size == 0 && !d->capacityReserved) {
- Data *x = shared_empty.data_ptr();
+ Data *x = Data::allocate(0);
if (!d->ref.deref())
- free(d);
+ Data::deallocate(d);
d = x;
} else if (d->size == 0 && d->ref.isStatic()) {
//
// which is used in place of the Qt 3 idiom:
// 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);
- x->ref.initializeOwned();
x->size = size;
- x->alloc = uint(size) + 1u;
- x->capacityReserved = false;
- x->offset = sizeof(QByteArrayData);
x->data()[size] = '\0';
d = x;
} else {
alloc = qAllocMore(alloc, sizeof(Data));
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);
- x->ref.initializeOwned();
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);
x->data()[x->size] = '\0';
if (!d->ref.deref())
- free(d);
+ Data::deallocate(d);
d = x;
} else {
Data *x = static_cast<Data *>(::realloc(d, sizeof(Data) + alloc));
void QByteArray::clear()
{
if (!d->ref.deref())
- free(d);
- d = shared_null.data_ptr();
+ Data::deallocate(d);
+ d = Data::sharedNull();
}
#if !defined(QT_NO_DATASTREAM) || (defined(QT_BOOTSTRAPPED) && !defined(QT_BUILD_QMAKE))
}
int l = end - start + 1;
if (l <= 0) {
- QByteArrayDataPtr empty = { shared_empty.data_ptr() };
+ QByteArrayDataPtr empty = { reinterpret_cast<QByteArrayData *>(Data::allocate(0)) };
return QByteArray(empty);
}
return QByteArray(s+start, l);
return result;
}
-bool QByteArray::isNull() const { return d == &shared_null.ba; }
+bool QByteArray::isNull() const { return d == QArrayData::sharedNull(); }
/*!
{
Data *x;
if (!data) {
- x = shared_null.data_ptr();
+ x = Data::sharedNull();
} else if (!size) {
- x = shared_empty.data_ptr();
+ x = Data::allocate(0);
} else {
- x = static_cast<Data *>(malloc(sizeof(Data)));
+ x = Data::fromRawData(data, size);
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);
}
#include <QtCore/qrefcount.h>
#include <QtCore/qnamespace.h>
+#include <QtCore/qarraydata.h>
#include <stdlib.h>
#include <string.h>
struct QByteArrayData
{
+ // Keep in sync with QArrayData
+
QtPrivate::RefCount ref;
int size;
uint alloc : 31;
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
{
QByteArrayData ba;
# define QByteArrayLiteral(str) (str)
#endif
-
class Q_CORE_EXPORT QByteArray
{
private:
- typedef QByteArrayData Data;
+ typedef QTypedArrayData<char> Data;
public:
inline QByteArray();
int length() const { return d->size; }
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:
operator QNoImplicitBoolCast() const;
- static const QStaticByteArrayData<1> shared_null;
- static const QStaticByteArrayData<1> shared_empty;
Data *d;
void reallocData(uint alloc, bool grow = false);
void expand(int i);
inline DataPtr &data_ptr() { return d; }
};
-inline QByteArray::QByteArray(): d(shared_null.data_ptr()) { }
-inline QByteArray::~QByteArray() { if (!d->ref.deref()) free(d); }
+inline QByteArray::QByteArray(): d(Data::sharedNull()) { }
+inline QByteArray::~QByteArray() { if (!d->ref.deref()) Data::deallocate(d); }
inline int QByteArray::size() const
{ return d->size; }