Core: Make enough room in an Id to store a pointer directly.

This switches internal storage from an (signed) int to an quintptr,
making it possible for client code to use Ids directly based on
pointers using Id::fromUniqueIdentifier().

Ids generated by existing code will keep using the low 32 bits only
for now, so the change effectively only affects 64 bit systems.

The patch also moves the rarely used fromUniqueIdentifier code
path out-of-line and replaces the Id::IdsPerPlugin and
Id::ReservedPlugins enums in the only place where they have been
used (setting a lower limit for ids). The limit itself not really
useful, but unchanged by this patch.

Change-Id: Ieabc7d9d72f3c5dd749fb49b7193a0c8629c186e
Reviewed-by: Eike Ziller <eike.ziller@theqtcompany.com>
This commit is contained in:
hjk
2015-06-19 14:01:31 +02:00
parent 5a4e6e17e1
commit caf3623ef2
2 changed files with 18 additions and 35 deletions

View File

@@ -53,16 +53,9 @@ namespace Core {
in a more typesafe and faster manner than a plain \c QString or
\c QByteArray would provide.
An id is internally represented as a 32 bit integer (its \c UID)
and associated with a plain 7-bit-clean ASCII name used
An id is associated with a plain 7-bit-clean ASCII name used
for display and persistency.
Each plugin that is distributed as part of \QC has a
private range of 10000 UIDs that are guaranteed to be unique.
Third party plugins are advised to construct ids from their
string representation.
*/
class StringHolder
@@ -85,8 +78,8 @@ public:
}
}
int n;
uint h;
const char *str;
quintptr h;
};
static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
@@ -98,10 +91,10 @@ static bool operator==(const StringHolder &sh1, const StringHolder &sh2)
static uint qHash(const StringHolder &sh)
{
return sh.h;
return QT_PREPEND_NAMESPACE(qHash)(sh.h, 0);
}
struct IdCache : public QHash<StringHolder, int>
struct IdCache : public QHash<StringHolder, quintptr>
{
#ifndef QTC_ALLOW_STATIC_LEAKS
~IdCache()
@@ -113,13 +106,12 @@ struct IdCache : public QHash<StringHolder, int>
};
static int firstUnusedId = Id::IdsPerPlugin * Id::ReservedPlugins;
static QHash<int, StringHolder> stringFromId;
static QHash<quintptr, StringHolder> stringFromId;
static IdCache idFromString;
static int theId(const char *str, int n = 0)
static quintptr theId(const char *str, int n = 0)
{
static quintptr firstUnusedId = 10 * 1000 * 1000;
QTC_ASSERT(str && *str, return 0);
StringHolder sh(str, n);
int res = idFromString.value(sh, 0);
@@ -132,26 +124,20 @@ static int theId(const char *str, int n = 0)
return res;
}
static int theId(const QByteArray &ba)
static quintptr theId(const QByteArray &ba)
{
return theId(ba.constData(), ba.size());
}
/*!
\fn Core::Id::Id(int uid)
\fn Core::Id::Id(quintptr uid)
\internal
Constructs an id given \a UID.
The UID is an integer value that is unique within the running
\QC process.
It is the callers responsibility to ensure the uniqueness of
the passed integer. The recommended approach is to use
\c{registerId()} with an value taken from the plugin's
private range.
\sa registerId()
*/
/*!
@@ -333,7 +319,7 @@ bool Id::operator==(const char *name) const
}
// For debugging purposes
CORE_EXPORT const char *nameForId(int id)
CORE_EXPORT const char *nameForId(quintptr id)
{
return stringFromId.value(id).str;
}