summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qmap.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2009-10-22 19:09:24 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2009-10-28 14:27:40 (GMT)
commite83bb2fdfc2dc899526c8157fd8b77a68cdde9da (patch)
tree50a23cf12df52705f254156072269ec4cce04a21 /src/corelib/tools/qmap.cpp
parent3f0b969772cf3056ed54349bfe6837d9de2995ea (diff)
downloadQt-e83bb2fdfc2dc899526c8157fd8b77a68cdde9da.zip
Qt-e83bb2fdfc2dc899526c8157fd8b77a68cdde9da.tar.gz
Qt-e83bb2fdfc2dc899526c8157fd8b77a68cdde9da.tar.bz2
Fix Qt containers to properly support types with strict alignments.
QContiguousCache is a new type, so there are no binary compatibility issues. QHash and QMap didn't have any public qMalloc / qFree, so the entire logic is contained in .cpp code. However, since old code will not inform us of the alignment requirements, we need to add a bit to the structure to indicate whether strict alignment is in use or not. QList doesn't require any changes. For small, movable types, they're all stored in the pointer array itself, so they're aligned. For larger types, we use new(), so types with stricter requirements should define their own operator new(). QLinkedList cannot be fixed. It uses new() on the QLinkedListNode, which contains a T type. Sorry. QVector did have public qMalloc / qFree. I've moved the calls to the inner function and made it keep the old calls if the alignment requirement is below a certain threshold. The idea is that, if it's above, no one was using QVector anyway. Reviewed-by: Bradley T. Hughes
Diffstat (limited to 'src/corelib/tools/qmap.cpp')
-rw-r--r--src/corelib/tools/qmap.cpp31
1 files changed, 26 insertions, 5 deletions
diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp
index 1385810..cfb18b4 100644
--- a/src/corelib/tools/qmap.cpp
+++ b/src/corelib/tools/qmap.cpp
@@ -53,11 +53,16 @@ QT_BEGIN_NAMESPACE
QMapData QMapData::shared_null = {
&shared_null,
{ &shared_null, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
- Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, 0, false, true
+ Q_BASIC_ATOMIC_INITIALIZER(1), 0, 0, 0, false, true, false
};
QMapData *QMapData::createData()
{
+ return createData(0);
+}
+
+QMapData *QMapData::createData(int alignment)
+{
QMapData *d = new QMapData;
Q_CHECK_PTR(d);
Node *e = reinterpret_cast<Node *>(d);
@@ -69,6 +74,7 @@ QMapData *QMapData::createData()
d->randomBits = 0;
d->insertInOrder = false;
d->sharable = true;
+ d->strictAlignment = alignment > 8;
return d;
}
@@ -80,11 +86,19 @@ void QMapData::continueFreeData(int offset)
while (cur != e) {
prev = cur;
cur = cur->forward[0];
- qFree(reinterpret_cast<char *>(prev) - offset);
+ if (strictAlignment)
+ qFreeAligned(reinterpret_cast<char *>(prev) - offset);
+ else
+ qFree(reinterpret_cast<char *>(prev) - offset);
}
delete this;
}
+QMapData::Node *QMapData::node_create(Node *update[], int offset)
+{
+ return node_create(update, offset, 0);
+}
+
/*!
Creates a new node inside the data structure.
@@ -94,10 +108,12 @@ void QMapData::continueFreeData(int offset)
\a offset is an amount of bytes that needs to reserved just before the
QMapData::Node structure.
+ \a alignment dictates the alignment for the data.
+
\internal
\since 4.6
*/
-QMapData::Node *QMapData::node_create(Node *update[], int offset)
+QMapData::Node *QMapData::node_create(Node *update[], int offset, int alignment)
{
int level = 0;
uint mask = (1 << Sparseness) - 1;
@@ -118,7 +134,9 @@ QMapData::Node *QMapData::node_create(Node *update[], int offset)
if (level == 3 && !insertInOrder)
randomBits = qrand();
- void *concreteNode = qMalloc(offset + sizeof(Node) + level * sizeof(Node *));
+ void *concreteNode = strictAlignment ?
+ qMallocAligned(offset + sizeof(Node) + level * sizeof(Node *), alignment) :
+ qMalloc(offset + sizeof(Node) + level * sizeof(Node *));
Q_CHECK_PTR(concreteNode);
Node *abstractNode = reinterpret_cast<Node *>(reinterpret_cast<char *>(concreteNode) + offset);
@@ -145,7 +163,10 @@ void QMapData::node_delete(Node *update[], int offset, Node *node)
update[i]->forward[i] = node->forward[i];
}
--size;
- qFree(reinterpret_cast<char *>(node) - offset);
+ if (strictAlignment)
+ qFreeAligned(reinterpret_cast<char *>(node) - offset);
+ else
+ qFree(reinterpret_cast<char *>(node) - offset);
}
#ifdef QT_QMAP_DEBUG