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>
This commit is contained in:
Marc Mutz 2017-02-14 01:26:25 +01:00
parent a5febadac5
commit 04d6495bf7
9 changed files with 48 additions and 21 deletions

View File

@ -968,10 +968,10 @@ QUuid QUuid::createUuid()
if (!uuidseed.hasLocalData()) if (!uuidseed.hasLocalData())
{ {
int *pseed = new int; int *pseed = new int;
static QBasicAtomicInt serial = Q_BASIC_ATOMIC_INITIALIZER(2); static QBasicAtomicInt serial;
qsrand(*pseed = QDateTime::currentSecsSinceEpoch() qsrand(*pseed = QDateTime::currentSecsSinceEpoch()
+ quintptr(&pseed) + quintptr(&pseed)
+ serial.fetchAndAddRelaxed(1)); + 2 + serial.fetchAndAddRelaxed(1));
uuidseed.setLocalData(pseed); uuidseed.setLocalData(pseed);
} }
#else #else

View File

@ -97,7 +97,11 @@ QT_BEGIN_NAMESPACE
\value On Display the pixmap when the widget is in an "on" state \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(); static void qt_cleanup_icon_cache();
namespace { namespace {
@ -139,7 +143,7 @@ static qreal qt_effective_device_pixel_ratio(QWindow *window = 0)
QIconPrivate::QIconPrivate(QIconEngine *e) QIconPrivate::QIconPrivate(QIconEngine *e)
: engine(e), ref(1), : engine(e), ref(1),
serialNum(serialNumCounter.fetchAndAddRelaxed(1)), serialNum(nextSerialNumCounter()),
detach_no(0), detach_no(0),
is_mask(false) is_mask(false)
{ {

View File

@ -89,12 +89,16 @@ static QImage rotated90(const QImage &src);
static QImage rotated180(const QImage &src); static QImage rotated180(const QImage &src);
static QImage rotated270(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() QImageData::QImageData()
: ref(0), width(0), height(0), depth(0), nbytes(0), devicePixelRatio(1.0), data(0), : ref(0), width(0), height(0), depth(0), nbytes(0), devicePixelRatio(1.0), data(0),
format(QImage::Format_ARGB32), bytes_per_line(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), detach_no(0),
dpmx(qt_defaultDpiX() * 100 / qreal(2.54)), dpmx(qt_defaultDpiX() * 100 / qreal(2.54)),
dpmy(qt_defaultDpiY() * 100 / qreal(2.54)), dpmy(qt_defaultDpiY() * 100 / qreal(2.54)),

View File

@ -47,7 +47,11 @@
QT_BEGIN_NAMESPACE 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) QOpenGLTextureGlyphCache::QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix)
: QImageTextureGlyphCache(format, matrix) : QImageTextureGlyphCache(format, matrix)
@ -55,7 +59,7 @@ QOpenGLTextureGlyphCache::QOpenGLTextureGlyphCache(QFontEngine::GlyphFormat form
, pex(0) , pex(0)
, m_blitProgram(0) , m_blitProgram(0)
, m_filterMode(Nearest) , m_filterMode(Nearest)
, m_serialNumber(qopengltextureglyphcache_serial_number.fetchAndAddRelaxed(1)) , m_serialNumber(next_qopengltextureglyphcache_serial_number())
, m_buffer(QOpenGLBuffer::VertexBuffer) , m_buffer(QOpenGLBuffer::VertexBuffer)
{ {
#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG

View File

@ -246,22 +246,25 @@ public:
} data; } data;
bool is_ba; 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) QFtpCommand::QFtpCommand(QFtp::Command cmd, const QStringList &raw, const QByteArray &ba)
: command(cmd), rawCmds(raw), is_ba(true) : command(cmd), rawCmds(raw), is_ba(true)
{ {
id = idCounter.fetchAndAddRelaxed(1); id = nextId();
data.ba = new QByteArray(ba); data.ba = new QByteArray(ba);
} }
QFtpCommand::QFtpCommand(QFtp::Command cmd, const QStringList &raw, QIODevice *dev) QFtpCommand::QFtpCommand(QFtp::Command cmd, const QStringList &raw, QIODevice *dev)
: command(cmd), rawCmds(raw), is_ba(false) : command(cmd), rawCmds(raw), is_ba(false)
{ {
id = idCounter.fetchAndAddRelaxed(1); id = nextId();
data.dev = dev; data.dev = dev;
} }

View File

@ -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} \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 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>(); qRegisterMetaType<QHostInfo>();
int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID int id = nextId(); // generate unique ID
if (name.isEmpty()) { if (name.isEmpty()) {
if (!receiver) if (!receiver)
@ -596,7 +600,7 @@ int QHostInfo::lookupHostImpl(const QString &name,
qRegisterMetaType<QHostInfo>(); qRegisterMetaType<QHostInfo>();
int id = theIdCounter.fetchAndAddRelaxed(1); // generate unique ID int id = nextId(); // generate unique ID
if (Q_UNLIKELY(name.isEmpty())) { if (Q_UNLIKELY(name.isEmpty())) {
QHostInfo hostInfo(id); QHostInfo hostInfo(id);

View File

@ -1002,13 +1002,17 @@ QSocks5SocketEngine::~QSocks5SocketEngine()
delete d->bindData; 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) bool QSocks5SocketEngine::initialize(QAbstractSocket::SocketType type, QAbstractSocket::NetworkLayerProtocol protocol)
{ {
Q_D(QSocks5SocketEngine); Q_D(QSocks5SocketEngine);
d->socketDescriptor = descriptorCounter.fetchAndAddRelaxed(1); d->socketDescriptor = nextDescriptor();
d->socketType = type; d->socketType = type;
d->socketProtocol = protocol; d->socketProtocol = protocol;

View File

@ -45,7 +45,11 @@
QT_BEGIN_NAMESPACE 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) QGLTextureGlyphCache::QGLTextureGlyphCache(QFontEngine::GlyphFormat format, const QTransform &matrix)
: QImageTextureGlyphCache(format, matrix) : QImageTextureGlyphCache(format, matrix)
@ -53,7 +57,7 @@ QGLTextureGlyphCache::QGLTextureGlyphCache(QFontEngine::GlyphFormat format, cons
, pex(0) , pex(0)
, m_blitProgram(0) , m_blitProgram(0)
, m_filterMode(Nearest) , m_filterMode(Nearest)
, m_serialNumber(qgltextureglyphcache_serial_number.fetchAndAddRelaxed(1)) , m_serialNumber(next_qgltextureglyphcache_serial_number())
{ {
#ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG #ifdef QT_GL_TEXTURE_GLYPH_CACHE_DEBUG
qDebug(" -> QGLTextureGlyphCache() %p for context %p.", this, QOpenGLContext::currentContext()); qDebug(" -> QGLTextureGlyphCache() %p for context %p.", this, QOpenGLContext::currentContext());

View File

@ -122,13 +122,13 @@ EGLNativeWindowType QEglFSEmulatorIntegration::createNativeWindow(QPlatformWindo
{ {
Q_UNUSED(size); Q_UNUSED(size);
Q_UNUSED(format); Q_UNUSED(format);
static QAtomicInt uniqueWindowId(1);
QEglFSEmulatorScreen *screen = static_cast<QEglFSEmulatorScreen *>(platformWindow->screen()); QEglFSEmulatorScreen *screen = static_cast<QEglFSEmulatorScreen *>(platformWindow->screen());
if (screen && setDisplay) { if (screen && setDisplay) {
// Let the emulator know which screen the window surface is attached to // Let the emulator know which screen the window surface is attached to
setDisplay(screen->id()); setDisplay(screen->id());
} }
return EGLNativeWindowType(qintptr(uniqueWindowId.fetchAndAddRelaxed(1))); static QBasicAtomicInt uniqueWindowId;
return EGLNativeWindowType(qintptr(1 + uniqueWindowId.fetchAndAddRelaxed(1)));
} }
QT_END_NAMESPACE QT_END_NAMESPACE