1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEPOOL_P_H
43 #define QDECLARATIVEPOOL_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qstring.h>
58 #include <QtCore/qurl.h>
62 // Exported for QtQuick1
63 class Q_DECLARATIVE_EXPORT QDeclarativePool
66 // The class has a destructor that needs to be called
69 inline QDeclarativePool *pool() const;
72 void *operator new(size_t);
73 void *operator new(size_t, void *m) { return m; }
74 friend class QDeclarativePool;
76 QDeclarativePool *_pool;
78 void (*_destroy)(Class *);
81 // The class is plain old data and no destructor needs to
85 inline QDeclarativePool *pool() const;
88 void *operator new(size_t);
89 void *operator new(size_t, void *m) { return m; }
90 friend class QDeclarativePool;
92 QDeclarativePool *_pool;
95 inline QDeclarativePool();
96 inline ~QDeclarativePool();
105 inline T *NewRawArray(int length);
107 inline QString *NewString(const QString &);
108 inline QByteArray *NewByteArray(const QByteArray &);
109 inline QUrl *NewUrl(const QUrl &);
113 List() : m_length(0), m_data(0) {}
114 List(const List &o) : m_length(o.m_length), m_data(o.m_data) {}
115 List &operator=(const List &o) {
116 m_length = o.m_length;
127 const T &at(int index) const {
128 Q_ASSERT(index < m_length);
129 return m_data[index];
131 T &operator[](int index) {
132 Q_ASSERT(index < m_length);
133 return m_data[index];
136 friend class QDeclarativePool;
137 List(T *d, int l) : m_length(l), m_data(d) {}
143 inline List<T> NewRawList(int length);
146 struct StringClass : public QString, public Class {
148 struct ByteArrayClass : public QByteArray, public Class {
150 struct UrlClass : public QUrl, public Class {
153 inline void *allocate(int size);
157 inline void initialize(POD *);
159 inline void initialize(Class *);
161 static void destroy(Class *c);
169 static const int pageSize = 4 * 4096 - sizeof(Header);
171 char memory[pageSize];
178 QDeclarativePool::QDeclarativePool()
179 : _page(0), _classList(0)
183 QDeclarativePool::~QDeclarativePool()
189 T *QDeclarativePool::New()
191 T *rv = new (allocate(sizeof(T))) T;
198 T *QDeclarativePool::NewRaw()
200 return (T*)allocate(sizeof(T));
204 T *QDeclarativePool::NewRawArray(int length)
206 return (T*)allocate(length * sizeof(T));
210 QDeclarativePool::List<T> QDeclarativePool::NewRawList(int length)
212 return List<T>(NewRawArray<T>(length), length);
215 QString *QDeclarativePool::NewString(const QString &s)
217 QString *rv = New<StringClass>();
222 QByteArray *QDeclarativePool::NewByteArray(const QByteArray &s)
224 QByteArray *rv = New<ByteArrayClass>();
229 QUrl *QDeclarativePool::NewUrl(const QUrl &s)
231 QUrl *rv = New<UrlClass>();
236 void *QDeclarativePool::allocate(int size)
238 if (!_page || (_page->header.free + size) > (_page->memory + Page::pageSize))
241 void *rv = _page->header.free;
242 _page->header.free += size + ((8 - size) & 7); // ensure 8 byte alignment;
247 void QDeclarativePool::initialize(QDeclarativePool::POD *)
252 void QDeclarativePool::initialize(QDeclarativePool::Class *c)
254 c->_next = _classList;
255 c->_destroy = &destroy<T>;
260 void QDeclarativePool::destroy(Class *c)
262 static_cast<T *>(c)->~T();
265 QDeclarativePool *QDeclarativePool::Class::pool() const
270 QDeclarativePool *QDeclarativePool::POD::pool() const
277 #endif // QDECLARATIVEPOOL_P_H