summaryrefslogtreecommitdiffstats
path: root/src/script/api/qscriptstring.cpp
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-09-02 16:14:01 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-09-02 16:17:13 (GMT)
commite4dfcd4392e5be1b5de8648fc20ff45f7faa30ca (patch)
tree8e1fbb0779b150d556b8d2807271c17090989cb6 /src/script/api/qscriptstring.cpp
parent03274e0f539086cbf58d3fbd8e7e126a0cc16433 (diff)
downloadQt-e4dfcd4392e5be1b5de8648fc20ff45f7faa30ca.zip
Qt-e4dfcd4392e5be1b5de8648fc20ff45f7faa30ca.tar.gz
Qt-e4dfcd4392e5be1b5de8648fc20ff45f7faa30ca.tar.bz2
improve memory management scheme of QScriptString(Private)
Get rid of QPointer. Use linked list of privates (like was recently done for QScriptValue). Allocate the private on the stack when we can. Reviewed-by: Olivier Goffart
Diffstat (limited to 'src/script/api/qscriptstring.cpp')
-rw-r--r--src/script/api/qscriptstring.cpp53
1 files changed, 34 insertions, 19 deletions
diff --git a/src/script/api/qscriptstring.cpp b/src/script/api/qscriptstring.cpp
index 6de1d88..8c818e1 100644
--- a/src/script/api/qscriptstring.cpp
+++ b/src/script/api/qscriptstring.cpp
@@ -39,10 +39,10 @@
**
****************************************************************************/
-#include "config.h"
#include "qscriptstring.h"
-
#include "qscriptstring_p.h"
+#include "qscriptengine.h"
+#include "qscriptengine_p.h"
QT_BEGIN_NAMESPACE
@@ -70,22 +70,6 @@ QT_BEGIN_NAMESPACE
*/
/*!
- \internal
-*/
-QScriptStringPrivate::QScriptStringPrivate(QScriptEngine *e, const JSC::Identifier &id)
- : engine(e), identifier(id)
-{
- ref = 0;
-}
-
-/*!
- \internal
-*/
-QScriptStringPrivate::~QScriptStringPrivate()
-{
-}
-
-/*!
Constructs an invalid QScriptString.
*/
QScriptString::QScriptString()
@@ -99,6 +83,13 @@ QScriptString::QScriptString()
QScriptString::QScriptString(const QScriptString &other)
: d_ptr(other.d_ptr)
{
+ if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
+ Q_ASSERT(d_func()->ref != 1);
+ d_ptr.detach();
+ d_func()->ref = 1;
+ d_func()->type = QScriptStringPrivate::HeapAllocated;
+ d_func()->engine->registerScriptString(d_func());
+ }
}
/*!
@@ -106,6 +97,19 @@ QScriptString::QScriptString(const QScriptString &other)
*/
QScriptString::~QScriptString()
{
+ Q_D(QScriptString);
+ if (d) {
+ switch (d->type) {
+ case QScriptStringPrivate::StackAllocated:
+ Q_ASSERT(d->ref == 1);
+ d->ref.ref(); // avoid deletion
+ break;
+ case QScriptStringPrivate::HeapAllocated:
+ if (d->engine && (d->ref == 1))
+ d->engine->unregisterScriptString(d);
+ break;
+ }
+ }
}
/*!
@@ -113,7 +117,18 @@ QScriptString::~QScriptString()
*/
QScriptString &QScriptString::operator=(const QScriptString &other)
{
+ if (d_func() && d_func()->engine && (d_func()->ref == 1) && (d_func()->type == QScriptStringPrivate::HeapAllocated)) {
+ // current d_ptr will be deleted at the assignment below, so unregister it first
+ d_func()->engine->unregisterScriptString(d_func());
+ }
d_ptr = other.d_ptr;
+ if (d_func() && (d_func()->type == QScriptStringPrivate::StackAllocated)) {
+ Q_ASSERT(d_func()->ref != 1);
+ d_ptr.detach();
+ d_func()->ref = 1;
+ d_func()->type = QScriptStringPrivate::HeapAllocated;
+ d_func()->engine->registerScriptString(d_func());
+ }
return *this;
}
@@ -137,7 +152,7 @@ bool QScriptString::operator==(const QScriptString &other) const
if (d == other.d_func())
return true;
if (!d || !other.d_func())
- return false;
+ return d == other.d_func();
if (d->engine != other.d_func()->engine)
return false;
if (!d->engine)