From 46e3aa29081e0c043d745e590b9d3b9220f8d471 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 20 Jul 2012 20:49:13 +0200 Subject: Lazily initialize iconv in the iconv codec Avoid dlopen'ing libiconv and initializing it's members until the codec gets used for the first time. This avoids some memory and startup time overhead in case we can use the utf8 codec instead of iconv. It also removes a circular dependency between codec initialization during app startup. Change-Id: I119c010c288dc59ab32279d8a213ae1f4347cace Reviewed-by: Thiago Macieira --- src/corelib/codecs/qiconvcodec.cpp | 13 ++++++++++--- src/corelib/codecs/qiconvcodec_p.h | 3 ++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/corelib/codecs/qiconvcodec.cpp b/src/corelib/codecs/qiconvcodec.cpp index c7c1d365b6..e56f8885e7 100644 --- a/src/corelib/codecs/qiconvcodec.cpp +++ b/src/corelib/codecs/qiconvcodec.cpp @@ -92,6 +92,10 @@ QT_BEGIN_NAMESPACE QIconvCodec::QIconvCodec() : utf16Codec(0) +{ +} + +void QIconvCodec::init() const { utf16Codec = QTextCodec::codecForMib(1015); Q_ASSERT_X(utf16Codec != 0, @@ -203,7 +207,7 @@ QString QIconvCodec::convertToUnicode(const char* chars, int len, ConverterState if (!*pstate) { // first time, create the state - iconv_t cd = QIconvCodec::createIconv_t(UTF16, 0); + iconv_t cd = createIconv_t(UTF16, 0); if (cd == reinterpret_cast(-1)) { static int reported = 0; if (!reported++) { @@ -348,7 +352,7 @@ QByteArray QIconvCodec::convertFromUnicode(const QChar *uc, int len, ConverterSt QThreadStorage *ts = fromUnicodeState(); IconvState *&state = ts ? ts->localData() : temporaryState; if (!state) { - iconv_t cd = QIconvCodec::createIconv_t(0, UTF16); + iconv_t cd = createIconv_t(0, UTF16); if (cd != reinterpret_cast(-1)) { if (!setByteOrder(cd)) { perror("QIconvCodec::convertFromUnicode: using Latin-1 for conversion, iconv failed for BOM"); @@ -460,10 +464,13 @@ int QIconvCodec::mibEnum() const return 0; } -iconv_t QIconvCodec::createIconv_t(const char *to, const char *from) +iconv_t QIconvCodec::createIconv_t(const char *to, const char *from) const { Q_ASSERT((to == 0 && from != 0) || (to != 0 && from == 0)); + if (!utf16Codec) + init(); + iconv_t cd = (iconv_t) -1; #if defined(__GLIBC__) || defined(GNU_LIBICONV) || defined(Q_OS_QNX) #if defined(Q_OS_QNX) diff --git a/src/corelib/codecs/qiconvcodec_p.h b/src/corelib/codecs/qiconvcodec_p.h index efe3b4b5f7..4b8bc68603 100644 --- a/src/corelib/codecs/qiconvcodec_p.h +++ b/src/corelib/codecs/qiconvcodec_p.h @@ -80,7 +80,8 @@ public: QByteArray name() const; int mibEnum() const; - static iconv_t createIconv_t(const char *to, const char *from); + void init() const; + iconv_t createIconv_t(const char *to, const char *from) const; class IconvState { -- cgit v1.2.3