From 04d6495bf773a6bb0d4fa6980df22d3b81a605b0 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 14 Feb 2017 01:26:25 +0100 Subject: 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 --- src/corelib/plugin/quuid.cpp | 4 ++-- src/gui/image/qicon.cpp | 8 ++++++-- src/gui/image/qimage.cpp | 8 ++++++-- src/gui/opengl/qopengltextureglyphcache.cpp | 8 ++++++-- src/network/access/qftp.cpp | 11 +++++++---- src/network/kernel/qhostinfo.cpp | 10 +++++++--- src/network/socket/qsocks5socketengine.cpp | 8 ++++++-- src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp | 8 ++++++-- .../deviceintegration/eglfs_emu/qeglfsemulatorintegration.cpp | 4 ++-- 9 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/corelib/plugin/quuid.cpp b/src/corelib/plugin/quuid.cpp index f11ac6548b..1a7073a069 100644 --- a/src/corelib/plugin/quuid.cpp +++ b/src/corelib/plugin/quuid.cpp @@ -968,10 +968,10 @@ QUuid QUuid::createUuid() if (!uuidseed.hasLocalData()) { int *pseed = new int; - static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); + static QBasicAtomicInt serial; qsrand(*pseed = QDateTime::currentSecsSinceEpoch() + quintptr(&pseed) - + serial.fetchAndAddRelaxed(1)); + + 2 + serial.fetchAndAddRelaxed(1)); uuidseed.setLocalData(pseed); } #else diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 17734f05f3..0c3323636a 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -97,7 +97,11 @@ QT_BEGIN_NAMESPACE \value On Display the pixmap when the widget is in an "on" state */ -static QBasicAtomicInt serialNumCounter = Q_BASIC_ATOMIC_INITIALIZER(1); +static int nextSerialNumCounter() +{ + static QBasicAtomicInt serial; + return 1 + serial.fetchAndAddRelaxed(1); +} static void qt_cleanup_icon_cache(); namespace { @@ -139,7 +143,7 @@ static qreal qt_effective_device_pixel_ratio(QWindow *window = 0) QIconPrivate::QIconPrivate(QIconEngine *e) : engine(e), ref(1), - serialNum(serialNumCounter.fetchAndAddRelaxed(1)), + serialNum(nextSerialNumCounter()), detach_no(0), is_mask(false) { diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index 9870e19cec..75229ada6c 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -89,12 +89,16 @@ static QImage rotated90(const QImage &src); static QImage rotated180(const QImage &src); static QImage rotated270(const QImage &src); -QBasicAtomicInt qimage_serial_number = Q_BASIC_ATOMIC_INITIALIZER(1); +static int next_qimage_serial_number() +{ + static QBasicAtomicInt serial; + return 1 + serial.fetchAndAddRelaxed(1); +} QImageData::QImageData() : ref(0), width(0), height(0), depth(0), nbytes(0), devicePixelRatio(1.0), data(0), format(QImage::Format_ARGB32), bytes_per_line(0), - ser_no(qimage_serial_number.fetchAndAddRelaxed(1)), + ser_no(next_qimage_serial_number()), detach_no(0), dpmx(qt_defaultDpiX() * 100 / qreal(2.54)), dpmy(qt_defaultDpiY() * 100 / qreal(2.54)), diff --git a/src/gui/opengl/qopengltextureglyphcache.cpp b/src/gui/opengl/qopengltextureglyphcache.cpp index 62b069a1d0..9a4527b4e6 100644 --- a/src/gui/opengl/qopengltextureglyphcache.cpp +++ b/src/gui/opengl/qopengltextureglyphcache.cpp @@ -47,7 +47,11 @@ QT_BEGIN_NAMESPACE -QBasicAtomicInt qopengltextureglyphcache_serial_number = Q_BASIC_ATOMIC_INITIALIZER(1); +static int next_qopengltextureglyphcache_serial_number() +{ + static QBasicAtomicInt serial; + return 1 + serial.fetchAndAddRelaxed(1); +} QOpenGLTextureGlyphCache::QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix) : QImageTextureGlyphCache(format, matrix) @@ -55,7 +59,7 @@ QOpenGLTextureGlyphCache::QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat form , pex(0) , m_blitProgram(0) , m_filterMode(Nearest) - , m_serialNumber(qopengltextureglyphcache_serial_number.fetchAndAddRelaxed(1)) + , m_serialNumber(next_qopengltextureglyphcache_serial_number()) , m_buffer(QOpenGLBuffer::VertexBuffer) { #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG 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(); - 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(); - 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; diff --git a/src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp b/src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp index 047b681bcb..c1c5661da4 100644 --- a/src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp +++ b/src/opengl/gl2paintengineex/qtextureglyphcache_gl.cpp @@ -45,7 +45,11 @@ QT_BEGIN_NAMESPACE -QBasicAtomicInt qgltextureglyphcache_serial_number = Q_BASIC_ATOMIC_INITIALIZER(1); +static int next_qgltextureglyphcache_serial_number() +{ + static QBasicAtomicInt serial; + return 1 + serial.fetchAndAddRelaxed(1); +} QGLTextureGlyphCache::QGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix) : QImageTextureGlyphCache(format, matrix) @@ -53,7 +57,7 @@ QGLTextureGlyphCache::QGLTextureGlyphCache(QFontEngine::GlyphFormat format, cons , pex(0) , m_blitProgram(0) , m_filterMode(Nearest) - , m_serialNumber(qgltextureglyphcache_serial_number.fetchAndAddRelaxed(1)) + , m_serialNumber(next_qgltextureglyphcache_serial_number()) { #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG qDebug(" -> QGLTextureGlyphCache() %p for context %p.", this, QOpenGLContext::currentContext()); diff --git a/src/plugins/platforms/eglfs/deviceintegration/eglfs_emu/qeglfsemulatorintegration.cpp b/src/plugins/platforms/eglfs/deviceintegration/eglfs_emu/qeglfsemulatorintegration.cpp index 8a059ba973..d0b4b5d5bd 100644 --- a/src/plugins/platforms/eglfs/deviceintegration/eglfs_emu/qeglfsemulatorintegration.cpp +++ b/src/plugins/platforms/eglfs/deviceintegration/eglfs_emu/qeglfsemulatorintegration.cpp @@ -122,13 +122,13 @@ EGLNativeWindowType QEglFSEmulatorIntegration::createNativeWindow(QPlatformWindo { Q_UNUSED(size); Q_UNUSED(format); - static QAtomicInt uniqueWindowId(1); QEglFSEmulatorScreen *screen = static_cast(platformWindow->screen()); if (screen && setDisplay) { // Let the emulator know which screen the window surface is attached to setDisplay(screen->id()); } - return EGLNativeWindowType(qintptr(uniqueWindowId.fetchAndAddRelaxed(1))); + static QBasicAtomicInt uniqueWindowId; + return EGLNativeWindowType(qintptr(1 + uniqueWindowId.fetchAndAddRelaxed(1))); } QT_END_NAMESPACE -- cgit v1.2.3