summaryrefslogtreecommitdiffstats
path: root/src/network
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2017-02-14 01:26:25 +0100
committerMarc Mutz <marc.mutz@kdab.com>2017-02-15 20:14:27 +0000
commit04d6495bf773a6bb0d4fa6980df22d3b81a605b0 (patch)
tree3372ff44c12f502402f80704c058082d1d27257b /src/network
parenta5febadac55741b4b48f25463a310835cc8bde19 (diff)
Make some atomic counters zero-based
A variable of static storage duration that is not zero-initialized takes up space in the DATA segment of the executable. By making the counters start at zero and adding the initial value afterwards, we move them over to the BSS segment, which does not take up space in the executable. Wrap atomics used across function boundaries into small functions, to avoid code duplication and to increase readability. Change-Id: Ida6ed316ecb8fe20da62a9577161349e14de5aed Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/network')
-rw-r--r--src/network/access/qftp.cpp11
-rw-r--r--src/network/kernel/qhostinfo.cpp10
-rw-r--r--src/network/socket/qsocks5socketengine.cpp8
3 files changed, 20 insertions, 9 deletions
diff --git a/src/network/access/qftp.cpp b/src/network/access/qftp.cpp
index 52b9dd9169..18c5a16cb5 100644
--- a/src/network/access/qftp.cpp
+++ b/src/network/access/qftp.cpp
@@ -246,22 +246,25 @@ public:
} data;
bool is_ba;
- static QBasicAtomicInt idCounter;
};
-QBasicAtomicInt QFtpCommand::idCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
+static int nextId()
+{
+ static QBasicAtomicInt counter;
+ return 1 + counter.fetchAndAddRelaxed(1);
+}
QFtpCommand::QFtpCommand(QFtp::Command cmd, const QStringList &raw, const QByteArray &ba)
: command(cmd), rawCmds(raw), is_ba(true)
{
- id = idCounter.fetchAndAddRelaxed(1);
+ id = nextId();
data.ba = new QByteArray(ba);
}
QFtpCommand::QFtpCommand(QFtp::Command cmd, const QStringList &raw, QIODevice *dev)
: command(cmd), rawCmds(raw), is_ba(false)
{
- id = idCounter.fetchAndAddRelaxed(1);
+ id = nextId();
data.dev = dev;
}
diff --git a/src/network/kernel/qhostinfo.cpp b/src/network/kernel/qhostinfo.cpp
index 193c990d8c..d3a1858f86 100644
--- a/src/network/kernel/qhostinfo.cpp
+++ b/src/network/kernel/qhostinfo.cpp
@@ -183,7 +183,11 @@ void emit_results_ready(const QHostInfo &hostInfo, const QObject *receiver,
\sa QAbstractSocket, {http://www.rfc-editor.org/rfc/rfc3492.txt}{RFC 3492}
*/
-static QBasicAtomicInt theIdCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
+static int nextId()
+{
+ static QBasicAtomicInt counter;
+ return 1 + counter.fetchAndAddRelaxed(1);
+}
/*!
Looks up the IP address(es) associated with host name \a name, and
@@ -229,7 +233,7 @@ int QHostInfo::lookupHost(const QString &name, QObject *receiver,
qRegisterMetaType<QHostInfo>();
- int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID
+ int id = nextId(); // generate unique ID
if (name.isEmpty()) {
if (!receiver)
@@ -596,7 +600,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
qRegisterMetaType<QHostInfo>();
- int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID
+ int id = nextId(); // generate unique ID
if (Q_UNLIKELY(name.isEmpty())) {
QHostInfo hostInfo(id);
diff --git a/src/network/socket/qsocks5socketengine.cpp b/src/network/socket/qsocks5socketengine.cpp
index 95a0d0563a..2847b910f3 100644
--- a/src/network/socket/qsocks5socketengine.cpp
+++ b/src/network/socket/qsocks5socketengine.cpp
@@ -1002,13 +1002,17 @@ QSocks5SocketEngine::~QSocks5SocketEngine()
delete d->bindData;
}
-static QBasicAtomicInt descriptorCounter = Q_BASIC_ATOMIC_INITIALIZER(1);
+static int nextDescriptor()
+{
+ static QBasicAtomicInt counter;
+ return 1 + counter.fetchAndAddRelaxed(1);
+}
bool QSocks5SocketEngine::initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol)
{
Q_D(QSocks5SocketEngine);
- d->socketDescriptor = descriptorCounter.fetchAndAddRelaxed(1);
+ d->socketDescriptor = nextDescriptor();
d->socketType = type;
d->socketProtocol = protocol;