summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-07-20 20:49:13 +0200
committerQt by Nokia <qt-info@nokia.com>2012-07-31 11:12:00 +0200
commit46e3aa29081e0c043d745e590b9d3b9220f8d471 (patch)
treed5f5adf56ec334b88a02be5afcc7eda85c588fcd /src/corelib
parentd4510d0d31759a7b5993d3b9e710642a9d340f53 (diff)
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 <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/codecs/qiconvcodec.cpp13
-rw-r--r--src/corelib/codecs/qiconvcodec_p.h3
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
@@ -93,6 +93,10 @@ QT_BEGIN_NAMESPACE
QIconvCodec::QIconvCodec()
: utf16Codec(0)
{
+}
+
+void QIconvCodec::init() const
+{
utf16Codec = QTextCodec::codecForMib(1015);
Q_ASSERT_X(utf16Codec != 0,
"QIconvCodec::convertToUnicode",
@@ -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<iconv_t>(-1)) {
static int reported = 0;
if (!reported++) {
@@ -348,7 +352,7 @@ QByteArray QIconvCodec::convertFromUnicode(const QChar *uc, int len, ConverterSt
QThreadStorage<QIconvCodec::IconvState *> *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<iconv_t>(-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
{