This is an automated email from the git hooks/post-receive script.

git pushed a commit to branch tymux-10-quiet-head
in repository terminology.

View the commit online.

commit 20cb963acfc15b17db0daed6d0a05ae6783bbb45
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 18 21:46:45 2026 -0600

    termpty: let backlog entries point at cells it does not own
    
    The tymux client will build its scrollback from read-only memfd pages
    mapped from the daemon, and plug each daemon line in as an ordinary
    backlog entry so the existing wrap and beacon code keeps reflowing it.
    Copying the pages into termpty-owned rows would defeat sharing them.
    
    Mark such entries with a flag in Termsave (the unused z bit, now ext)
    rather than keeping a separate store: row lookup, the beacon and reflow
    then need no second path. termpty never frees, reallocates or writes
    flagged cells; growing one copies it first, and the writeable row
    accessor returns NULL for them.
    
    Cursor moves read these rows through the read accessor instead of
    asserting that a writeable row exists, and link detection skips the
    highlight when it cannot write the row. A click while scrolled back in a
    tymux terminal reached that path.
---
 src/bin/backlog.c    | 101 ++++++++++++++++++++++++++++++++++++++++++++++++---
 src/bin/backlog.h    |  20 ++++++++++
 src/bin/termpty.c    |  79 ++++++++++++++++++++++++++++++++++++----
 src/bin/termpty.h    |   4 +-
 src/bin/termptyext.c |  11 ++++--
 src/bin/termptyops.c |  25 ++++++++-----
 src/bin/tytest.c     |   1 +
 src/bin/unit_tests.h |   1 +
 8 files changed, 213 insertions(+), 29 deletions(-)

diff --git a/src/bin/backlog.c b/src/bin/backlog.c
index d57ea5bd..4d7cc520 100644
--- a/src/bin/backlog.c
+++ b/src/bin/backlog.c
@@ -61,7 +61,7 @@ termpty_save_new(Termpty *ty, Termsave *ts, int w)
     * settles at the longest line it has held and stops allocating. Rows stay
     * trimmed to their content, so backlog memory does not scale with
     * scrollback times columns. */
-   if (ts->cells && !ts->comp && (ts->cap >= (unsigned int)w))
+   if (ts->cells && !ts->comp && !ts->ext && (ts->cap >= (unsigned int)w))
      {
         termpty_cells_refs_release(ty, ts->cells, ts->w);
         /* Zeroing is only needed when the caller's copy does bookkeeping:
@@ -95,7 +95,21 @@ termpty_save_expand(Termpty *ty, Termsave *ts, Termcell *cells, size_t delta)
    Termcell *newcells;
    size_t need = ts->w + delta;
 
-   if (need > ts->cap)
+   if (ts->ext)
+     {
+        /* Cells termpty does not own are never written: grow a copy. */
+        size_t cap = MAX(need, (size_t)ts->w * 2);
+
+        newcells = malloc(cap * sizeof(Termcell));
+        if (!newcells)
+          return NULL;
+        memcpy(newcells, ts->cells, ts->w * sizeof(Termcell));
+        _accounting_change(cap * sizeof(Termcell));
+        ts->cap = cap;
+        ts->cells = newcells;
+        ts->ext = 0;
+     }
+   else if (need > ts->cap)
      {
         /* grown geometrically, else each wrapped row copies the line whole */
         size_t cap = MAX(need, (size_t)ts->cap * 2);
@@ -128,11 +142,14 @@ termpty_save_free(Termpty *ty, Termsave *ts)
    if (ts->comp) ts_comp--;
    else ts_uncomp--;
    ts_freeops++;
-   termpty_cells_refs_release(ty, ts->cells, ts->w);
-
-   free(ts->cells);
+   if (!ts->ext)
+     {
+        termpty_cells_refs_release(ty, ts->cells, ts->w);
+        free(ts->cells);
+        _accounting_change((-1) * (int64_t)(ts->cap * sizeof(Termcell)));
+     }
    ts->cells = NULL;
-   _accounting_change((-1) * (int64_t)(ts->cap * sizeof(Termcell)));
+   ts->ext = 0;
    ts->w = 0;
    ts->cap = 0;
 }
