a30839ade02a63b9b9a226d38abbc8f1deaa056d
[profile/ivi/qtdeclarative.git] / src / declarative / qml / ftw / qdeclarativepool_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file.  Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 **
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPOOL_P_H
43 #define QDECLARATIVEPOOL_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include <QtCore/qglobal.h>
57 #include <QtCore/qstring.h>
58
59 QT_BEGIN_NAMESPACE
60
61 class QDeclarativePool
62 {
63 public:
64     // The class has a destructor that needs to be called
65     class Class { 
66     public:
67         inline QDeclarativePool *pool() const;
68
69     private:
70         void *operator new(size_t);
71         void *operator new(size_t, void *m) { return m; }
72         friend class QDeclarativePool;
73
74         QDeclarativePool *_pool;
75         Class *_next;
76         void (*_destroy)(Class *);
77     };
78
79     // The class is plain old data and no destructor needs to
80     // be called
81     class POD {
82     public:
83         inline QDeclarativePool *pool() const;
84         
85     private:
86         void *operator new(size_t);
87         void *operator new(size_t, void *m) { return m; }
88         friend class QDeclarativePool;
89
90         QDeclarativePool *_pool;
91     };
92
93     inline QDeclarativePool();
94     inline ~QDeclarativePool();
95
96     void clear();
97
98     template<typename T>
99     inline T *New();
100
101     inline QString *NewString(const QString &);
102     inline QByteArray *NewByteArray(const QByteArray &);
103
104 private:
105     struct StringClass : public QString, public Class {
106     };
107     struct ByteArrayClass : public QByteArray, public Class {
108     };
109
110     inline void *allocate(int size);
111     void newpage();
112
113     template<typename T>
114     inline void initialize(POD *);
115     template<typename T>
116     inline void initialize(Class *);
117     template<typename T>
118     static void destroy(Class *c);
119
120     struct Page {
121         struct Header {
122             Page *next;
123             char *free;
124         } header;
125
126         static const int pageSize = 4 * 4096 - sizeof(Header);
127
128         char memory[pageSize];
129     };
130
131     Page *_page;
132     Class *_classList;
133 };
134
135 QDeclarativePool::QDeclarativePool()
136 : _page(0), _classList(0)
137 {
138 }
139
140 QDeclarativePool::~QDeclarativePool()
141 {
142     clear();
143 }
144
145 template<typename T>
146 T *QDeclarativePool::New()
147 {
148     T *rv = new (allocate(sizeof(T))) T;
149     initialize<T>(rv);
150     rv->_pool = this;
151     return rv;
152 }
153
154 QString *QDeclarativePool::NewString(const QString &s)
155 {
156     QString *rv = New<StringClass>();
157     *rv = s;
158     return rv;
159 }
160
161 QByteArray *QDeclarativePool::NewByteArray(const QByteArray &s)
162 {
163     QByteArray *rv = New<ByteArrayClass>();
164     *rv = s;
165     return rv;
166 }
167
168 void *QDeclarativePool::allocate(int size)
169 {
170     if (!_page || (_page->header.free + size) > (_page->memory + Page::pageSize))
171         newpage();
172
173     void *rv = _page->header.free;
174     _page->header.free += size + ((8 - size) & 7); // ensure 8 byte alignment;
175     return rv;
176 }
177
178 template<typename T>
179 void QDeclarativePool::initialize(QDeclarativePool::POD *)
180 {
181 }
182
183 template<typename T>
184 void QDeclarativePool::initialize(QDeclarativePool::Class *c)
185 {
186     c->_next = _classList;
187     c->_destroy = &destroy<T>;
188     _classList = c;
189 }
190
191 template<typename T>
192 void QDeclarativePool::destroy(Class *c)
193 {
194     static_cast<T *>(c)->~T();
195 }
196
197 QDeclarativePool *QDeclarativePool::Class::pool() const
198 {
199     return _pool;
200 }
201
202 QDeclarativePool *QDeclarativePool::POD::pool() const
203 {
204     return _pool;
205 }
206
207 QT_END_NAMESPACE
208
209 #endif // QDECLARATIVEPOOL_P_H
210