QResource: consistently cache resourceList()

Each call to reourceList() uses atomic operations to check whether
resourceGLobalData has not expired, yet.

Some functions already cached the value, but then went on to use the
function directly afterwards. In some cases, this was due to the
cached value being a pointer-to-const and the function later deciding
to mutate the list. But all the code is safe from detaches, so this
distinction need not be made.

Standardize on caching, and using the cached value, except in
functions which call it only once.

Change-Id: I79780b990da539bf7beaa8104e13cb8187f84812
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2019-07-23 08:38:47 +03:00
parent 231b70c294
commit 6e8dd46a6f

View File

@ -951,11 +951,12 @@ Q_CORE_EXPORT bool qRegisterResourceData(int version, const unsigned char *tree,
if (resourceGlobalData.isDestroyed())
return false;
QMutexLocker lock(resourceMutex());
ResourceList *list = resourceList();
if (version >= 0x01 && version <= 0x3) {
bool found = false;
QResourceRoot res(version, tree, name, data);
for(int i = 0; i < resourceList()->size(); ++i) {
if(*resourceList()->at(i) == res) {
for (int i = 0; i < list->size(); ++i) {
if (*list->at(i) == res) {
found = true;
break;
}
@ -963,7 +964,7 @@ Q_CORE_EXPORT bool qRegisterResourceData(int version, const unsigned char *tree,
if(!found) {
QResourceRoot *root = new QResourceRoot(version, tree, name, data);
root->ref.ref();
resourceList()->append(root);
list->append(root);
}
return true;
}
@ -979,9 +980,10 @@ Q_CORE_EXPORT bool qUnregisterResourceData(int version, const unsigned char *tre
QMutexLocker lock(resourceMutex());
if (version >= 0x01 && version <= 0x3) {
QResourceRoot res(version, tree, name, data);
for(int i = 0; i < resourceList()->size(); ) {
if(*resourceList()->at(i) == res) {
QResourceRoot *root = resourceList()->takeAt(i);
ResourceList *list = resourceList();
for (int i = 0; i < list->size(); ) {
if (*list->at(i) == res) {
QResourceRoot *root = list->takeAt(i);
if(!root->ref.deref())
delete root;
} else {
@ -1227,7 +1229,7 @@ QResource::unregisterResource(const QString &rccFilename, const QString &resourc
if(res->type() == QResourceRoot::Resource_File) {
QDynamicFileResourceRoot *root = reinterpret_cast<QDynamicFileResourceRoot*>(res);
if (root->mappingFile() == rccFilename && root->mappingRoot() == r) {
resourceList()->removeAt(i);
list->removeAt(i);
if(!root->ref.deref()) {
delete root;
return true;
@ -1298,7 +1300,7 @@ QResource::unregisterResource(const uchar *rccData, const QString &resourceRoot)
if(res->type() == QResourceRoot::Resource_Buffer) {
QDynamicBufferResourceRoot *root = reinterpret_cast<QDynamicBufferResourceRoot*>(res);
if (root->mappingBuffer() == rccData && root->mappingRoot() == r) {
resourceList()->removeAt(i);
list->removeAt(i);
if(!root->ref.deref()) {
delete root;
return true;