@@ -238,3 +255,75 @@ end:
    ty->backlog_beacon.screen_y = 0;
    ty->backlog_beacon.backlog_y = 0;
 }
+
+void
+termpty_backlog_external_push(Termpty *ty, Termcell *cells, ssize_t w)
+{
+   Termsave *ts;
+
+   EINA_SAFETY_ON_NULL_RETURN(cells);
+   if (ty->backsize == 0)
+     return;
+
+   ts = BACKLOG_ROW_GET(ty, 0);
+   termpty_save_free(ty, ts);
+   ts->cells = cells;
+   ts->w = w;
+   ts->ext = 1;
+   ty->backpos++;
+   if (ty->backpos >= ty->backsize)
+     ty->backpos = 0;
+
+   ty->backlog_beacon.screen_y += termpty_backlog_entry_rows(ty, w);
+   ty->backlog_beacon.backlog_y++;
+   if (ty->backlog_beacon.backlog_y >= (int)ty->backsize)
+     {
+        ty->backlog_beacon.screen_y = 0;
+        ty->backlog_beacon.backlog_y = 0;
+     }
+}
+
+void
+termpty_backlog_external_set(Termpty *ty, int backlog_y,
+                             Termcell *cells, ssize_t w)
+{
+   Termsave *ts;
+   int old_rows;
+
+   EINA_SAFETY_ON_NULL_RETURN(cells);
+   if ((backlog_y < 1) || (backlog_y >= (int)ty->backsize))
+     return;
+
+   ts = BACKLOG_ROW_GET(ty, backlog_y);
+   old_rows = termpty_backlog_entry_rows(ty, ts->w);
+   termpty_save_free(ty, ts);
+   ts->cells = cells;
+   ts->w = w;
+   ts->ext = 1;
+
+   if (backlog_y <= ty->backlog_beacon.backlog_y)
+     ty->backlog_beacon.screen_y += termpty_backlog_entry_rows(ty, w)
+                                  - old_rows;
+}
+
+void
+termpty_backlog_external_drop_oldest(Termpty *ty, size_t count)
+{
+   int backlog_y;
+
+   for (backlog_y = (int)ty->backsize - 1; (backlog_y >= 1) && count;
+        backlog_y--)
+     {
+        Termsave *ts = BACKLOG_ROW_GET(ty, backlog_y);
+
+        if (!ts->cells)
+          continue;
+        termpty_save_free(ty, ts);
+        count--;
+        if (backlog_y <= ty->backlog_beacon.backlog_y)
+          {
+             ty->backlog_beacon.screen_y = 0;
+             ty->backlog_beacon.backlog_y = 0;
+          }
+     }
+}
diff --git a/src/bin/backlog.h b/src/bin/backlog.h
index ddaca487..7dcfa1bd 100644
--- a/src/bin/backlog.h
+++ b/src/bin/backlog.h
@@ -21,6 +21,26 @@ termpty_backlog_length(Termpty *ty);
 int64_t
 termpty_backlog_memory_get(void);
 
+/* Rows an entry of w cells takes on a screen ty->w wide. */
+static inline int
+termpty_backlog_entry_rows(const Termpty *ty, size_t w)
+{
+   return (w == 0) ? 1 : DIV_ROUND_UP(w, ty->w);
+}
+
+/* Entries over cells the caller owns, such as a read-only mapping. They have
+ * to stay valid and unchanged while an entry points at them, and until the
+ * entry is replaced, dropped or the backlog freed: termpty never frees them.
+ * cells must not be NULL, even for w == 0, as NULL marks an empty entry. */
+void
+termpty_backlog_external_push(Termpty *ty, Termcell *cells, ssize_t w);
+/* backlog_y is 1 for the newest entry. */
+void
+termpty_backlog_external_set(Termpty *ty, int backlog_y,
+                             Termcell *cells, ssize_t w);
+void
+termpty_backlog_external_drop_oldest(Termpty *ty, size_t count);
+
 #define BACKLOG_ROW_GET(Ty, Y) \
    (&(Ty)->back[((Ty)->backsize - 1 + (Ty)->backpos - (Y)) % (Ty)->backsize])
 
