1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #ifndef QQMLJSMEMORYPOOL_P_H |
5 | #define QQMLJSMEMORYPOOL_P_H |
6 | |
7 | // |
8 | // W A R N I N G |
9 | // ------------- |
10 | // |
11 | // This file is not part of the Qt API. It exists purely as an |
12 | // implementation detail. This header file may change from version to |
13 | // version without notice, or even be removed. |
14 | // |
15 | // We mean it. |
16 | // |
17 | |
18 | #include <QtCore/private/qglobal_p.h> |
19 | #include <QtCore/qstring.h> |
20 | #include <QtCore/qvector.h> |
21 | |
22 | #include <cstdlib> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | namespace QQmlJS { |
27 | |
28 | class Managed; |
29 | |
30 | class MemoryPool |
31 | { |
32 | Q_DISABLE_COPY_MOVE(MemoryPool); |
33 | |
34 | public: |
35 | MemoryPool() = default; |
36 | ~MemoryPool() |
37 | { |
38 | if (_blocks) { |
39 | for (int i = 0; i < _allocatedBlocks; ++i) { |
40 | if (char *b = _blocks[i]) |
41 | free(ptr: b); |
42 | } |
43 | |
44 | free(ptr: _blocks); |
45 | } |
46 | } |
47 | |
48 | inline void *allocate(size_t size) |
49 | { |
50 | size = (size + 7) & ~size_t(7); |
51 | if (Q_LIKELY(_ptr && size < size_t(_end - _ptr))) { |
52 | void *addr = _ptr; |
53 | _ptr += size; |
54 | return addr; |
55 | } |
56 | return allocate_helper(size); |
57 | } |
58 | |
59 | void reset() |
60 | { |
61 | _blockCount = -1; |
62 | _ptr = _end = nullptr; |
63 | } |
64 | |
65 | template <typename Tp> Tp *New() { return new (this->allocate(size: sizeof(Tp))) Tp(); } |
66 | template <typename Tp, typename... Ta> Tp *New(Ta... args) |
67 | { return new (this->allocate(size: sizeof(Tp))) Tp(args...); } |
68 | |
69 | QStringView newString(QString string) { |
70 | return strings.emplace_back(args: std::move(string)); |
71 | } |
72 | |
73 | private: |
74 | Q_NEVER_INLINE void *allocate_helper(size_t size) |
75 | { |
76 | size_t currentBlockSize = DEFAULT_BLOCK_SIZE; |
77 | while (Q_UNLIKELY(size >= currentBlockSize)) |
78 | currentBlockSize *= 2; |
79 | |
80 | if (++_blockCount == _allocatedBlocks) { |
81 | if (! _allocatedBlocks) |
82 | _allocatedBlocks = DEFAULT_BLOCK_COUNT; |
83 | else |
84 | _allocatedBlocks *= 2; |
85 | |
86 | _blocks = reinterpret_cast<char **>(realloc(ptr: _blocks, size: sizeof(char *) * size_t(_allocatedBlocks))); |
87 | Q_CHECK_PTR(_blocks); |
88 | |
89 | for (int index = _blockCount; index < _allocatedBlocks; ++index) |
90 | _blocks[index] = nullptr; |
91 | } |
92 | |
93 | char *&block = _blocks[_blockCount]; |
94 | |
95 | if (! block) { |
96 | block = reinterpret_cast<char *>(malloc(size: currentBlockSize)); |
97 | Q_CHECK_PTR(block); |
98 | } |
99 | |
100 | _ptr = block; |
101 | _end = _ptr + currentBlockSize; |
102 | |
103 | void *addr = _ptr; |
104 | _ptr += size; |
105 | return addr; |
106 | } |
107 | |
108 | private: |
109 | char **_blocks = nullptr; |
110 | int _allocatedBlocks = 0; |
111 | int _blockCount = -1; |
112 | char *_ptr = nullptr; |
113 | char *_end = nullptr; |
114 | QStringList strings; |
115 | |
116 | enum |
117 | { |
118 | DEFAULT_BLOCK_SIZE = 8 * 1024, |
119 | DEFAULT_BLOCK_COUNT = 8 |
120 | }; |
121 | }; |
122 | |
123 | class Managed |
124 | { |
125 | Q_DISABLE_COPY(Managed) |
126 | public: |
127 | Managed() = default; |
128 | ~Managed() = default; |
129 | |
130 | void *operator new(size_t size, MemoryPool *pool) { return pool->allocate(size); } |
131 | void operator delete(void *) {} |
132 | void operator delete(void *, MemoryPool *) {} |
133 | }; |
134 | |
135 | } // namespace QQmlJS |
136 | |
137 | QT_END_NAMESPACE |
138 | |
139 | #endif |
140 |