diff options
author | Kent Hansen <khansen@trolltech.com> | 2009-08-24 13:54:09 (GMT) |
---|---|---|
committer | Kent Hansen <khansen@trolltech.com> | 2009-08-24 13:58:47 (GMT) |
commit | 9e8b3f2e789e24c561d2f583927ee15b38808941 (patch) | |
tree | d6f0d5334f533bfc93e6415459cc7462da12eb7a /src/script/api/qscriptvalue.cpp | |
parent | 2c21acaee645590f907672b6cfc4d84597444418 (diff) | |
download | Qt-9e8b3f2e789e24c561d2f583927ee15b38808941.zip Qt-9e8b3f2e789e24c561d2f583927ee15b38808941.tar.gz Qt-9e8b3f2e789e24c561d2f583927ee15b38808941.tar.bz2 |
handle memory management of QScriptValuePrivate in engine if possible
Avoid calling malloc() and free() so often. The premise is that
QScriptValue is usually a short-lived type, and only a few QScriptValues
exist at a time, so if we cache privates in the engine, QScriptValues
will be much faster to create and destroy.
Reviewed-by: Olivier Goffart
Diffstat (limited to 'src/script/api/qscriptvalue.cpp')
-rw-r--r-- | src/script/api/qscriptvalue.cpp | 46 |
1 files changed, 31 insertions, 15 deletions
diff --git a/src/script/api/qscriptvalue.cpp b/src/script/api/qscriptvalue.cpp index a9c07a7..4b92049 100644 --- a/src/script/api/qscriptvalue.cpp +++ b/src/script/api/qscriptvalue.cpp @@ -406,6 +406,22 @@ void QScriptValuePrivate::detachFromEngine() engine = 0; } +void* QScriptValuePrivate::operator new(size_t size, QScriptEnginePrivate *engine) +{ + if (engine) + return engine->allocateScriptValuePrivate(size); + return qMalloc(size); +} + +void QScriptValuePrivate::operator delete(void *ptr) +{ + QScriptValuePrivate *d = reinterpret_cast<QScriptValuePrivate*>(ptr); + if (d->engine) + d->engine->freeScriptValuePrivate(d); + else + qFree(d); +} + /*! Constructs an invalid QScriptValue. */ @@ -440,7 +456,7 @@ QScriptValue::QScriptValue(const QScriptValue &other) registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, QScriptValue::SpecialValue value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); switch (value) { @@ -462,7 +478,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, QScriptValue::SpecialValue val registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, bool val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); d_ptr->initFrom(JSC::jsBoolean(val)); @@ -476,7 +492,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, bool val) registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, int val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); if (engine) { @@ -499,7 +515,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, int val) registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, uint val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); if (engine) { @@ -522,7 +538,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, uint val) registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, qsreal val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); if (engine) { @@ -545,7 +561,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, qsreal val) registers it with the script \a engine. */ QScriptValue::QScriptValue(QScriptEngine *engine, const QString &val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); if (engine) { @@ -566,7 +582,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, const QString &val) #ifndef QT_NO_CAST_FROM_ASCII QScriptValue::QScriptValue(QScriptEngine *engine, const char *val) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (QScriptEnginePrivate::get(engine))QScriptValuePrivate) { d_ptr->engine = QScriptEnginePrivate::get(engine); if (engine) { @@ -584,7 +600,7 @@ QScriptValue::QScriptValue(QScriptEngine *engine, const char *val) Constructs a new QScriptValue with a special \a value. */ QScriptValue::QScriptValue(SpecialValue value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; switch (value) { @@ -603,7 +619,7 @@ QScriptValue::QScriptValue(SpecialValue value) Constructs a new QScriptValue with a boolean \a value. */ QScriptValue::QScriptValue(bool value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; d_ptr->initFrom(JSC::jsBoolean(value)); @@ -615,7 +631,7 @@ QScriptValue::QScriptValue(bool value) Constructs a new QScriptValue with a number \a value. */ QScriptValue::QScriptValue(int value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; JSC::JSValue immediate = JSC::JSImmediate::from(value); @@ -631,7 +647,7 @@ QScriptValue::QScriptValue(int value) Constructs a new QScriptValue with a number \a value. */ QScriptValue::QScriptValue(uint value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; JSC::JSValue immediate = JSC::JSImmediate::from(value); @@ -647,7 +663,7 @@ QScriptValue::QScriptValue(uint value) Constructs a new QScriptValue with a number \a value. */ QScriptValue::QScriptValue(qsreal value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; JSC::JSValue immediate = JSC::JSImmediate::from(value); @@ -663,7 +679,7 @@ QScriptValue::QScriptValue(qsreal value) Constructs a new QScriptValue with a string \a value. */ QScriptValue::QScriptValue(const QString &value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; d_ptr->initFrom(value); @@ -675,7 +691,7 @@ QScriptValue::QScriptValue(const QString &value) Constructs a new QScriptValue with a string \a value. */ QScriptValue::QScriptValue(const QLatin1String &value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; d_ptr->initFrom(value); @@ -689,7 +705,7 @@ QScriptValue::QScriptValue(const QLatin1String &value) #ifndef QT_NO_CAST_FROM_ASCII QScriptValue::QScriptValue(const char *value) - : d_ptr(new QScriptValuePrivate) + : d_ptr(new (/*engine=*/0)QScriptValuePrivate) { d_ptr->engine = 0; d_ptr->initFrom(QString::fromAscii(value)); |