diff --git a/src/bin/termpty.c b/src/bin/termpty.c
index 624c25e2..b257a6e3 100644
--- a/src/bin/termpty.c
+++ b/src/bin/termpty.c
@@ -1145,7 +1145,7 @@ add_new_ts:
    if (ty->backlog_hook.func)
      ty->backlog_hook.func(ty->backlog_hook.data, ty, cells, w);
 
-   ty->backlog_beacon.screen_y += (w == 0) ? 1 : DIV_ROUND_UP(w, ty->w);
+   ty->backlog_beacon.screen_y += termpty_backlog_entry_rows(ty, w);
    ty->backlog_beacon.backlog_y++;
    if (ty->backlog_beacon.backlog_y >= (int)ty->backsize)
      {
@@ -1210,7 +1210,8 @@ termpty_backscroll_adjust(Termpty *ty, int *scroll)
 
 /* @requested_y unit is in visual lines on the screen */
 static Termcell*
-_termpty_cellrow_from_beacon_get(Termpty *ty, int requested_y, ssize_t *wret)
+_termpty_cellrow_from_beacon_get(Termpty *ty, int requested_y, ssize_t *wret,
+                                 Eina_Bool *ext)
 {
    int backlog_y = ty->backlog_beacon.backlog_y;
    int screen_y = ty->backlog_beacon.screen_y;
@@ -1253,6 +1254,7 @@ _termpty_cellrow_from_beacon_get(Termpty *ty, int requested_y, ssize_t *wret)
              *wret = ts->w - delta * ty->w;
              if (*wret > ty->w)
                *wret = ty->w;
+             if (ext) *ext = ts->ext;
              return &ts->cells[delta * ty->w];
           }
         backlog_y++;
@@ -1278,6 +1280,7 @@ _termpty_cellrow_from_beacon_get(Termpty *ty, int requested_y, ssize_t *wret)
              *wret = ts->w - delta * ty->w;
              if (*wret > ty->w)
                *wret = ty->w;
+             if (ext) *ext = ts->ext;
              return &ts->cells[delta * ty->w];
           }
         screen_y -= nb_lines;
@@ -1289,7 +1292,8 @@ _termpty_cellrow_from_beacon_get(Termpty *ty, int requested_y, ssize_t *wret)
 
 /* @requested_y unit is in visual lines on the screen */
 static Termcell *
-_termpty_cellrow_live_get(Termpty *ty, int y_requested, ssize_t *wret)
+_termpty_cellrow_live_get(Termpty *ty, int y_requested, ssize_t *wret,
+                          Eina_Bool *ext)
 {
    if (y_requested >= 0)
      {
@@ -1303,7 +1307,7 @@ _termpty_cellrow_live_get(Termpty *ty, int y_requested, ssize_t *wret)
    if (!ty->back)
      return NULL;
 
-   return _termpty_cellrow_from_beacon_get(ty, y_requested, wret);
+   return _termpty_cellrow_from_beacon_get(ty, y_requested, wret, ext);
 }
 
 /* Read accessor: returns the snapshot row during a sync window (pre-BSU state),
@@ -1321,15 +1325,19 @@ termpty_cellrow_get(Termpty *ty, int y_requested, ssize_t *wret)
                                      ty->sync_output.shadow_w);
         return ty->sync_output.shadow_rows[y_requested];
      }
-   return _termpty_cellrow_live_get(ty, y_requested, wret);
+   return _termpty_cellrow_live_get(ty, y_requested, wret, NULL);
 }
 
-/* Write accessor: always returns the live (mutable) row.
+/* Write accessor: always returns the live (mutable) row, or NULL for a row
+ * of a backlog entry termpty does not own.
  * Snapshot was taken eagerly at BSU, so no per-write COW is needed here. */
 Termcell *
 termpty_cellrow_get_writeable(Termpty *ty, int y_requested, ssize_t *wret)
 {
-   return _termpty_cellrow_live_get(ty, y_requested, wret);
+   Eina_Bool ext = EINA_FALSE;
+   Termcell *cells = _termpty_cellrow_live_get(ty, y_requested, wret, &ext);
+
+   return ext ? NULL : cells;
 }
 
 /* @requested_y unit is in visual lines on the screen */
@@ -1350,7 +1358,7 @@ termpty_cell_get(Termpty *ty, int y_requested, int x_requested)
    if (!ty->back)
      return NULL;
 
-   cells = _termpty_cellrow_from_beacon_get(ty, y_requested, &wret);
+   cells = _termpty_cellrow_from_beacon_get(ty, y_requested, &wret, NULL);
    if (!cells || x_requested >= wret)
      return NULL;
    return cells + x_requested;
@@ -5005,6 +5013,61 @@ tytest_backlog_wide_entry_rows(void)
    return 0;
 }
 
