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 introduces 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. Display buffer allocation failures are reported through existing API errors. A subsequent API call such as el_gets() retries the allocation if needed. --- 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; /* Number of allocated rows */ 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 int 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 @@ -48,6 +48,7 @@ */ #include #include +#include #include #include #include @@ -300,6 +301,8 @@ if (el->el_terminal.t_val == NULL) goto out; (void) terminal_set(el, NULL); + if (el->el_refresh.r_error) + goto out; terminal_init_arrow(el); return 0; out: @@ -401,39 +404,56 @@ static int terminal_rebuffer_display(EditLine *el) { - coord_t *c = &el->el_terminal.t_size; - - terminal_free_display(el); - - c->h = Val(T_co); - c->v = Val(T_li); - - if (terminal_alloc_display(el) == -1) - return -1; - return 0; + return terminal_alloc_display(el); } +/* terminal_alloc_buffer(): + * Allocate a zeroed row-pointer array, leaving rows unallocated. + */ static wint_t ** -terminal_alloc_buffer(EditLine *el) +terminal_alloc_buffer(int rows) +{ + return el_calloc((size_t)(rows + 1), sizeof(wint_t *)); +} + +/* terminal_alloc_rows(): + * Allocate row buffers up to and including the specified row, + * if not already allocated. Return -1 on allocation failure. + */ +libedit_private int +terminal_alloc_rows(EditLine *el, int row) { - wint_t **b; coord_t *c = &el->el_terminal.t_size; + wint_t *real, *virt; 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; + if (el->el_refresh.r_error) + return -1; + + 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; + real = el_calloc((size_t)(c->h + 1), sizeof(*real)); + if (real == NULL) + goto fail; + virt = el_calloc((size_t)(c->h + 1), sizeof(*virt)); + if (virt == NULL) { + el_free(real); + goto fail; } + el->el_display[i] = real; + el->el_vdisplay[i] = virt; + el->el_terminal.t_allocated_rows++; } - b[c->v] = NULL; - return b; + return 0; +fail: + el->el_refresh.r_error = errno = ENOMEM; + return -1; } static void @@ -459,15 +479,25 @@ static int terminal_alloc_display(EditLine *el) { - el->el_display = terminal_alloc_buffer(el); - if (el->el_display == NULL) - goto done; - el->el_vdisplay = terminal_alloc_buffer(el); - if (el->el_vdisplay == NULL) - goto done; - return 0; -done: + wint_t **display, **vdisplay; + + display = terminal_alloc_buffer(Val(T_li)); + if (display == NULL) + goto fail; + vdisplay = terminal_alloc_buffer(Val(T_li)); + if (vdisplay == NULL) { + el_free(display); + goto fail; + } + /* Keep the old display and dimensions until both arrays are ready. */ terminal_free_display(el); + el->el_terminal.t_size.h = Val(T_co); + el->el_terminal.t_size.v = Val(T_li); + el->el_display = display; + el->el_vdisplay = vdisplay; + return 0; +fail: + el->el_refresh.r_error = errno = ENOMEM; return -1; } @@ -480,6 +510,7 @@ { terminal_free_buffer(&el->el_display); terminal_free_buffer(&el->el_vdisplay); + el->el_terminal.t_allocated_rows = 0; } @@ -559,6 +590,9 @@ terminal_tputs(el, tgoto(Str(T_RI), del, del), del); else { + /* A resize may leave this row unallocated. */ + if (terminal_alloc_rows(el, el->el_cursor.v) == -1) + return; /* if I can do tabs, use them */ if (EL_CAN_TAB) { if ((el->el_cursor.h & 0370) != @@ -652,7 +686,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] @@ -836,7 +871,7 @@ libedit_private int terminal_set(EditLine *el, const char *term) { - int i; + int i, rv; char buf[TC_BUFSIZE]; char *area; const struct termcapstr *t; @@ -902,16 +937,14 @@ if (Val(T_li) < 1) Val(T_li) = 24; - el->el_terminal.t_size.v = Val(T_co); - el->el_terminal.t_size.h = Val(T_li); - terminal_setflags(el); /* get the correct window size */ (void) terminal_get_size(el, &lins, &cols); - if (terminal_change_size(el, lins, cols) == -1) - return -1; + rv = terminal_change_size(el, lins, cols); (void) sigprocmask(SIG_SETMASK, &oset, NULL); + if (rv == -1) + return -1; terminal_bind_arrow(el); el->el_terminal.t_name = term; return i <= 0 ? -1 : 0; @@ -969,8 +1002,11 @@ Val(T_li) = (lins < 1) ? 24 : lins; /* re-make display buffers */ - if (terminal_rebuffer_display(el) == -1) + if (terminal_rebuffer_display(el) == -1) { + Val(T_co) = el->el_terminal.t_size.h; + Val(T_li) = el->el_terminal.t_size.v; return -1; + } re_clear_display(el); el->el_cursor = cur; return 0; @@ -1376,15 +1412,8 @@ return -1; } el->el_terminal.t_val[tv - tval] = (int) i; - i = 0; - if (tv == &tval[T_co]) { - el->el_terminal.t_size.v = Val(T_co); - i++; - } else if (tv == &tval[T_li]) { - el->el_terminal.t_size.h = Val(T_li); - i++; - } - if (i && terminal_change_size(el, Val(T_li), Val(T_co)) == -1) + if ((tv == &tval[T_co] || tv == &tval[T_li]) && + terminal_change_size(el, Val(T_li), Val(T_co)) == -1) return -1; return 0; } --- a/lib/libedit/refresh.h +++ b/lib/libedit/refresh.h @@ -44,6 +44,7 @@ coord_t r_cursor; /* Refresh cursor position */ int r_oldcv; /* Vertical locations */ int r_newcv; + int r_error; /* Display error propagated to the caller */ } el_refresh_t; libedit_private void re_putc(EditLine *, wint_t, int); --- a/lib/libedit/refresh.c +++ b/lib/libedit/refresh.c @@ -95,6 +95,9 @@ static void re_nextline(EditLine *el) { + if (el->el_refresh.r_error) + return; + el->el_refresh.r_cursor.h = 0; /* reset it. */ /* @@ -131,6 +134,8 @@ case CHTYPE_TAB: /* expand the tab */ for (;;) { re_putc(el, ' ', 1); + if (el->el_refresh.r_error) + return; if ((el->el_refresh.r_cursor.h & 07) == 0) break; /* go until tab stop */ } @@ -170,6 +175,8 @@ c = literal_add(el, begin, end, &w); if (c == 0 || w < 0) return; + if (terminal_alloc_rows(el, cur->v) == -1) + return; el->el_vdisplay[cur->v][cur->h] = c; i = w; @@ -200,9 +207,14 @@ if (w == -1) w = 0; - while (shift && (cur->h + w > sizeh)) - re_putc(el, ' ', 1); + while (shift && (cur->h + w > sizeh)) { + re_putc(el, ' ', 1); + if (el->el_refresh.r_error) + return; + } + if (terminal_alloc_rows(el, cur->v) == -1) + return; el->el_vdisplay[cur->v][cur->h] = c; /* assumes !shift is only used for single-column chars */ i = w; @@ -322,6 +334,8 @@ } re_putc(el, '\0', 0); /* make line ended with NUL, no cursor shift */ + if (el->el_refresh.r_error) + return; el->el_refresh.r_newcv = el->el_refresh.r_cursor.v; @@ -1098,9 +1112,14 @@ int w; w = wcwidth(c); - while (w > 1 && el->el_cursor.h + w > el->el_terminal.t_size.h) - re_fastputc(el, ' '); + while (w > 1 && el->el_cursor.h + w > el->el_terminal.t_size.h) { + re_fastputc(el, ' '); + if (el->el_refresh.r_error) + return; + } + if (terminal_alloc_rows(el, el->el_cursor.v) == -1) + return; terminal__putc(el, c); el->el_display[el->el_cursor.v][el->el_cursor.h++] = c; while (--w > 0) @@ -1126,6 +1145,9 @@ el->el_display[i - 1] = lastline; } else { + /* Allocate before advancing to the next row. */ + if (terminal_alloc_rows(el, el->el_cursor.v + 1) == -1) + return; el->el_cursor.v++; lastline = el->el_display[++el->el_refresh.r_oldcv]; } @@ -1202,7 +1224,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; } --- a/lib/libedit/read.c +++ b/lib/libedit/read.c @@ -288,6 +288,16 @@ el->el_signal->sig_no = 0; while ((num_read = read(el->el_infd, cbuf + cbp, (size_t)1)) == -1) { int e = errno; + + /* + * An interrupting signal handler may have failed to allocate + * display buffers during resize or redraw. Stop before retrying. + */ + if (el->el_refresh.r_error) { + errno = el->el_refresh.r_error; + *cp = L'\0'; + return -1; + } switch (el->el_signal->sig_no) { case SIGCONT: el_wset(el, EL_REFRESH); @@ -382,6 +392,14 @@ struct macros *ma = &el->el_read->macros; int num_read; + /* + * Stop reading after a display error, including in nested prompts + * and searches, so el_wgets() can report it to the caller. + */ + if (el->el_refresh.r_error) { + errno = el->el_refresh.r_error; + return -1; + } terminal__flush(el); for (;;) { if (ma->level < 0) @@ -421,6 +439,7 @@ libedit_private void read_prepare(EditLine *el) { + el->el_refresh.r_error = 0; if (el->el_flags & HANDLE_SIGNALS) sig_set(el); if (el->el_flags & NO_TTY) @@ -506,8 +525,11 @@ } #endif /* FIONREAD */ - if ((el->el_flags & UNBUFFERED) == 0) + /* Retry a previous failed read_prepare() in unbuffered mode. */ + if ((el->el_flags & UNBUFFERED) == 0 || el->el_refresh.r_error) read_prepare(el); + if (el->el_refresh.r_error) + goto display_error; if (el->el_flags & EDIT_DISABLED) { if ((el->el_flags & UNBUFFERED) == 0) @@ -603,6 +625,8 @@ break; } + if (el->el_refresh.r_error) + goto display_error; terminal__flush(el); /* flush any buffered output */ /* make sure the tty is set up correctly */ if ((el->el_flags & UNBUFFERED) == 0) { @@ -620,4 +644,11 @@ return NULL; } else return el->el_line.buffer; + +display_error: + if ((el->el_flags & UNBUFFERED) == 0) + read_finish(el); + *nread = -1; + errno = el->el_refresh.r_error; + return NULL; } --- a/lib/libedit/el.c +++ b/lib/libedit/el.c @@ -100,6 +100,8 @@ el->el_flags = flags; if (terminal_init(el) == -1) { + el_free(el->el_scratch.cbuff); + el_free(el->el_scratch.wbuff); el_free(el->el_prog); el_free(el); return NULL; @@ -172,6 +174,7 @@ { tty_cookedmode(el); + el->el_refresh.r_error = 0; ch_reset(el); /* XXX: Do we want that? */ } @@ -335,6 +338,10 @@ if (rv && !(el->el_flags & UNBUFFERED)) { el->el_flags |= UNBUFFERED; read_prepare(el); + if (el->el_refresh.r_error) { + rv = -1; + break; + } } else if (!rv && (el->el_flags & UNBUFFERED)) { el->el_flags &= ~UNBUFFERED; read_finish(el); @@ -381,9 +388,12 @@ } case EL_REFRESH: + el->el_refresh.r_error = 0; re_clear_display(el); re_refresh(el); terminal__flush(el); + if (el->el_refresh.r_error) + rv = -1; break; case EL_WORDCHARS: --- a/lib/libedit/eln.c +++ b/lib/libedit/eln.c @@ -253,10 +253,7 @@ } case EL_REFRESH: - re_clear_display(el); - re_refresh(el); - terminal__flush(el); - ret = 0; + ret = el_wset(el, op); break; default: