1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef QSCRIPTMEMORYPOOL_P_H
|
---|
43 | #define QSCRIPTMEMORYPOOL_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/qshareddata.h>
|
---|
58 | #include <string.h>
|
---|
59 |
|
---|
60 | QT_BEGIN_NAMESPACE
|
---|
61 |
|
---|
62 | namespace QScript {
|
---|
63 |
|
---|
64 | class MemoryPool : public QSharedData
|
---|
65 | {
|
---|
66 | public:
|
---|
67 | enum { maxBlockCount = -1 };
|
---|
68 | enum { defaultBlockSize = 1 << 12 };
|
---|
69 |
|
---|
70 | MemoryPool() {
|
---|
71 | m_blockIndex = maxBlockCount;
|
---|
72 | m_currentIndex = 0;
|
---|
73 | m_storage = 0;
|
---|
74 | m_currentBlock = 0;
|
---|
75 | m_currentBlockSize = 0;
|
---|
76 | }
|
---|
77 |
|
---|
78 | virtual ~MemoryPool() {
|
---|
79 | for (int index = 0; index < m_blockIndex + 1; ++index)
|
---|
80 | qFree(m_storage[index]);
|
---|
81 |
|
---|
82 | qFree(m_storage);
|
---|
83 | }
|
---|
84 |
|
---|
85 | char *allocate(int bytes) {
|
---|
86 | bytes += (8 - bytes) & 7; // ensure multiple of 8 bytes (maintain alignment)
|
---|
87 | if (m_currentBlock == 0 || m_currentBlockSize < m_currentIndex + bytes) {
|
---|
88 | ++m_blockIndex;
|
---|
89 | m_currentBlockSize = defaultBlockSize << m_blockIndex;
|
---|
90 |
|
---|
91 | m_storage = reinterpret_cast<char**>(qRealloc(m_storage, sizeof(char*) * (1 + m_blockIndex)));
|
---|
92 | m_currentBlock = m_storage[m_blockIndex] = reinterpret_cast<char*>(qMalloc(m_currentBlockSize));
|
---|
93 | ::memset(m_currentBlock, 0, m_currentBlockSize);
|
---|
94 |
|
---|
95 | m_currentIndex = (8 - quintptr(m_currentBlock)) & 7; // ensure first chunk is 64-bit aligned
|
---|
96 | Q_ASSERT(m_currentIndex + bytes <= m_currentBlockSize);
|
---|
97 | }
|
---|
98 |
|
---|
99 | char *p = reinterpret_cast<char *>
|
---|
100 | (m_currentBlock + m_currentIndex);
|
---|
101 |
|
---|
102 | m_currentIndex += bytes;
|
---|
103 |
|
---|
104 | return p;
|
---|
105 | }
|
---|
106 |
|
---|
107 | int bytesAllocated() const {
|
---|
108 | int bytes = 0;
|
---|
109 | for (int index = 0; index < m_blockIndex; ++index)
|
---|
110 | bytes += (defaultBlockSize << index);
|
---|
111 | bytes += m_currentIndex;
|
---|
112 | return bytes;
|
---|
113 | }
|
---|
114 |
|
---|
115 | private:
|
---|
116 | int m_blockIndex;
|
---|
117 | int m_currentIndex;
|
---|
118 | char *m_currentBlock;
|
---|
119 | int m_currentBlockSize;
|
---|
120 | char **m_storage;
|
---|
121 |
|
---|
122 | private:
|
---|
123 | Q_DISABLE_COPY(MemoryPool)
|
---|
124 | };
|
---|
125 |
|
---|
126 | } // namespace QScript
|
---|
127 |
|
---|
128 | QT_END_NAMESPACE
|
---|
129 |
|
---|
130 | #endif
|
---|