+/* Test: backlog entries pointing at cells termpty does not own count rows the
+ * way owned ones do, can be repointed, dropped from the old end, are never
+ * handed out writeable, and are never freed by termpty. The cells are static:
+ * freeing them would be caught by ASan. */
+int
+tytest_backlog_external(void)
+{
+   static Termcell cells[40];
+   Termpty ty;
+   ssize_t w = 0;
+   int i;
+
+   _ty_test_init(&ty, 10, 5);
+   termpty_backlog_size_set(&ty, 8);
+   for (i = 0; i < 40; i++)
+     cells[i].codepoint = 'a' + (i % 26);
+
+   termpty_backlog_external_push(&ty, cells, 4);
+   termpty_backlog_external_push(&ty, cells, 25);
+   termpty_backlog_external_push(&ty, cells, 0);
+   assert(termpty_backlog_length(&ty) == 1 + 3 + 1);
+
+   /* the beacon is set now: growing a counted entry moves it */
+   termpty_backlog_external_set(&ty, 2, cells, 35);
+   assert(termpty_backlog_length(&ty) == 6);
+
+   termpty_backlog_external_drop_oldest(&ty, 1);
+   assert(termpty_backlog_length(&ty) == 5);
+
+   assert(termpty_cellrow_get_writeable(&ty, -1, &w) == NULL);
+   assert(termpty_cellrow_get(&ty, -1, &w) == cells);
+
+   /* moving the cursor over them, the empty newest one included, only
+    * reads them */
+   termpty_move_cursor(&ty, 0, -1);
+   termpty_move_cursor(&ty, 3, -4);
+   ty_sb_free(&ty.write_buffer);
+
+   /* an owned row appended after external ones is still writeable */
+   termpty_text_save_top(&ty, cells, 4);
+   assert(termpty_backlog_length(&ty) == 6);
+   assert(termpty_cellrow_get_writeable(&ty, -1, &w) != NULL);
+   assert(termpty_cellrow_get_writeable(&ty, -1, &w) != cells);
+
+   /* a wrapped owned row growing over an external one copies it first */
+   termpty_backlog_external_push(&ty, cells, 10);
+   cells[9].att.autowrapped = 1;
+   termpty_text_save_top(&ty, cells + 20, 3);
+   cells[9].att.autowrapped = 0;
+   assert(termpty_cellrow_get_writeable(&ty, -1, &w) != NULL);
+
+   _ty_test_shutdown(&ty);
+   return 0;
+}
+
 /* Test: a logical line spanning several wrapped rows arrives intact on the
  * other end when every hook call is replayed through
  * termpty_text_save_top() again: the hook reports each row (not the merged
diff --git a/src/bin/termpty.h b/src/bin/termpty.h
index 1087dfcc..13dd10b1 100644
--- a/src/bin/termpty.h
+++ b/src/bin/termpty.h
@@ -357,7 +357,9 @@ struct tag_Termsave
 {
    unsigned int   gen  : 8;
    unsigned int   comp : 1;
-   unsigned int   z    : 1;
+   /* The cells belong to someone else: termpty never frees, reallocates or
+    * writes them. See termpty_backlog_external_push(). */
+   unsigned int   ext  : 1;
    unsigned int   w    : 22;
    /* Cells the allocation holds, which is >= 'w': a row keeps its block when
     * rewritten. Free on LP64, where the pointer below already forced padding. */
