From 43772cbbb0a8e590094646fea191e18632ff5f8a Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Sat, 23 Oct 2021 12:40:02 +0900 Subject: [PATCH] terminal: make scrollback more efficient? --- apps/terminal.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/apps/terminal.c b/apps/terminal.c index 1fc959f6..bb8330fd 100644 --- a/apps/terminal.c +++ b/apps/terminal.c @@ -170,7 +170,7 @@ struct scrollback_row { term_cell_t cells[]; }; -#define MAX_SCROLLBACK 10240 +#define MAX_SCROLLBACK 1000 static list_t * scrollback_list = NULL; static int scrollback_offset = 0; @@ -1099,20 +1099,34 @@ static int is_wide(uint32_t codepoint) { static void save_scrollback(void) { /* If the scrollback is already full, remove the oldest element. */ + struct scrollback_row * row = NULL; + node_t * n = NULL; + if (scrollback_list->length == MAX_SCROLLBACK) { - node_t * n = list_dequeue(scrollback_list); - free(n->value); - free(n); + n = list_dequeue(scrollback_list); + row = n->value; + if (row->width < term_width) { + free(row); + row = NULL; + } + } + + if (!row) { + row = malloc(sizeof(struct scrollback_row) + sizeof(term_cell_t) * term_width); + row->width = term_width; + } + + if (!n) { + list_insert(scrollback_list, row); + } else { + n->value = row; + list_append(scrollback_list, n); } - struct scrollback_row * row = malloc(sizeof(struct scrollback_row) + sizeof(term_cell_t) * term_width + 20); - row->width = term_width; for (int i = 0; i < term_width; ++i) { term_cell_t * cell = (term_cell_t *)((uintptr_t)term_buffer + (i) * sizeof(term_cell_t)); memcpy(&row->cells[i], cell, sizeof(term_cell_t)); } - - list_insert(scrollback_list, row); } /* Draw the scrollback. */