From e38f2b026a00acafab96b1fcc7c27a4446ec49d1 Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Thu, 18 Feb 2010 22:29:52 +0100 Subject: 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 --- src/corelib/tools/qlist.cpp | 36 ++++++++++++++++++------------------ 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 -- cgit v0.12