Skip to content

Commit

Permalink
removed the memory based limit of undo/redo as it was actually causin…
Browse files Browse the repository at this point in the history
…g some integer underflow bugs

this resolves issue #23 and also makes undo/redo slightly more efficient to track
  • Loading branch information
eteran committed Jan 15, 2018
1 parent 0a51118 commit 7d9ef6d
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 29 deletions.
21 changes: 1 addition & 20 deletions source/DocumentWidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,22 +1546,11 @@ void DocumentWidget::addUndoItem(UndoInfo &&undo) {
// Add the item to the beginning of the list
undo_.emplace_front(std::move(undo));

// Increment the operation and memory counts
undoMemUsed_ += undo.oldText.size();

// Trim the list if it exceeds any of the limits
if (undo_.size() > UNDO_OP_LIMIT) {
trimUndoList(UNDO_OP_TRIMTO);
}

if (undoMemUsed_ > UNDO_WORRY_LIMIT) {
trimUndoList(UNDO_WORRY_TRIMTO);
}

if (undoMemUsed_ > UNDO_PURGE_LIMIT) {
trimUndoList(UNDO_PURGE_TRIMTO);
}

if(MainWindow *window = MainWindow::fromDocument(this)) {
window->undoAvailable(undo_.size() != 0);
}
Expand Down Expand Up @@ -1589,11 +1578,6 @@ void DocumentWidget::removeUndoItem() {
return;
}

const UndoInfo &undo = undo_.front();

// Decrement the operation and memory counts
undoMemUsed_ -= undo.oldText.size();

// Remove and free the item
undo_.pop_front();

Expand Down Expand Up @@ -1636,10 +1620,7 @@ void DocumentWidget::trimUndoList(size_t maxLength) {
}

// Trim off all subsequent entries
while(it != undo_.end()) {
undoMemUsed_ -= it->oldText.size();
it = undo_.erase(it);
}
undo_.erase(it, undo_.end());
}

void DocumentWidget::Undo() {
Expand Down
1 change: 0 additions & 1 deletion source/DocumentWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,6 @@ class DocumentWidget : public QWidget {
int autoSaveCharCount_ = 0; // count of single characters typed since last backup file generated
int autoSaveOpCount_ = 0; // count of editing operations ""
size_t nMarks_ = 0; // number of active bookmarks
size_t undoMemUsed_ = 0; // amount of memory (in bytes) dedicated to the undo list
mode_t mode_ = 0; // permissions of file being edited
time_t lastModTime_ = 0; // time of last modification to file
uid_t uid_ = 0; // last recorded user id of the file
Expand Down
9 changes: 1 addition & 8 deletions source/UndoInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,8 @@
amounts of memory. These tuning parameters determine how much undo infor-
mation is retained. Normally, the list is kept between UNDO_OP_LIMIT and
UNDO_OP_TRIMTO in length (when the list reaches UNDO_OP_LIMIT, it is
trimmed to UNDO_OP_TRIMTO then allowed to grow back to UNDO_OP_LIMIT).
When there are very large amounts of saved text held in the list,
UNDO_WORRY_LIMIT and UNDO_PURGE_LIMIT take over and cause the list to
be trimmed back further to keep its size down. */
trimmed to UNDO_OP_TRIMTO then allowed to grow back to UNDO_OP_LIMIT). */

constexpr auto UNDO_PURGE_LIMIT = 15000000u; // If undo list gets this large (in bytes), trim it to length of UNDO_PURGE_TRIMTO
constexpr auto UNDO_PURGE_TRIMTO = 1u; // Amount to trim the undo list in a purge
constexpr auto UNDO_WORRY_LIMIT = 2000000u; // If undo list gets this large (in bytes), trim it to length of UNDO_WORRY_TRIMTO
constexpr auto UNDO_WORRY_TRIMTO = 5u; // Amount to trim the undo list when memory use begins to get serious
constexpr auto UNDO_OP_LIMIT = 400u; // normal limit for length of undo list
constexpr auto UNDO_OP_TRIMTO = 200u; // size undo list is normally trimmed to when it exceeds UNDO_OP_TRIMTO in length

Expand Down

0 comments on commit 7d9ef6d

Please sign in to comment.