QLibrary: Use QTaggedPointer for the did_load flag

This makes the size nicely aligned and conveniently intializes
everything to the right values.

Change-Id: Ibad2defbbd323fd5cdd4bed8374f40558d80acc0
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Ulf Hermann 2020-07-14 14:16:22 +02:00
parent eda30d1986
commit 8d4eb292b2
2 changed files with 19 additions and 19 deletions

View File

@ -825,9 +825,10 @@ bool QLibrary::load()
{ {
if (!d) if (!d)
return false; return false;
if (did_load) if (d.tag() == Loaded)
return d->pHnd.loadRelaxed(); return d->pHnd.loadRelaxed();
did_load = true; else
d.setTag(Loaded);
return d->load(); return d->load();
} }
@ -848,8 +849,8 @@ bool QLibrary::load()
*/ */
bool QLibrary::unload() bool QLibrary::unload()
{ {
if (did_load) { if (d.tag() == Loaded) {
did_load = false; d.setTag(NotLoaded);
return d->unload(); return d->unload();
} }
return false; return false;
@ -869,8 +870,7 @@ bool QLibrary::isLoaded() const
/*! /*!
Constructs a library with the given \a parent. Constructs a library with the given \a parent.
*/ */
QLibrary::QLibrary(QObject *parent) QLibrary::QLibrary(QObject *parent) : QObject(parent)
:QObject(parent), d(nullptr), did_load(false)
{ {
} }
@ -884,8 +884,7 @@ QLibrary::QLibrary(QObject *parent)
suffix in accordance with the platform, e.g. ".so" on Unix, suffix in accordance with the platform, e.g. ".so" on Unix,
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/ */
QLibrary::QLibrary(const QString& fileName, QObject *parent) QLibrary::QLibrary(const QString& fileName, QObject *parent) : QObject(parent)
:QObject(parent), d(nullptr), did_load(false)
{ {
setFileName(fileName); setFileName(fileName);
} }
@ -901,8 +900,7 @@ QLibrary::QLibrary(const QString& fileName, QObject *parent)
suffix in accordance with the platform, e.g. ".so" on Unix, suffix in accordance with the platform, e.g. ".so" on Unix,
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/ */
QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent) QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent) : QObject(parent)
:QObject(parent), d(nullptr), did_load(false)
{ {
setFileNameAndVersion(fileName, verNum); setFileNameAndVersion(fileName, verNum);
} }
@ -918,7 +916,7 @@ QLibrary::QLibrary(const QString& fileName, int verNum, QObject *parent)
".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.) ".dylib" on \macos and iOS, and ".dll" on Windows. (See \l{fileName}.)
*/ */
QLibrary::QLibrary(const QString& fileName, const QString &version, QObject *parent) QLibrary::QLibrary(const QString& fileName, const QString &version, QObject *parent)
:QObject(parent), d(nullptr), did_load(false) : QObject(parent)
{ {
setFileNameAndVersion(fileName, version); setFileNameAndVersion(fileName, version);
} }
@ -965,8 +963,7 @@ void QLibrary::setFileName(const QString &fileName)
if (d) { if (d) {
lh = d->loadHints(); lh = d->loadHints();
d->release(); d->release();
d = nullptr; d = {};
did_load = false;
} }
d = QLibraryPrivate::findOrCreate(fileName, QString(), lh); d = QLibraryPrivate::findOrCreate(fileName, QString(), lh);
} }
@ -995,8 +992,7 @@ void QLibrary::setFileNameAndVersion(const QString &fileName, int verNum)
if (d) { if (d) {
lh = d->loadHints(); lh = d->loadHints();
d->release(); d->release();
d = nullptr; d = {};
did_load = false;
} }
d = QLibraryPrivate::findOrCreate(fileName, verNum >= 0 ? QString::number(verNum) : QString(), lh); d = QLibraryPrivate::findOrCreate(fileName, verNum >= 0 ? QString::number(verNum) : QString(), lh);
} }
@ -1016,8 +1012,7 @@ void QLibrary::setFileNameAndVersion(const QString &fileName, const QString &ver
if (d) { if (d) {
lh = d->loadHints(); lh = d->loadHints();
d->release(); d->release();
d = nullptr; d = {};
did_load = false;
} }
d = QLibraryPrivate::findOrCreate(fileName, version, lh); d = QLibraryPrivate::findOrCreate(fileName, version, lh);
} }

View File

@ -41,6 +41,7 @@
#define QLIBRARY_H #define QLIBRARY_H
#include <QtCore/qobject.h> #include <QtCore/qobject.h>
#include <QtCore/qtaggedpointer.h>
QT_REQUIRE_CONFIG(library); QT_REQUIRE_CONFIG(library);
@ -92,8 +93,12 @@ public:
void setLoadHints(LoadHints hints); void setLoadHints(LoadHints hints);
LoadHints loadHints() const; LoadHints loadHints() const;
private: private:
QLibraryPrivate *d; enum LoadStatusTag {
bool did_load; NotLoaded,
Loaded
};
QTaggedPointer<QLibraryPrivate, LoadStatusTag> d = nullptr;
Q_DISABLE_COPY(QLibrary) Q_DISABLE_COPY(QLibrary)
}; };