Change CompactHistoryBlockList pointer to unique pointer

Obs.: Qt containers can't use unique_ptr, I'm using std container.
wilder
Carlos Alves 5 years ago
parent 94505d94d4
commit 3bfe6125bd
  1. 39
      src/history/compact/CompactHistoryBlockList.cpp
  2. 6
      src/history/compact/CompactHistoryBlockList.h

@ -12,53 +12,44 @@
using namespace Konsole; using namespace Konsole;
CompactHistoryBlockList::CompactHistoryBlockList() CompactHistoryBlockList::CompactHistoryBlockList()
: list(QList<CompactHistoryBlock *>()) : _blocks()
{ {
} }
void *CompactHistoryBlockList::allocate(size_t size) void *CompactHistoryBlockList::allocate(size_t size)
{ {
CompactHistoryBlock *block; if (_blocks.empty() || _blocks.back()->remaining() < size) {
if (list.isEmpty() || list.last()->remaining() < size) { _blocks.push_back(std::make_unique<CompactHistoryBlock>());
block = new CompactHistoryBlock(); ////qDebug() << "new block created, remaining " << block->remaining() << "number of blocks=" << _blocks.size();
list.append(block);
////qDebug() << "new block created, remaining " << block->remaining() << "number of blocks=" << list.size();
} else {
block = list.last();
////qDebug() << "old block used, remaining " << block->remaining();
} }
return block->allocate(size); return _blocks.back()->allocate(size);
} }
void CompactHistoryBlockList::deallocate(void *ptr) void CompactHistoryBlockList::deallocate(void *ptr)
{ {
Q_ASSERT(!list.isEmpty()); Q_ASSERT(!_blocks.empty());
int i = 0; auto block = _blocks.begin();
CompactHistoryBlock *block = list.at(i); while (block != _blocks.end() && !(*block)->contains(ptr)) {
while (i < list.size() && !block->contains(ptr)) { block++;
i++;
block = list.at(i);
} }
Q_ASSERT(i < list.size()); Q_ASSERT(block != _blocks.end());
block->deallocate(); (*block)->deallocate();
if (!block->isInUse()) { if (!(*block)->isInUse()) {
list.removeAt(i); _blocks.erase(block);
delete block;
////qDebug() << "block deleted, new size = " << list.size(); ////qDebug() << "block deleted, new size = " << list.size();
} }
} }
int CompactHistoryBlockList::length() int CompactHistoryBlockList::length()
{ {
return list.size(); return _blocks.size();
} }
CompactHistoryBlockList::~CompactHistoryBlockList() CompactHistoryBlockList::~CompactHistoryBlockList()
{ {
qDeleteAll(list.begin(), list.end()); _blocks.clear();
list.clear();
} }

@ -7,7 +7,9 @@
#ifndef COMPACTHISTORYBLOCKLIST_H #ifndef COMPACTHISTORYBLOCKLIST_H
#define COMPACTHISTORYBLOCKLIST_H #define COMPACTHISTORYBLOCKLIST_H
#include <QList> #include <memory>
#include <vector>
#include "CompactHistoryBlock.h" #include "CompactHistoryBlock.h"
namespace Konsole namespace Konsole
@ -24,7 +26,7 @@ public:
int length(); int length();
private: private:
QList<CompactHistoryBlock *> list; std::vector<std::unique_ptr<CompactHistoryBlock>> _blocks;
}; };
} }

Loading…
Cancel
Save