diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-04-04 13:50:21 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-05 01:44:34 +0200 |
commit | e5d10b2a3be77244d8db3a4dac86168ee9b91e6f (patch) | |
tree | e9f2a63e957d95fd2f10a62c772aaa993bdd9f79 /src/corelib/tools/qbytearray.cpp | |
parent | 7488b79652cb18de3e2b295835c08a46f3e286f4 (diff) |
Move growth computation to re-allocation function
Callers of QByteArray/QString::realloc() are still responsible for the
heuristics and decide whether to provide the "grow" hint, but
computation is centralized there.
With this change we also ensure growth takes into account the
terminating null. Previously, calls to qAllocMore took into account
header and string size, for left out the null, meaning we ended up
allocating ("nice-size" + Null).
Change-Id: Iad1536e7706cd2d446daee96859db9b01c5f9680
Reviewed-by: Marius Storm-Olsen <marius.storm-olsen@nokia.com>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qbytearray.cpp')
-rw-r--r-- | src/corelib/tools/qbytearray.cpp | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/src/corelib/tools/qbytearray.cpp b/src/corelib/tools/qbytearray.cpp index 5961d682c5..731a1b163b 100644 --- a/src/corelib/tools/qbytearray.cpp +++ b/src/corelib/tools/qbytearray.cpp @@ -1432,7 +1432,7 @@ void QByteArray::resize(int size) } else { if (d->ref.isShared() || size > int(d->alloc) || (!d->capacityReserved && size < d->size && size < int(d->alloc) >> 1)) - realloc(qAllocMore(size, sizeof(Data))); + realloc(size, true); if (int(d->alloc) >= size) { d->size = size; d->data()[size] = '\0'; @@ -1459,8 +1459,11 @@ QByteArray &QByteArray::fill(char ch, int size) return *this; } -void QByteArray::realloc(int alloc) +void QByteArray::realloc(int alloc, bool grow) { + if (grow) + alloc = qAllocMore(alloc + 1, sizeof(Data)) - 1; + if (d->ref.isShared() || IS_RAW_DATA(d)) { Data *x = static_cast<Data *>(malloc(sizeof(Data) + alloc + 1)); Q_CHECK_PTR(x); @@ -1563,7 +1566,7 @@ QByteArray &QByteArray::prepend(const char *str, int len) { if (str) { if (d->ref.isShared() || d->size + len > int(d->alloc)) - realloc(qAllocMore(d->size + len, sizeof(Data))); + realloc(d->size + len, true); memmove(d->data()+len, d->data(), d->size); memcpy(d->data(), str, len); d->size += len; @@ -1581,7 +1584,7 @@ QByteArray &QByteArray::prepend(const char *str, int len) QByteArray &QByteArray::prepend(char ch) { if (d->ref.isShared() || d->size + 1 > int(d->alloc)) - realloc(qAllocMore(d->size + 1, sizeof(Data))); + realloc(d->size + 1, true); memmove(d->data()+1, d->data(), d->size); d->data()[0] = ch; ++d->size; @@ -1619,7 +1622,7 @@ QByteArray &QByteArray::append(const QByteArray &ba) *this = ba; } else if (ba.d != &shared_null.ba) { if (d->ref.isShared() || d->size + ba.d->size > int(d->alloc)) - realloc(qAllocMore(d->size + ba.d->size, sizeof(Data))); + realloc(d->size + ba.d->size, true); memcpy(d->data() + d->size, ba.d->data(), ba.d->size); d->size += ba.d->size; d->data()[d->size] = '\0'; @@ -1653,7 +1656,7 @@ QByteArray& QByteArray::append(const char *str) if (str) { int len = strlen(str); if (d->ref.isShared() || d->size + len > int(d->alloc)) - realloc(qAllocMore(d->size + len, sizeof(Data))); + realloc(d->size + len, true); memcpy(d->data() + d->size, str, len + 1); // include null terminator d->size += len; } @@ -1678,7 +1681,7 @@ QByteArray &QByteArray::append(const char *str, int len) len = qstrlen(str); if (str && len) { if (d->ref.isShared() || d->size + len > int(d->alloc)) - realloc(qAllocMore(d->size + len, sizeof(Data))); + realloc(d->size + len, true); memcpy(d->data() + d->size, str, len); // include null terminator d->size += len; d->data()[d->size] = '\0'; @@ -1695,7 +1698,7 @@ QByteArray &QByteArray::append(const char *str, int len) QByteArray& QByteArray::append(char ch) { if (d->ref.isShared() || d->size + 1 > int(d->alloc)) - realloc(qAllocMore(d->size + 1, sizeof(Data))); + realloc(d->size + 1, true); d->data()[d->size++] = ch; d->data()[d->size] = '\0'; return *this; |