Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / parser / qdeclarativejsmemorypool_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEJSMEMORYPOOL_P_H
43 #define QDECLARATIVEJSMEMORYPOOL_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 "qdeclarativejsglobal_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 QDeclarativeJS {
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                     qFree(b);
88             }
89
90             qFree(_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 **) qRealloc(_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 *) qMalloc(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 QDeclarativeJS
170
171 QT_QML_END_NAMESPACE
172
173 #endif