Commit 78971aa5 authored by Matan Ziv-Av's avatar Matan Ziv-Av Committed by Tomaz Canabrava
Browse files

Improve memory handling

- Remove placements when they scroll out of the scroll buffer
- Delete removed placements.
- Free QImage memory when image is deleted.
parent 3f02436b
......@@ -1746,6 +1746,11 @@ int Screen::getHistLines() const
return _history->getLines();
}
int Screen::getHistMaxLines() const
{
return _history->getMaxLines();
}
void Screen::setScroll(const HistoryType &t, bool copyPreviousScroll)
{
clearSelection();
......
......@@ -412,6 +412,7 @@ public:
/** Return the number of lines in the history buffer. */
int getHistLines() const;
int getHistMaxLines() const;
/**
* Sets the type of storage used to keep lines in the history.
* If @p copyPreviousScroll is true then the contents of the previous
......
......@@ -1300,6 +1300,11 @@ void Vt102Emulation::processToken(int token, int p, int q)
/* clang-format on */
}
void delete_func(void *p)
{
delete (QByteArray *)p;
}
void Vt102Emulation::processGraphicsToken(int tokenSize)
{
QString value = QString::fromUcs4(&tokenBuffer[3], tokenSize - 4);
......@@ -1411,7 +1416,7 @@ void Vt102Emulation::processGraphicsToken(int tokenSize)
if (!out) {
out = new QByteArray(imageData.constData(), imageData.size());
}
image = new QImage((unsigned char *)out->constData(), 0 + keys['s'], 0 + keys['v'], 0 + keys['s'] * keys['f'] / 8, format);
image = new QImage((unsigned char *)out->constData(), 0 + keys['s'], 0 + keys['v'], 0 + keys['s'] * keys['f'] / 8, format, delete_func, out);
} else {
image = new QImage();
if (!out) {
......@@ -1451,6 +1456,10 @@ void Vt102Emulation::processGraphicsToken(int tokenSize)
}
if (image) {
QPixmap pixmap = QPixmap::fromImage(*image);
if (!keys['i']) {
// We did not save the image, so delete it
delete image;
}
if (keys['x'] || keys['y'] || keys['w'] || keys['h']) {
int w = keys['w'] ? keys['w'] : pixmap.width() - keys['x'];
int h = keys['h'] ? keys['h'] : pixmap.height() - keys['y'];
......
......@@ -3048,8 +3048,10 @@ void TerminalDisplay::addPlacement(TerminalGraphicsPlacement_t *p)
// remove placement with the same id and pid, if pid is non zero
if (p->pid >= 0 && p->id >= 0)
for (i = 0; i < _graphicsPlacements->size(); i++) {
if (p->id == (*_graphicsPlacements)[i]->id && p->pid == (*_graphicsPlacements)[i]->pid) {
TerminalGraphicsPlacement_t *placement = (*_graphicsPlacements)[i];
if (p->id == placement->id && p->pid == placement->pid) {
_graphicsPlacements->remove(i);
delete placement;
break;
}
}
......@@ -3085,9 +3087,15 @@ TerminalGraphicsPlacement_t *TerminalDisplay::getGraphicsPlacement(int i)
void TerminalDisplay::scrollUpVisiblePlacements(int n)
{
int histMaxLines = _screenWindow->screen()->getHistMaxLines();
for (int i = _graphicsPlacements->size() - 1; i >= 0; i--) {
if ((*_graphicsPlacements)[i]->scrolling) {
(*_graphicsPlacements)[i]->row -= n;
TerminalGraphicsPlacement_t *placement = (*_graphicsPlacements)[i];
if (placement->scrolling) {
placement->row -= n;
if (placement->row + placement->rows < -histMaxLines) {
_graphicsPlacements->remove(i);
delete placement;
}
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment