forked from qt-creator/qt-creator
		
	Ensure that the memory pool can be reused after a rewind and get rid of the segmented array.
This commit is contained in:
		@@ -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);
 | 
			
		||||
 | 
			
		||||
    void *addr = ptr;
 | 
			
		||||
    ptr += size;
 | 
			
		||||
    _ptr = block;
 | 
			
		||||
    _end = _ptr + BLOCK_SIZE;
 | 
			
		||||
 | 
			
		||||
    void *addr = _ptr;
 | 
			
		||||
    _ptr += size;
 | 
			
		||||
    return addr;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
MemoryPool::State MemoryPool::state() const
 | 
			
		||||
{ return State(ptr, _blockCount); }
 | 
			
		||||
 | 
			
		||||
void MemoryPool::rewind(const State &state)
 | 
			
		||||
RecursiveMemoryPool::RecursiveMemoryPool(MemoryPool *pool)
 | 
			
		||||
    : _pool(pool),
 | 
			
		||||
      _blockCount(pool->_blockCount),
 | 
			
		||||
      _ptr(pool->_ptr),
 | 
			
		||||
      _end(pool->_end)
 | 
			
		||||
{
 | 
			
		||||
    if (_blockCount == state.blockCount && state.ptr < ptr) {
 | 
			
		||||
        if (_initializeAllocatedMemory)
 | 
			
		||||
            std::memset(state.ptr, '\0', ptr - state.ptr);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
        ptr = state.ptr;
 | 
			
		||||
    }
 | 
			
		||||
RecursiveMemoryPool::~RecursiveMemoryPool()
 | 
			
		||||
{
 | 
			
		||||
    _pool->_blockCount = _blockCount;
 | 
			
		||||
    _pool->_ptr = _ptr;
 | 
			
		||||
    _pool->_end = _end;
 | 
			
		||||
 | 
			
		||||
    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 *)
 | 
			
		||||
{ }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user