From a63a506d543430c22ce23107e1bdedeb489161b2 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 9 Apr 2014 08:08:01 +0200 Subject: [PATCH] QSslContext: provide sharedFromConfiguration() It's a version of fromConfiguration() that returns the QSslContext instance in a shared instead of a naked pointer. Use it in QSslSocketBackend. The idea here, of course, is to use QSharedPointer::create(), which co-locates the refcount with the payload in a single memory allocation, instead of QSharedPointer(new T), which causes two allocations. Change-Id: Ia5396fb3b291f2912fca5cd97e0aa1e45e065e55 Reviewed-by: Thiago Macieira --- src/network/ssl/qsslcontext_openssl.cpp | 7 +++++++ src/network/ssl/qsslcontext_openssl_p.h | 3 +++ src/network/ssl/qsslsocket_openssl.cpp | 3 +-- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/network/ssl/qsslcontext_openssl.cpp b/src/network/ssl/qsslcontext_openssl.cpp index 05f2f8f769..b3786f989e 100644 --- a/src/network/ssl/qsslcontext_openssl.cpp +++ b/src/network/ssl/qsslcontext_openssl.cpp @@ -375,6 +375,13 @@ QSslContext* QSslContext::fromConfiguration(QSslSocket::SslMode mode, const QSsl return sslContext; } +QSharedPointer QSslContext::sharedFromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration, bool allowRootCertOnDemandLoading) +{ + QSharedPointer sslContext = QSharedPointer::create(); + initSslContext(sslContext.data(), mode, configuration, allowRootCertOnDemandLoading); + return sslContext; +} + #if OPENSSL_VERSION_NUMBER >= 0x1000100fL && !defined(OPENSSL_NO_NEXTPROTONEG) static int next_proto_cb(SSL *, unsigned char **out, unsigned char *outlen, diff --git a/src/network/ssl/qsslcontext_openssl_p.h b/src/network/ssl/qsslcontext_openssl_p.h index 41800d01f9..ef4dc6f815 100644 --- a/src/network/ssl/qsslcontext_openssl_p.h +++ b/src/network/ssl/qsslcontext_openssl_p.h @@ -72,6 +72,8 @@ public: static QSslContext* fromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration, bool allowRootCertOnDemandLoading); + static QSharedPointer sharedFromConfiguration(QSslSocket::SslMode mode, const QSslConfiguration &configuration, + bool allowRootCertOnDemandLoading); QSslError::SslError error() const; QString errorString() const; @@ -99,6 +101,7 @@ public: protected: QSslContext(); + friend class QSharedPointer; private: static void initSslContext(QSslContext* sslContext, QSslSocket::SslMode mode, const QSslConfiguration &configuration, diff --git a/src/network/ssl/qsslsocket_openssl.cpp b/src/network/ssl/qsslsocket_openssl.cpp index 57082e247b..40ccb209fa 100644 --- a/src/network/ssl/qsslsocket_openssl.cpp +++ b/src/network/ssl/qsslsocket_openssl.cpp @@ -360,8 +360,7 @@ bool QSslSocketBackendPrivate::initSslContext() // create a deep copy of our configuration QSslConfigurationPrivate *configurationCopy = new QSslConfigurationPrivate(configuration); configurationCopy->ref.store(0); // the QSslConfiguration constructor refs up - sslContextPointer = QSharedPointer( - QSslContext::fromConfiguration(mode, configurationCopy, allowRootCertOnDemandLoading)); + sslContextPointer = QSslContext::sharedFromConfiguration(mode, configurationCopy, allowRootCertOnDemandLoading); } if (sslContextPointer->error() != QSslError::NoError) {