diff --git a/src/bin/termptyext.c b/src/bin/termptyext.c
index 88ef3148..cccd1488 100644
--- a/src/bin/termptyext.c
+++ b/src/bin/termptyext.c
@@ -329,10 +329,13 @@ _handle_link(Termpty *ty, const Eina_Unicode *buf)
         ssize_t w;
 
         cells = termpty_cellrow_get_writeable(ty, sd->mouse.cy, &w);
-        termpty_reset_att(&cells[sd->mouse.cx].att);
-        cells[sd->mouse.cx].att.bold = 1;
-        cells[sd->mouse.cx].att.fg = COL_WHITE;
-        cells[sd->mouse.cx].att.bg = COL_RED;
+        if (cells)
+          {
+             termpty_reset_att(&cells[sd->mouse.cx].att);
+             cells[sd->mouse.cx].att.bold = 1;
+             cells[sd->mouse.cx].att.fg = COL_WHITE;
+             cells[sd->mouse.cx].att.bg = COL_RED;
+          }
      }
 
    /* skip type */
diff --git a/src/bin/termptyops.c b/src/bin/termptyops.c
index db48c1d5..3e1e4912 100644
--- a/src/bin/termptyops.c
+++ b/src/bin/termptyops.c
@@ -877,15 +877,24 @@ termpty_cursor_copy(Termpty *ty, Eina_Bool save)
 }
 
 
+/* The row above the screen may belong to a backlog entry termpty does not
+ * own, which has no writeable row: read it instead. */
+static Eina_Bool
+_row_autowrapped(Termpty *ty, int y)
+{
+   ssize_t wlen = 0;
+   const Termcell *cells = termpty_cellrow_get_writeable(ty, y, &wlen);
+
+   if (!cells)
+     cells = termpty_cellrow_get(ty, y, &wlen);
+   return cells && (wlen > 0) && cells[wlen - 1].att.autowrapped;
+}
+
 void
 termpty_move_cursor(Termpty *ty, int cx, int cy)
 {
    int cur_cx = ty->cursor_state.cx;
    int cur_cy = ty->cursor_state.cy;
-   /* Use the writeable (live) accessor: autowrapped is a parser-semantic flag
-    * that must reflect the current live state, not any sync snapshot. */
-   Termcell *cells;
-   ssize_t wlen;
    int n_to_down = 0;
    int n_to_right = 0;
 
@@ -895,9 +904,7 @@ termpty_move_cursor(Termpty *ty, int cx, int cy)
         if (cur_cy < cy)
           {
              /* go down */
-             cells = termpty_cellrow_get_writeable(ty, cur_cy, &wlen);
-             assert(cells);
-             if (cells[wlen-1].att.autowrapped)
+             if (_row_autowrapped(ty, cur_cy))
                {
                   n_to_right += ty->w;
                }
@@ -910,9 +917,7 @@ termpty_move_cursor(Termpty *ty, int cx, int cy)
         else
           {
              /* go up */
-             cells = termpty_cellrow_get_writeable(ty, cur_cy - 1, &wlen);
-             assert(cells);
-             if (cells[wlen-1].att.autowrapped)
+             if (_row_autowrapped(ty, cur_cy - 1))
                {
                   n_to_right -= ty->w;
                }
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 9d47a448..e764905b 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -40,6 +40,7 @@ static struct {
        { "backlog_hook", tytest_backlog_hook},
        { "backlog_hook_wrap_replay", tytest_backlog_hook_wrap_replay},
        { "backlog_wide_entry_rows", tytest_backlog_wide_entry_rows},
+       { "backlog_external", tytest_backlog_external},
        { "simd_parity", tytest_simd_parity},
        { "rgb_to_palette", tytest_rgb_to_palette},
        { "colors_is_dark", tytest_colors_is_dark},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 406b88e8..ac72191c 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -10,6 +10,7 @@ int tytest_write_hook(void);
 int tytest_backlog_hook(void);
 int tytest_backlog_hook_wrap_replay(void);
 int tytest_backlog_wide_entry_rows(void);
+int tytest_backlog_external(void);
 int tytest_simd_parity(void);
 int tytest_rgb_to_palette(void);
 int tytest_colors_is_dark(void);

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.

Reply via email to