diff options
author | Roberto Raggi <roberto.raggi@nokia.com> | 2011-07-19 15:12:40 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-08-30 13:18:28 +0200 |
commit | ef9751ee246ca77a75ece557326311ac1436db0c (patch) | |
tree | 278d3546dcecfcce05ccc5003c23243b4af88644 /src/declarative/qml/parser | |
parent | 5f54b6c091ddfd69f02c4365820ac27432e87e9f (diff) |
Change the V4 IR so it can be stored in a memory pool.
That is, remove members with a non-trivial destructor from IR::Stmt
and IR::Expr (that's because we don't destroy the objects
allocated in the pool).
Added the method MemoryPool::reset() so we can clear the pool
without disposing the allocated memory.
Change-Id: I126332be387c016578c086db8b3aa8098b2507f6
Reviewed-on: http://codereview.qt.nokia.com/3758
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Diffstat (limited to 'src/declarative/qml/parser')
-rw-r--r-- | src/declarative/qml/parser/qdeclarativejsmemorypool_p.h | 111 |
1 files changed, 69 insertions, 42 deletions
diff --git a/src/declarative/qml/parser/qdeclarativejsmemorypool_p.h b/src/declarative/qml/parser/qdeclarativejsmemorypool_p.h index e921ac6331..043331d9e8 100644 --- a/src/declarative/qml/parser/qdeclarativejsmemorypool_p.h +++ b/src/declarative/qml/parser/qdeclarativejsmemorypool_p.h @@ -57,8 +57,9 @@ #include <QtCore/qglobal.h> #include <QtCore/qshareddata.h> +#include <QtCore/qdebug.h> -#include <string.h> +#include <cstring> QT_QML_BEGIN_NAMESPACE @@ -66,63 +67,89 @@ namespace QDeclarativeJS { class QML_PARSER_EXPORT MemoryPool : public QSharedData { + MemoryPool(const MemoryPool &other); + void operator =(const MemoryPool &other); + public: - enum { maxBlockCount = -1 }; - enum { defaultBlockSize = 1 << 12 }; - - MemoryPool() { - m_blockIndex = maxBlockCount; - m_currentIndex = 0; - m_storage = 0; - m_currentBlock = 0; - m_currentBlockSize = 0; + MemoryPool() + : _blocks(0), + _allocatedBlocks(0), + _blockCount(-1), + _ptr(0), + _end(0) + { } + + ~MemoryPool() + { + if (_blocks) { + for (int i = 0; i < _allocatedBlocks; ++i) { + if (char *b = _blocks[i]) + qFree(b); + } + + qFree(_blocks); + } } - virtual ~MemoryPool() { - for (int index = 0; index < m_blockIndex + 1; ++index) - qFree(m_storage[index]); + inline void *allocate(size_t size) + { + size = (size + 7) & ~7; + if (_ptr && (_ptr + size < _end)) { + void *addr = _ptr; + _ptr += size; + return addr; + } + return allocate_helper(size); + } - qFree(m_storage); + void reset() + { + _blockCount = -1; + _ptr = _end = 0; } - char *allocate(int bytes) { - bytes += (8 - bytes) & 7; // ensure multiple of 8 bytes (maintain alignment) - if (m_currentBlock == 0 || m_currentBlockSize < m_currentIndex + bytes) { - ++m_blockIndex; - m_currentBlockSize = defaultBlockSize << m_blockIndex; +private: + void *allocate_helper(size_t size) + { + Q_ASSERT(size < BLOCK_SIZE); - m_storage = reinterpret_cast<char**>(qRealloc(m_storage, sizeof(char*) * (1 + m_blockIndex))); - m_currentBlock = m_storage[m_blockIndex] = reinterpret_cast<char*>(qMalloc(m_currentBlockSize)); + if (++_blockCount == _allocatedBlocks) { + if (! _allocatedBlocks) + _allocatedBlocks = DEFAULT_BLOCK_COUNT; + else + _allocatedBlocks *= 2; - m_currentIndex = (8 - quintptr(m_currentBlock)) & 7; // ensure first chunk is 64-bit aligned - Q_ASSERT(m_currentIndex + bytes <= m_currentBlockSize); + _blocks = (char **) qRealloc(_blocks, sizeof(char *) * _allocatedBlocks); + + for (int index = _blockCount; index < _allocatedBlocks; ++index) + _blocks[index] = 0; } - char *p = reinterpret_cast<char *> - (m_currentBlock + m_currentIndex); + char *&block = _blocks[_blockCount]; - m_currentIndex += bytes; + if (! block) + block = (char *) qMalloc(BLOCK_SIZE); - return p; - } + _ptr = block; + _end = _ptr + BLOCK_SIZE; - int bytesAllocated() const { - int bytes = 0; - for (int index = 0; index < m_blockIndex; ++index) - bytes += (defaultBlockSize << index); - bytes += m_currentIndex; - return bytes; + void *addr = _ptr; + _ptr += size; + return addr; } private: - int m_blockIndex; - int m_currentIndex; - char *m_currentBlock; - int m_currentBlockSize; - char **m_storage; - -private: - Q_DISABLE_COPY(MemoryPool) + char **_blocks; + int _allocatedBlocks; + int _blockCount; + char *_ptr; + char *_end; + + enum + { + BLOCK_SIZE = 8 * 1024, + DEFAULT_BLOCK_COUNT = 8 + }; }; class QML_PARSER_EXPORT Managed |