Add a dedicated QListData::realloc_grow for growing QList

This hides the call to ::grow to now two places in the source code, so
it will be easier to fix the inefficient call to qAllocMore.

Change-Id: I5d1e6f7607404caa96e4ffff13e80a3e4cb0ee93
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
Reviewed-by: Alex Trotsenko <alex1973tr@gmail.com>
This commit is contained in:
Thiago Macieira 2015-06-15 15:34:56 -07:00
parent dafa3618d2
commit d9d9420d8d
2 changed files with 16 additions and 3 deletions

View File

@ -1,6 +1,7 @@
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Copyright (C) 2015 Intel Corporation.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
@ -148,6 +149,17 @@ void QListData::realloc(int alloc)
d->begin = d->end = 0;
}
void QListData::realloc_grow(int growth)
{
Q_ASSERT(!d->ref.isShared());
int alloc = grow(d->alloc + growth);
Data *x = static_cast<Data *>(::realloc(d, DataHeaderSize + alloc * sizeof(void *)));
Q_CHECK_PTR(x);
d = x;
d->alloc = alloc;
}
void QListData::dispose(Data *d)
{
Q_ASSERT(!d->ref.isShared());
@ -167,7 +179,7 @@ void **QListData::append(int n)
::memcpy(d->array, d->array + b, e * sizeof(void *));
d->begin = 0;
} else {
realloc(grow(d->alloc + n));
realloc_grow(n);
}
}
d->end = e + n;
@ -191,7 +203,7 @@ void **QListData::prepend()
Q_ASSERT(!d->ref.isShared());
if (d->begin == 0) {
if (d->end >= d->alloc / 3)
realloc(grow(d->alloc + 1));
realloc_grow(1);
if (d->end < d->alloc / 3)
d->begin = d->alloc - 2 * d->end;
@ -218,7 +230,7 @@ void **QListData::insert(int i)
if (d->begin == 0) {
if (d->end == d->alloc) {
// If the array is full, we expand it and move some items rightward
realloc(grow(d->alloc + 1));
realloc_grow(1);
} else {
// If there is free space at the end of the array, we move some items rightward
}

View File

@ -90,6 +90,7 @@ struct Q_CORE_EXPORT QListData {
Data *detach(int alloc);
Data *detach_grow(int *i, int n);
void realloc(int alloc);
void realloc_grow(int growth);
inline void dispose() { dispose(d); }
static void dispose(Data *d);
static const Data shared_null;