1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
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 The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/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 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QQMLJSMEMORYPOOL_P_H |
41 | #define QQMLJSMEMORYPOOL_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qglobal.h> |
55 | #include <QtCore/qshareddata.h> |
56 | #include <QtCore/qdebug.h> |
57 | |
58 | #include <cstring> |
59 | |
60 | QT_BEGIN_NAMESPACE |
61 | |
62 | namespace QQmlJS { |
63 | |
64 | class Managed; |
65 | |
66 | class MemoryPool : public QSharedData |
67 | { |
68 | MemoryPool(const MemoryPool &other); |
69 | void operator =(const MemoryPool &other); |
70 | |
71 | public: |
72 | MemoryPool() {} |
73 | |
74 | ~MemoryPool() |
75 | { |
76 | if (_blocks) { |
77 | for (int i = 0; i < _allocatedBlocks; ++i) { |
78 | if (char *b = _blocks[i]) |
79 | free(ptr: b); |
80 | } |
81 | |
82 | free(ptr: _blocks); |
83 | } |
84 | qDeleteAll(c: strings); |
85 | } |
86 | |
87 | inline void *allocate(size_t size) |
88 | { |
89 | size = (size + 7) & ~size_t(7); |
90 | if (Q_LIKELY(_ptr && (_ptr + size < _end))) { |
91 | void *addr = _ptr; |
92 | _ptr += size; |
93 | return addr; |
94 | } |
95 | return allocate_helper(size); |
96 | } |
97 | |
98 | void reset() |
99 | { |
100 | _blockCount = -1; |
101 | _ptr = _end = nullptr; |
102 | } |
103 | |
104 | template <typename Tp> Tp *New() { return new (this->allocate(size: sizeof(Tp))) Tp(); } |
105 | template <typename Tp, typename... Ta> Tp *New(Ta... args) |
106 | { return new (this->allocate(size: sizeof(Tp))) Tp(args...); } |
107 | |
108 | QStringRef newString(const QString &string) { |
109 | strings.append(t: new QString(string)); |
110 | return QStringRef(strings.last()); |
111 | } |
112 | |
113 | private: |
114 | Q_NEVER_INLINE void *allocate_helper(size_t size) |
115 | { |
116 | size_t currentBlockSize = DEFAULT_BLOCK_SIZE; |
117 | while (Q_UNLIKELY(size >= currentBlockSize)) |
118 | currentBlockSize *= 2; |
119 | |
120 | if (++_blockCount == _allocatedBlocks) { |
121 | if (! _allocatedBlocks) |
122 | _allocatedBlocks = DEFAULT_BLOCK_COUNT; |
123 | else |
124 | _allocatedBlocks *= 2; |
125 | |
126 | _blocks = reinterpret_cast<char **>(realloc(ptr: _blocks, size: sizeof(char *) * size_t(_allocatedBlocks))); |
127 | Q_CHECK_PTR(_blocks); |
128 | |
129 | for (int index = _blockCount; index < _allocatedBlocks; ++index) |
130 | _blocks[index] = nullptr; |
131 | } |
132 | |
133 | char *&block = _blocks[_blockCount]; |
134 | |
135 | if (! block) { |
136 | block = reinterpret_cast<char *>(malloc(size: currentBlockSize)); |
137 | Q_CHECK_PTR(block); |
138 | } |
139 | |
140 | _ptr = block; |
141 | _end = _ptr + currentBlockSize; |
142 | |
143 | void *addr = _ptr; |
144 | _ptr += size; |
145 | return addr; |
146 | } |
147 | |
148 | private: |
149 | char **_blocks = nullptr; |
150 | int _allocatedBlocks = 0; |
151 | int _blockCount = -1; |
152 | char *_ptr = nullptr; |
153 | char *_end = nullptr; |
154 | QVector<QString*> strings; |
155 | |
156 | enum |
157 | { |
158 | DEFAULT_BLOCK_SIZE = 8 * 1024, |
159 | DEFAULT_BLOCK_COUNT = 8 |
160 | }; |
161 | }; |
162 | |
163 | class Managed |
164 | { |
165 | Q_DISABLE_COPY(Managed) |
166 | public: |
167 | Managed() = default; |
168 | ~Managed() = default; |
169 | |
170 | void *operator new(size_t size, MemoryPool *pool) { return pool->allocate(size); } |
171 | void operator delete(void *) {} |
172 | void operator delete(void *, MemoryPool *) {} |
173 | }; |
174 | |
175 | } // namespace QQmlJS |
176 | |
177 | QT_END_NAMESPACE |
178 | |
179 | #endif |
180 | |