NetBSD-Bugs archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

lib/60995: libedit: allocate display rows on demand



>Number:         60995
>Category:       lib
>Synopsis:       libedit: allocate display rows on demand
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    lib-bug-people
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 12 18:55:00 +0000 2026
>Originator:     Hua Zhong
>Release:        
>Organization:
Zhong
>Environment:
Alma EL9
>Description:
libedit (unlike readline) allocates a buffer for the entire terminal width * height from the beginning, which is often wasteful as most of the time very few rows are needed. This change intrduces delayed allocation for row buffers. Initially, the row-pointer array is allocated with NULL pointers. When we need row N, we allocate all row buffers up to that row. This significantly reduces memory usage of libedit.

>How-To-Repeat:
You can set a very large terminal width (e.g., through stty) and observe huge memory usage from libedit.
>Fix:
Subject: libedit: allocate display rows on demand

libedit (unlike readline) allocates a buffer for the entire terminal width *
height from the beginning, which is often wasteful as most of the time very
few rows are needed. This change intrduces delayed allocation for row buffers.
Initially, the row-pointer array is allocated with NULL pointers. When we need
row N, we allocate all row buffers up to that row.

To keep things simple, we exit with status 2 when row buffer allocation fails
(which is also the readline behavior). This can be handled more gracefully with
more code changes.

--- a/lib/libedit/terminal.h
+++ b/lib/libedit/terminal.h
@@ -50,6 +50,7 @@
 typedef struct {
 	const char *t_name;		/* the terminal name	*/
 	coord_t	  t_size;		/* # lines and cols	*/
+	int	  t_allocated_rows;	/* Row capacity in both images */
 	int	  t_flags;
 #define	TERM_CAN_INSERT		0x001	/* Has insert cap	*/
 #define	TERM_CAN_DELETE		0x002	/* Has delete cap	*/
@@ -90,6 +91,7 @@
 libedit_private void	terminal_beep(EditLine *);
 libedit_private int	terminal_change_size(EditLine *, int, int);
 libedit_private int	terminal_get_size(EditLine *, int *, int *);
+libedit_private void	terminal_alloc_rows(EditLine *, int);
 libedit_private int	terminal_init(EditLine *);
 libedit_private void	terminal_bind_arrow(EditLine *);
 libedit_private void	terminal_print_arrow(EditLine *, const wchar_t *);
--- a/lib/libedit/terminal.c
+++ b/lib/libedit/terminal.c
@@ -413,27 +413,47 @@
 	return 0;
 }
 
+/* terminal_alloc_buffer():
+ *	Allocate a zeroed row-pointer array, leaving rows unallocated.
+ */
 static wint_t **
 terminal_alloc_buffer(EditLine *el)
 {
-	wint_t **b;
+	coord_t *c = &el->el_terminal.t_size;
+
+	return el_calloc((size_t)(c->v + 1), sizeof(wint_t *));
+}
+
+/* terminal_alloc_rows():
+ *	Allocate row buffers up to and including the specified row,
+ *	if not already allocated.
+ */
+libedit_private void
+terminal_alloc_rows(EditLine *el, int row)
+{
 	coord_t *c = &el->el_terminal.t_size;
 	int i;
 
-	b =  el_calloc((size_t)(c->v + 1), sizeof(*b));
-	if (b == NULL)
-		return NULL;
-	for (i = 0; i < c->v; i++) {
-		b[i] = el_calloc((size_t)(c->h + 1), sizeof(**b));
-		if (b[i] == NULL) {
-			while (--i >= 0)
-				el_free(b[i]);
-			el_free(b);
-			return NULL;
-		}
-	}
-	b[c->v] = NULL;
-	return b;
+	if (row < 0 || row >= c->v) {
+		(void) fprintf(el->el_errfile,
+		    "libedit: invalid display row %d\n", row);
+		(void) fflush(el->el_errfile);
+		abort();
+	}
+	while (el->el_terminal.t_allocated_rows <= row) {
+		i = el->el_terminal.t_allocated_rows;
+		el->el_display[i] = el_calloc((size_t)(c->h + 1),
+		    sizeof(**el->el_display));
+		el->el_vdisplay[i] = el_calloc((size_t)(c->h + 1),
+		    sizeof(**el->el_vdisplay));
+		if (el->el_display[i] == NULL || el->el_vdisplay[i] == NULL) {
+			(void) fprintf(el->el_errfile,
+			    "libedit: out of memory allocating display row\n");
+			(void) fflush(el->el_errfile);
+			exit(2);
+		}
+		el->el_terminal.t_allocated_rows++;
+	}
 }
 
 static void
@@ -480,6 +500,7 @@
 {
 	terminal_free_buffer(&el->el_display);
 	terminal_free_buffer(&el->el_vdisplay);
+	el->el_terminal.t_allocated_rows = 0;
 }
 
 
@@ -559,6 +580,8 @@
 				terminal_tputs(el, tgoto(Str(T_RI), del, del),
 				    del);
 			else {
+				/* A resize may leave this row unallocated. */
+				terminal_alloc_rows(el, el->el_cursor.v);
 					/* if I can do tabs, use them */
 				if (EL_CAN_TAB) {
 					if ((el->el_cursor.h & 0370) !=
@@ -652,7 +675,8 @@
 				/* force the wrap to avoid the "magic"
 				 * situation */
 				wchar_t c;
-				if ((c = el->el_display[el->el_cursor.v]
+				if (el->el_display[el->el_cursor.v] != NULL &&
+				    (c = el->el_display[el->el_cursor.v]
 				    [el->el_cursor.h]) != '\0') {
 					terminal_overwrite(el, &c, (size_t)1);
 					while (el->el_display[el->el_cursor.v]
--- a/lib/libedit/refresh.c
+++ b/lib/libedit/refresh.c
@@ -170,6 +170,7 @@
 	c = literal_add(el, begin, end, &w);
 	if (c == 0 || w < 0)
 		return;
+	terminal_alloc_rows(el, cur->v);
 	el->el_vdisplay[cur->v][cur->h] = c;
 
 	i = w;
@@ -203,6 +204,7 @@
 	while (shift && (cur->h + w > sizeh))
 	    re_putc(el, ' ', 1);
 
+	terminal_alloc_rows(el, cur->v);
 	el->el_vdisplay[cur->v][cur->h] = c;
 	/* assumes !shift is only used for single-column chars */
 	i = w;
@@ -1102,6 +1104,7 @@
 	    re_fastputc(el, ' ');
 
 	terminal__putc(el, c);
+	terminal_alloc_rows(el, el->el_cursor.v);
 	el->el_display[el->el_cursor.v][el->el_cursor.h++] = c;
 	while (--w > 0)
 		el->el_display[el->el_cursor.v][el->el_cursor.h++]
@@ -1127,6 +1130,7 @@
 			el->el_display[i - 1] = lastline;
 		} else {
 			el->el_cursor.v++;
+			terminal_alloc_rows(el, el->el_cursor.v);
 			lastline = el->el_display[++el->el_refresh.r_oldcv];
 		}
 		re__copy_and_pad((wchar_t *)lastline, L"",
@@ -1202,7 +1206,7 @@
 
 	el->el_cursor.v = 0;
 	el->el_cursor.h = 0;
-	for (i = 0; i < el->el_terminal.t_size.v; i++)
+	for (i = 0; i < el->el_terminal.t_allocated_rows; i++)
 		el->el_display[i][0] = '\0';
 	el->el_refresh.r_oldcv = 0;
 }




Home | Main Index | Thread Index | Old Index