summaryrefslogtreecommitdiff
path: root/src/shared/cplusplus/MemoryPool.cpp
diff options
context:
space:
mode:
authorRoberto Raggi <roberto.raggi@nokia.com>2010-03-18 15:21:07 +0100
committerRoberto Raggi <roberto.raggi@nokia.com>2010-03-18 15:23:13 +0100
commit61a504c4273d01aecf1a149f5d9d8a3ab5e354c3 (patch)
tree4417d9e1e2f67acd3c56ec351055072e21cb6a27 /src/shared/cplusplus/MemoryPool.cpp
parent1e2af0a77dcddd84de43879609733cd55302756f (diff)
downloadqt-creator-61a504c4273d01aecf1a149f5d9d8a3ab5e354c3.tar.gz
Ensure that the memory pool can be reused after a rewind and get rid of the segmented array.
Diffstat (limited to 'src/shared/cplusplus/MemoryPool.cpp')
-rw-r--r--src/shared/cplusplus/MemoryPool.cpp61
1 files changed, 37 insertions, 24 deletions
diff --git a/src/shared/cplusplus/MemoryPool.cpp b/src/shared/cplusplus/MemoryPool.cpp
index 583320653d..56c9673200 100644
--- a/src/shared/cplusplus/MemoryPool.cpp
+++ b/src/shared/cplusplus/MemoryPool.cpp
@@ -57,20 +57,26 @@ MemoryPool::MemoryPool()
_blocks(0),
_allocatedBlocks(0),
_blockCount(-1),
- ptr(0),
- end(0)
+ _ptr(0),
+ _end(0)
{ }
MemoryPool::~MemoryPool()
{
- if (_blockCount != -1) {
- for (int i = 0; i < _blockCount + 1; ++i) {
- std::free(_blocks[i]);
+ if (_blocks) {
+ for (int i = 0; i < _allocatedBlocks; ++i) {
+ if (char *b = _blocks[i])
+ std::free(b);
}
- }
- if (_blocks)
std::free(_blocks);
+ }
+}
+
+void MemoryPool::reset()
+{
+ _blockCount = -1;
+ _ptr = _end = 0;
}
bool MemoryPool::initializeAllocatedMemory() const
@@ -85,39 +91,47 @@ void *MemoryPool::allocate_helper(size_t size)
if (++_blockCount == _allocatedBlocks) {
if (! _allocatedBlocks)
- _allocatedBlocks = 8;
+ _allocatedBlocks = DEFAULT_BLOCK_COUNT;
else
_allocatedBlocks *= 2;
_blocks = (char **) realloc(_blocks, sizeof(char *) * _allocatedBlocks);
+
+ for (int index = _blockCount; index < _allocatedBlocks; ++index)
+ _blocks[index] = 0;
}
char *&block = _blocks[_blockCount];
- if (_initializeAllocatedMemory)
- block = (char *) std::calloc(1, BLOCK_SIZE);
- else
+ if (! block)
block = (char *) std::malloc(BLOCK_SIZE);
- ptr = block;
- end = ptr + BLOCK_SIZE;
+ if (_initializeAllocatedMemory)
+ std::memset(block, '\0', BLOCK_SIZE);
+
+ _ptr = block;
+ _end = _ptr + BLOCK_SIZE;
- void *addr = ptr;
- ptr += size;
+ void *addr = _ptr;
+ _ptr += size;
return addr;
}
-MemoryPool::State MemoryPool::state() const
-{ return State(ptr, _blockCount); }
+RecursiveMemoryPool::RecursiveMemoryPool(MemoryPool *pool)
+ : _pool(pool),
+ _blockCount(pool->_blockCount),
+ _ptr(pool->_ptr),
+ _end(pool->_end)
+{
+}
-void MemoryPool::rewind(const State &state)
+RecursiveMemoryPool::~RecursiveMemoryPool()
{
- if (_blockCount == state.blockCount && state.ptr < ptr) {
- if (_initializeAllocatedMemory)
- std::memset(state.ptr, '\0', ptr - state.ptr);
+ _pool->_blockCount = _blockCount;
+ _pool->_ptr = _ptr;
+ _pool->_end = _end;
- ptr = state.ptr;
- }
+ std::memset(_pool->_ptr, 0, _pool->_end - _pool->_ptr);
}
Managed::Managed()
@@ -135,4 +149,3 @@ void Managed::operator delete(void *)
void Managed::operator delete(void *, MemoryPool *)
{ }
-