Change copyrights from Nokia to Digia
[profile/ivi/qtdeclarative.git] / src / qml / qml / parser / qqmljsmemorypool_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQMLJSMEMORYPOOL_P_H
43 #define QQMLJSMEMORYPOOL_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 "qqmljsglobal_p.h"
57
58 #include <QtCore/qglobal.h>
59 #include <QtCore/qshareddata.h>
60 #include <QtCore/qdebug.h>
61
62 #include <cstring>
63
64 QT_QML_BEGIN_NAMESPACE
65
66 namespace QQmlJS {
67
68 class QML_PARSER_EXPORT MemoryPool : public QSharedData
69 {
70     MemoryPool(const MemoryPool &other);
71     void operator =(const MemoryPool &other);
72
73 public:
74     MemoryPool()
75         : _blocks(0),
76           _allocatedBlocks(0),
77           _blockCount(-1),
78           _ptr(0),
79           _end(0)
80     { }
81
82     ~MemoryPool()
83     {
84         if (_blocks) {
85             for (int i = 0; i < _allocatedBlocks; ++i) {
86                 if (char *b = _blocks[i])
87                     free(b);
88             }
89
90             free(_blocks);
91         }
92     }
93
94     inline void *allocate(size_t size)
95     {
96         size = (size + 7) & ~7;
97         if (_ptr && (_ptr + size < _end)) {
98             void *addr = _ptr;
99             _ptr += size;
100             return addr;
101         }
102         return allocate_helper(size);
103     }
104
105     void reset()
106     {
107         _blockCount = -1;
108         _ptr = _end = 0;
109     }
110
111 private:
112     void *allocate_helper(size_t size)
113     {
114         Q_ASSERT(size < BLOCK_SIZE);
115
116         if (++_blockCount == _allocatedBlocks) {
117             if (! _allocatedBlocks)
118                 _allocatedBlocks = DEFAULT_BLOCK_COUNT;
119             else
120                 _allocatedBlocks *= 2;
121
122             _blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks);
123
124             for (int index = _blockCount; index < _allocatedBlocks; ++index)
125                 _blocks[index] = 0;
126         }
127
128         char *&block = _blocks[_blockCount];
129
130         if (! block)
131             block = (char *) malloc(BLOCK_SIZE);
132
133         _ptr = block;
134         _end = _ptr + BLOCK_SIZE;
135
136         void *addr = _ptr;
137         _ptr += size;
138         return addr;
139     }
140
141 private:
142     char **_blocks;
143     int _allocatedBlocks;
144     int _blockCount;
145     char *_ptr;
146     char *_end;
147
148     enum
149     {
150         BLOCK_SIZE = 8 * 1024,
151         DEFAULT_BLOCK_COUNT = 8
152     };
153 };
154
155 class QML_PARSER_EXPORT Managed
156 {
157     Managed(const Managed &other);
158     void operator = (const Managed &other);
159
160 public:
161     Managed() {}
162     ~Managed() {}
163
164     void *operator new(size_t size, MemoryPool *pool) { return pool->allocate(size); }
165     void operator delete(void *) {}
166     void operator delete(void *, MemoryPool *) {}
167 };
168
169 } // namespace QQmlJS
170
171 QT_QML_END_NAMESPACE
172
173 #endif