diff options
author | hjk <qtc-committer@nokia.com> | 2012-01-31 13:14:14 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@nokia.com> | 2012-02-01 14:08:10 +0100 |
commit | 658a608083e8e0f733c8185efae12597cb1840c1 (patch) | |
tree | cfe01d34f26a0592edb290d2afc564f1cdba7b0c /src/plugins/coreplugin/id.cpp | |
parent | fe8cead2d004e5a571bd8a65f91757db2c34a12c (diff) | |
download | qt-creator-658a608083e8e0f733c8185efae12597cb1840c1.tar.gz |
core: prevent allocations when looking up an existing Core::Id
Change-Id: I7bd2f83ffc6a57135aab9f76ff929eca48d16885
Reviewed-by: Eike Ziller <eike.ziller@nokia.com>
Diffstat (limited to 'src/plugins/coreplugin/id.cpp')
-rw-r--r-- | src/plugins/coreplugin/id.cpp | 58 |
1 files changed, 52 insertions, 6 deletions
diff --git a/src/plugins/coreplugin/id.cpp b/src/plugins/coreplugin/id.cpp index 6abcc5642a..a03b4eb9d4 100644 --- a/src/plugins/coreplugin/id.cpp +++ b/src/plugins/coreplugin/id.cpp @@ -49,20 +49,66 @@ namespace Core { */ +class StringHolder +{ +public: + explicit StringHolder(const char *s) + : str(s) + { + n = strlen(s); + int m = n; + h = 0; + while (m--) { + h = (h << 4) + *s++; + h ^= (h & 0xf0000000) >> 23; + h &= 0x0fffffff; + } + } + int n; + const char *str; + uint h; +}; + +static bool operator==(const StringHolder &sh1, const StringHolder &sh2) +{ + // sh.n is unlikely to discriminate better than the hash. + return sh1.h == sh2.h && strcmp(sh1.str, sh1.str) == 0; +} + + +static uint qHash(const StringHolder &sh) +{ + return sh.h; +} + +struct IdCache : public QHash<StringHolder, int> +{ +#ifndef QTC_ALLOW_STATIC_LEAKS + ~IdCache() + { + for (IdCache::iterator it = begin(); it != end(); ++it) + free(const_cast<char *>(it.key().str)); + } +#endif +}; + + static int lastUid = 0; static QVector<QByteArray> stringFromId; -static QHash<QByteArray, int> idFromString; +static IdCache idFromString; -static int theId(const QByteArray &ba) +static int theId(const char *str) { - QTC_CHECK(!ba.isEmpty()); - int res = idFromString.value(ba); + QTC_ASSERT(str && *str, return 0); + StringHolder sh(str); + int res = idFromString.value(sh, 0); if (res == 0) { if (lastUid == 0) stringFromId.append(QByteArray()); res = ++lastUid; - idFromString[ba] = res; - stringFromId.append(ba); + sh.str = strdup(sh.str); + idFromString[sh] = res; + stringFromId.append(QByteArray::fromRawData(sh.str, sh.n)); } return res; } |