Optimize default property resolution in compiler
[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     template<typename T>
101     inline T *NewRaw();
102     template<typename T>
103     inline T *NewRawArray(int length);
104
105     inline QString *NewString(const QString &);
106     inline QByteArray *NewByteArray(const QByteArray &);
107
108 private:
109     struct StringClass : public QString, public Class {
110     };
111     struct ByteArrayClass : public QByteArray, public Class {
112     };
113
114     inline void *allocate(int size);
115     void newpage();
116
117     template<typename T>
118     inline void initialize(POD *);
119     template<typename T>
120     inline void initialize(Class *);
121     template<typename T>
122     static void destroy(Class *c);
123
124     struct Page {
125         struct Header {
126             Page *next;
127             char *free;
128         } header;
129
130         static const int pageSize = 4 * 4096 - sizeof(Header);
131
132         char memory[pageSize];
133     };
134
135     Page *_page;
136     Class *_classList;
137 };
138
139 QDeclarativePool::QDeclarativePool()
140 : _page(0), _classList(0)
141 {
142 }
143
144 QDeclarativePool::~QDeclarativePool()
145 {
146     clear();
147 }
148
149 template<typename T>
150 T *QDeclarativePool::New()
151 {
152     T *rv = new (allocate(sizeof(T))) T;
153     initialize<T>(rv);
154     rv->_pool = this;
155     return rv;
156 }
157
158 template<typename T>
159 T *QDeclarativePool::NewRaw()
160 {
161     return (T*)allocate(sizeof(T));
162 }
163
164 template<typename T>
165 T *QDeclarativePool::NewRawArray(int length)
166 {
167     return (T*)allocate(length * sizeof(T));
168 }
169
170 QString *QDeclarativePool::NewString(const QString &s)
171 {
172     QString *rv = New<StringClass>();
173     *rv = s;
174     return rv;
175 }
176
177 QByteArray *QDeclarativePool::NewByteArray(const QByteArray &s)
178 {
179     QByteArray *rv = New<ByteArrayClass>();
180     *rv = s;
181     return rv;
182 }
183
184 void *QDeclarativePool::allocate(int size)
185 {
186     if (!_page || (_page->header.free + size) > (_page->memory + Page::pageSize))
187         newpage();
188
189     void *rv = _page->header.free;
190     _page->header.free += size + ((8 - size) & 7); // ensure 8 byte alignment;
191     return rv;
192 }
193
194 template<typename T>
195 void QDeclarativePool::initialize(QDeclarativePool::POD *)
196 {
197 }
198
199 template<typename T>
200 void QDeclarativePool::initialize(QDeclarativePool::Class *c)
201 {
202     c->_next = _classList;
203     c->_destroy = &destroy<T>;
204     _classList = c;
205 }
206
207 template<typename T>
208 void QDeclarativePool::destroy(Class *c)
209 {
210     static_cast<T *>(c)->~T();
211 }
212
213 QDeclarativePool *QDeclarativePool::Class::pool() const
214 {
215     return _pool;
216 }
217
218 QDeclarativePool *QDeclarativePool::POD::pool() const
219 {
220     return _pool;
221 }
222
223 QT_END_NAMESPACE
224
225 #endif // QDECLARATIVEPOOL_P_H
226