1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEJSMEMORYPOOL_P_H
43 #define QDECLARATIVEJSMEMORYPOOL_P_H
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.
56 #include "qdeclarativejsglobal_p.h"
58 #include <QtCore/qglobal.h>
59 #include <QtCore/qshareddata.h>
60 #include <QtCore/qdebug.h>
64 QT_QML_BEGIN_NAMESPACE
66 namespace QDeclarativeJS {
68 class QML_PARSER_EXPORT MemoryPool : public QSharedData
70 MemoryPool(const MemoryPool &other);
71 void operator =(const MemoryPool &other);
85 for (int i = 0; i < _allocatedBlocks; ++i) {
86 if (char *b = _blocks[i])
94 inline void *allocate(size_t size)
96 size = (size + 7) & ~7;
97 if (_ptr && (_ptr + size < _end)) {
102 return allocate_helper(size);
112 void *allocate_helper(size_t size)
114 Q_ASSERT(size < BLOCK_SIZE);
116 if (++_blockCount == _allocatedBlocks) {
117 if (! _allocatedBlocks)
118 _allocatedBlocks = DEFAULT_BLOCK_COUNT;
120 _allocatedBlocks *= 2;
122 _blocks = (char **) qRealloc(_blocks, sizeof(char *) * _allocatedBlocks);
124 for (int index = _blockCount; index < _allocatedBlocks; ++index)
128 char *&block = _blocks[_blockCount];
131 block = (char *) qMalloc(BLOCK_SIZE);
134 _end = _ptr + BLOCK_SIZE;
143 int _allocatedBlocks;
150 BLOCK_SIZE = 8 * 1024,
151 DEFAULT_BLOCK_COUNT = 8
155 class QML_PARSER_EXPORT Managed
157 Managed(const Managed &other);
158 void operator = (const Managed &other);
164 void *operator new(size_t size, MemoryPool *pool) { return pool->allocate(size); }
165 void operator delete(void *) {}
166 void operator delete(void *, MemoryPool *) {}
169 } // namespace QDeclarativeJS