From 940ceebd4da5e69caff0d2e0c69d89eba278bc84 Mon Sep 17 00:00:00 2001 From: Gustavo Carneiro Date: Fri, 17 Jul 2020 00:08:17 -0300 Subject: [PATCH] Move CompactHistoryBlockClass to a new file. --- src/CMakeLists.txt | 1 + src/CompactHistoryBlock.cpp | 81 +++++++++++++++++++++++++++++++++++++ src/CompactHistoryBlock.h | 60 +++++++++++++++++++++++++++ src/History.cpp | 23 ----------- src/History.h | 51 +---------------------- 5 files changed, 143 insertions(+), 73 deletions(-) create mode 100644 src/CompactHistoryBlock.cpp create mode 100644 src/CompactHistoryBlock.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 3d85caca..51c6df53 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -69,6 +69,7 @@ set(konsoleprivate_SRCS ${sessionadaptors_SRCS} HistoryScrollFile.cpp HistoryScrollNone.cpp CharacterFormat.cpp + CompactHistoryBlock.cpp HistorySizeDialog.cpp widgets/HistorySizeWidget.cpp widgets/IncrementalSearchBar.cpp diff --git a/src/CompactHistoryBlock.cpp b/src/CompactHistoryBlock.cpp new file mode 100644 index 00000000..27a42ac1 --- /dev/null +++ b/src/CompactHistoryBlock.cpp @@ -0,0 +1,81 @@ +/* + This file is part of Konsole, an X terminal. + Copyright 1997,1998 by Lars Doelle + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +// Own +#include "CompactHistoryBlock.h" + +using namespace Konsole; + +CompactHistoryBlock::CompactHistoryBlock() : + _blockLength(4096 * 64), // 256kb + _head(static_cast(mmap(nullptr, _blockLength, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0))), + _tail(nullptr), + _blockStart(nullptr), + _allocCount(0) +{ + Q_ASSERT(_head != MAP_FAILED); + _tail = _blockStart = _head; +} + +CompactHistoryBlock::~CompactHistoryBlock() +{ + //free(_blockStart); + munmap(_blockStart, _blockLength); +} + +unsigned int CompactHistoryBlock::remaining() +{ + return _blockStart + _blockLength - _tail; +} + +unsigned CompactHistoryBlock::length() +{ + return _blockLength; +} + +bool CompactHistoryBlock::contains(void *addr) +{ + return addr >= _blockStart && addr < (_blockStart + _blockLength); +} + +bool CompactHistoryBlock::isInUse() +{ + return _allocCount != 0; +} + +void *CompactHistoryBlock::allocate(size_t size) +{ + Q_ASSERT(size > 0); + if (_tail - _blockStart + size > _blockLength) { + return nullptr; + } + + void *block = _tail; + _tail += size; + ////qDebug() << "allocated " << length << " bytes at address " << block; + _allocCount++; + return block; +} + +void CompactHistoryBlock::deallocate() +{ + _allocCount--; + Q_ASSERT(_allocCount >= 0); +} diff --git a/src/CompactHistoryBlock.h b/src/CompactHistoryBlock.h new file mode 100644 index 00000000..75ac8495 --- /dev/null +++ b/src/CompactHistoryBlock.h @@ -0,0 +1,60 @@ +/* + This file is part of Konsole, an X terminal. + Copyright 1997,1998 by Lars Doelle + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. +*/ + +#ifndef COMPACTHISTORYBLOCK_H +#define COMPACTHISTORYBLOCK_H + +// Konsole +#include "Character.h" + +// System +#include + +namespace Konsole +{ + +class CompactHistoryBlock +{ +public: + CompactHistoryBlock(); + + virtual ~CompactHistoryBlock(); + + virtual unsigned int remaining(); + + virtual unsigned length(); + + virtual void *allocate(size_t size); + virtual bool contains(void *addr); + + virtual void deallocate(); + virtual bool isInUse(); + +private: + size_t _blockLength; + quint8 *_head; + quint8 *_tail; + quint8 *_blockStart; + int _allocCount; +}; + +} + +#endif diff --git a/src/History.cpp b/src/History.cpp index e20d8c8d..37d8a29a 100644 --- a/src/History.cpp +++ b/src/History.cpp @@ -60,29 +60,6 @@ using namespace Konsole; at constant costs. */ -//////////////////////////////////////////////////////////////// -// Compact History Scroll ////////////////////////////////////// -//////////////////////////////////////////////////////////////// -void *CompactHistoryBlock::allocate(size_t size) -{ - Q_ASSERT(size > 0); - if (_tail - _blockStart + size > _blockLength) { - return nullptr; - } - - void *block = _tail; - _tail += size; - ////qDebug() << "allocated " << length << " bytes at address " << block; - _allocCount++; - return block; -} - -void CompactHistoryBlock::deallocate() -{ - _allocCount--; - Q_ASSERT(_allocCount >= 0); -} - void *CompactHistoryBlockList::allocate(size_t size) { CompactHistoryBlock *block; diff --git a/src/History.h b/src/History.h index 69bfb393..18b7ae81 100644 --- a/src/History.h +++ b/src/History.h @@ -37,6 +37,7 @@ #include "HistoryScrollFile.h" #include "HistoryScrollNone.h" #include "CharacterFormat.h" +#include "CompactHistoryBlock.h" // Konsole #include "Character.h" @@ -50,56 +51,6 @@ namespace Konsole { ////////////////////////////////////////////////////////////////////// typedef QVector TextLine; -class CompactHistoryBlock -{ -public: - CompactHistoryBlock() : - _blockLength(4096 * 64), // 256kb - _head(static_cast(mmap(nullptr, _blockLength, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0))), - _tail(nullptr), - _blockStart(nullptr), - _allocCount(0) - { - Q_ASSERT(_head != MAP_FAILED); - _tail = _blockStart = _head; - } - - virtual ~CompactHistoryBlock() - { - //free(_blockStart); - munmap(_blockStart, _blockLength); - } - - virtual unsigned int remaining() - { - return _blockStart + _blockLength - _tail; - } - - virtual unsigned length() - { - return _blockLength; - } - - virtual void *allocate(size_t size); - virtual bool contains(void *addr) - { - return addr >= _blockStart && addr < (_blockStart + _blockLength); - } - - virtual void deallocate(); - virtual bool isInUse() - { - return _allocCount != 0; - } - -private: - size_t _blockLength; - quint8 *_head; - quint8 *_tail; - quint8 *_blockStart; - int _allocCount; -}; - class CompactHistoryBlockList { public: