diff options
author | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2010-02-18 21:29:52 (GMT) |
---|---|---|
committer | Oswald Buddenhagen <oswald.buddenhagen@nokia.com> | 2010-02-22 17:12:16 (GMT) |
commit | e38f2b026a00acafab96b1fcc7c27a4446ec49d1 (patch) | |
tree | c8f20dbeec8e089f86a3b67f16e116e23d43c55f /src/corelib/tools | |
parent | 4967231a9e5c39de3a60b61db9b6a71266a04829 (diff) | |
download | Qt-e38f2b026a00acafab96b1fcc7c27a4446ec49d1.zip Qt-e38f2b026a00acafab96b1fcc7c27a4446ec49d1.tar.gz Qt-e38f2b026a00acafab96b1fcc7c27a4446ec49d1.tar.bz2 |
make queues to which only lists are appended not blow the memory
append2(list) didn't use the array shifting logic the append() for
single elements does. this would cause the list to grow endlessly
despite leading elements being removed if only lists were ever appended.
Reviewed-by: joao
Diffstat (limited to 'src/corelib/tools')
-rw-r--r-- | src/corelib/tools/qlist.cpp | 36 | ||||
-rw-r--r-- | src/corelib/tools/qlist.h | 1 |
2 files changed, 19 insertions, 18 deletions
diff --git a/src/corelib/tools/qlist.cpp b/src/corelib/tools/qlist.cpp index 39c1beb..249b8d1 100644 --- a/src/corelib/tools/qlist.cpp +++ b/src/corelib/tools/qlist.cpp @@ -180,22 +180,30 @@ void QListData::realloc(int alloc) d->begin = d->end = 0; } -// ensures that enough space is available to append one element -void **QListData::append() +// ensures that enough space is available to append n elements +void **QListData::append(int n) { Q_ASSERT(d->ref == 1); - if (d->end == d->alloc) { - int n = d->end - d->begin; - if (d->begin > 2 * d->alloc / 3) { + int e = d->end; + if (e + n > d->alloc) { + int b = d->begin; + if (b - n >= 2 * d->alloc / 3) { // we have enough space. Just not at the end -> move it. - ::memcpy(d->array, d->array + d->begin, n * sizeof(void *)); + e -= b; + ::memcpy(d->array, d->array + b, e * sizeof(void *)); d->begin = 0; - d->end = n; } else { - realloc(grow(d->alloc + 1)); + realloc(grow(d->alloc + n)); } } - return d->array + d->end++; + d->end = e + n; + return d->array + e; +} + +// ensures that enough space is available to append one element +void **QListData::append() +{ + return append(1); } // ensures that enough space is available to append the list @@ -219,15 +227,7 @@ void **QListData::append(const QListData& l) // ensures that enough space is available to append the list void **QListData::append2(const QListData& l) { - Q_ASSERT(d->ref == 1); - int e = d->end; - int n = l.d->end - l.d->begin; - if (n) { - if (e + n > d->alloc) - realloc(grow(e + n)); - d->end += n; - } - return d->array + e; + return append(l.d->end - l.d->begin); } void **QListData::prepend() diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 5364e8e..fdebd7d 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -80,6 +80,7 @@ struct Q_CORE_EXPORT QListData { static Data shared_null; Data *d; void **erase(void **xi); + void **append(int n); void **append(); void **append(const QListData &l); void **append2(const QListData &l); // remove in 5.0 |