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 17ec73fb40d8ff69192e6ccccd76d00a73fc8b6a
Author: Cedric BAIL <[email protected]>
AuthorDate: Wed Nov 18 22:08:34 2026 -0600

    termd_backlog: keep the daemon's scrollback in memfd pages
    
    Today every tymux client keeps its own full copy of the scrollback, fed
    line by line by the daemon, so memory grows with sessions times clients
    times scrollback. This store holds the scrollback once, in memfd pages
    that clients map read-only and follow through the head page's atomics.
    
    Pages hold logical lines, as wide as they were before wrapping, so a
    client can still plug them in as Termsave entries and let termpty reflow
    them on resize; a width change does not seal a page. Each page is a
    fixed-size sparse memfd that can always fit the entry in progress, so
    appending a row is a memcpy and a few stores. A new page costs
    memfd_create, ftruncate and mmap, and a grown page table a realloc, once
    every 256 entries or 1M cells. The daemon keeps entry counts and widths
    in its own memory, not in the page header: clients map the memfd and
    could write to it. Only the cells written use memory.
---
 DESIGN.md               |   1 +
 src/bin/meson.build     |   2 +
 src/bin/termd_backlog.c | 553 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/bin/termd_backlog.h |  45 ++++
 src/bin/tytest.c        |   3 +
 src/bin/unit_tests.h    |   3 +
 6 files changed, 607 insertions(+)

diff --git a/DESIGN.md b/DESIGN.md
index 048175d2..a544ae0a 100644
--- a/DESIGN.md
+++ b/DESIGN.md
@@ -26,6 +26,7 @@ A simple documentation to navigate through the C files:
 * `src/bin/options_wallpaper.c`: the settings panel to configure a wallpaper
 * `src/bin/sel.c`: the tab selector
 * `src/bin/termcmd.c` handles custom terminology commands
+* `src/bin/termd_backlog.c`: the scrollback of a `tymuxd` session, in memfd pages its clients map
 * `src/bin/termd_tymux.c`: publishes a terminal running in `tymuxd` to its clients
 * `src/bin/termio.c`: the core term widget with the textgrid
 * `src/bin/termiolink.c`: link detection in the terminal
diff --git a/src/bin/meson.build b/src/bin/meson.build
index 2b70b8ab..801b8a8b 100644
--- a/src/bin/meson.build
+++ b/src/bin/meson.build
@@ -165,6 +165,7 @@ tybench_sources = [color_names,
 
 if host_os == 'linux'
   tytest_sources += ['termsrv.c', 'termsrv.h', 'termtymux.c', 'termtymux.h',
+                     'termd_backlog.c', 'termd_backlog.h',
                      'tytest_tymux.c', 'tytest_tymux.h']
 endif
 
@@ -215,6 +216,7 @@ if host_os == 'linux'
                     'tymuxd_stubs.c',
                     'color_names.h', color_names,
                     'termd_tymux.c', 'termd_tymux.h',
+                    'termd_backlog.c', 'termd_backlog.h',
                     'termsrv.c', 'termsrv.h',
                     'termpty.c', 'termpty.h',
                     'termptydbl.c', 'termptydbl.h',
diff --git a/src/bin/termd_backlog.c b/src/bin/termd_backlog.c
new file mode 100644
index 00000000..87c18dd5
--- /dev/null
+++ b/src/bin/termd_backlog.c
@@ -0,0 +1,553 @@
+#include "private.h"
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/mman.h>
+#include <Elementary.h>
+#include "termpty.h"
+#include "termsrv.h"
+#include "termd_backlog.h"
+
+_Static_assert(sizeof(Termcell) == sizeof(TermSrvCell),
+               "TermSrvCell must mirror Termcell");
+
+typedef struct
+{
+   TermSrvBacklogPage *header;
+   /* What the daemon reads back about the entries is kept here: the memfd
+    * is writable by the clients, the header is only ever stored to. */
+   uint32_t            entries;
+   uint32_t            last_w;
+   int                 fd;
+   uint32_t            refs;
+} TermDBacklogPage;
+
+struct tag_TermDBacklog
+{
+   const char       *name;
+   TermDBacklogPage *pages; /* oldest first, the head last */
+   uint32_t          count;
+   uint32_t          alloc;
+   uint32_t          first_id;
+   uint32_t          max_entries;
+   uint32_t          head_cells;
+   uint64_t          total_entries;
+};
+
+static TermSrvCell *
+_page_cells(TermSrvBacklogPage *header)
+{
+   return (TermSrvCell *)
+      ((unsigned char *)header + TERMSRV_BACKLOG_HEADER_SIZE);
+}
+
+static TermDBacklogPage *
+_page_find(TermDBacklog *bl, uint32_t page_id)
+{
+   uint32_t idx = page_id - bl->first_id;
+
+   if (idx >= bl->count)
+     return NULL;
+   return &bl->pages[idx];
+}
+
+static void
+_page_close(TermDBacklogPage *page)
+{
+   munmap(page->header, TERMSRV_BACKLOG_PAGE_SIZE);
+   close(page->fd);
+}
+
+static TermSrvBacklogPage *
+_page_add(TermDBacklog *bl)
+{
+   TermDBacklogPage *page;
+   char memfd_name[128];
+   uint32_t page_id = bl->first_id + bl->count;
+   int fd;
+
+   if (bl->count == bl->alloc)
+     {
+        uint32_t alloc = bl->alloc ? bl->alloc * 2 : 4;
+        TermDBacklogPage *pages = realloc(bl->pages, alloc * sizeof(*pages));
+
+        if (!pages)
+          {
+             ERR("cannot grow the backlog page table of '%s'", bl->name);
+             return NULL;
+          }
+        bl->pages = pages;
+        bl->alloc = alloc;
+     }
+
+   snprintf(memfd_name, sizeof(memfd_name), "ty-backlog-%s-%u",
+            bl->name, page_id);
+   fd = memfd_create(memfd_name, MFD_CLOEXEC);
+   if (fd < 0)
+     {
+        ERR("memfd_create: %s", strerror(errno));
+        return NULL;
+     }
+   if (ftruncate(fd, TERMSRV_BACKLOG_PAGE_SIZE) < 0)
+     {
+        ERR("ftruncate: %s", strerror(errno));
+        close(fd);
+        return NULL;
+     }
+   page = &bl->pages[bl->count];
+   page->header = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE,
+                       PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+   if (page->header == MAP_FAILED)
+     {
+        ERR("mmap: %s", strerror(errno));
+        close(fd);
+        return NULL;
+     }
+   page->fd = fd;
+   page->refs = 0;
+   page->header->magic = TERMSRV_BACKLOG_MAGIC;
+   page->header->version = TERMSRV_BACKLOG_VERSION;
+   page->header->page_id = page_id;
+   bl->count++;
+   bl->head_cells = 0;
+   return page->header;
+}
+
+static void
+_evict(TermDBacklog *bl)
+{
+   while (bl->count > 1)
+     {
+        uint32_t oldest = bl->pages[0].entries;
+
+        if (bl->total_entries - oldest < bl->max_entries)
+          return;
+        _page_close(&bl->pages[0]);
+        bl->count--;
+        memmove(&bl->pages[0], &bl->pages[1],
+                bl->count * sizeof(bl->pages[0]));
+        bl->first_id++;
+        bl->total_entries -= oldest;
+     }
+}
+
+TermDBacklog *
+termd_backlog_new(const char *name, uint32_t max_entries)
+{
+   TermDBacklog *bl;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
+   bl = calloc(1, sizeof(*bl));
+   if (!bl)
+     return NULL;
+   bl->name = eina_stringshare_add(name);
+   bl->max_entries = max_entries;
+   return bl;
+}
+
+void
+termd_backlog_free(TermDBacklog *bl)
+{
+   uint32_t i;
+
+   if (!bl)
+     return;
+   for (i = 0; i < bl->count; i++)
+     _page_close(&bl->pages[i]);
+   free(bl->pages);
+   eina_stringshare_del(bl->name);
+   free(bl);
+}
+
+/* Clients follow the head through its atomics without a lock: the cells
+ * are written before the width that covers them is published, and a new
+ * entry's offset and width before num_entries counts it. */
+Eina_Bool
+termd_backlog_append(TermDBacklog *bl, const Termcell *cells, ssize_t w,
+                     Eina_Bool extends)
+{
+   TermSrvBacklogPage *head = NULL;
+   TermDBacklogPage *head_page = NULL;
+   uint32_t n = 0, cur_w, add;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, EINA_FALSE);
+   EINA_SAFETY_ON_TRUE_RETURN_VAL(w < 0 || (w > 0 && !cells), EINA_FALSE);
+
+   if (bl->count)
+     {
+        head_page = &bl->pages[bl->count - 1];
+        head = head_page->header;
+        n = head_page->entries;
+     }
+
+   if (extends && n > 0)
+     {
+        cur_w = head_page->last_w;
+        add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS - cur_w);
+        if (add == 0)
+          return EINA_TRUE;
+        memcpy(_page_cells(head) + bl->head_cells, cells,
+               add * sizeof(Termcell));
+        atomic_store_explicit(&head->entry_w[n - 1], cur_w + add,
+                              memory_order_release);
+        head_page->last_w = cur_w + add;
+        bl->head_cells += add;
+        return EINA_TRUE;
+     }
+
+   if (!head || n >= TERMSRV_BACKLOG_PAGE_ENTRIES ||
+       bl->head_cells >= TERMSRV_BACKLOG_PAGE_SEAL_CELLS)
+     {
+        head = _page_add(bl);
+        if (!head)
+          return EINA_FALSE;
+        head_page = &bl->pages[bl->count - 1];
+        n = 0;
+     }
+   add = MIN((size_t)w, TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
+   if (add > 0)
+     memcpy(_page_cells(head) + bl->head_cells, cells,
+            add * sizeof(Termcell));
+   head->entry_off[n] = bl->head_cells;
+   atomic_store_explicit(&head->entry_w[n], add, memory_order_release);
+   atomic_store_explicit(&head->num_entries, n + 1, memory_order_release);
+   head_page->entries = n + 1;
+   head_page->last_w = add;
+   bl->head_cells += add;
+   bl->total_entries++;
+   _evict(bl);
+   return EINA_TRUE;
+}
+
+uint32_t
+termd_backlog_first_id(const TermDBacklog *bl)
+{
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, 0);
+   return bl->first_id;
+}
+
+uint32_t
+termd_backlog_count(const TermDBacklog *bl)
+{
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, 0);
+   return bl->count;
+}
+
+int
+termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id)
+{
+   TermDBacklogPage *page;
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, -1);
+   page = _page_find(bl, page_id);
+   return page ? page->fd : -1;
+}
+
+void
+termd_backlog_hold(TermDBacklog *bl, uint32_t page_id)
+{
+   TermDBacklogPage *page;
+
+   EINA_SAFETY_ON_NULL_RETURN(bl);
+   page = _page_find(bl, page_id);
+   if (page)
+     page->refs++;
+}
+
+void
+termd_backlog_release(TermDBacklog *bl, uint32_t page_id)
+{
+   TermDBacklogPage *page;
+
+   EINA_SAFETY_ON_NULL_RETURN(bl);
+   page = _page_find(bl, page_id);
+   if (page && page->refs > 0)
+     page->refs--;
+}
+
+uint32_t
+termd_backlog_map_write(TermDBacklog *bl, uint32_t first, uint32_t count,
+                        void *buf, uint32_t bufsz)
+{
+   TermSrvMsgBacklogMapReq echo = { .first_page_id = first, .count = 0 };
+   uint64_t page_id, end;
+   uint32_t off = sizeof(echo);
+
+   EINA_SAFETY_ON_NULL_RETURN_VAL(bl, 0);
+   EINA_SAFETY_ON_NULL_RETURN_VAL(buf, 0);
+   if (bufsz < sizeof(echo))
+     return 0;
+
+   page_id = MAX(first, bl->first_id);
+   end = MIN((uint64_t)first + count, (uint64_t)bl->first_id + bl->count);
+   if (page_id < end)
+     echo.first_page_id = page_id;
+   for (; page_id < end; page_id++)
+     {
+        const TermDBacklogPage *page = &bl->pages[page_id - bl->first_id];
+
+        if (termsrv_backlog_map_page_append(buf, bufsz, &off, page_id,
+                                            page->entries,
+                                            page->header->entry_w) < 0)
+          break;
+        echo.count++;
+     }
+   memcpy(buf, &echo, sizeof(echo));
+   return off;
+}
+
+#if defined(BINARY_TYTEST)
+#include <assert.h>
+
+static void
+_test_row_fill(Termcell *cells, int w, Eina_Unicode first)
+{
+   int i;
+
+   memset(cells, 0, w * sizeof(Termcell));
+   for (i = 0; i < w; i++)
+     cells[i].codepoint = first + i;
+}
+
+static const TermSrvBacklogPage *
+_test_page_map(TermDBacklog *bl, uint32_t page_id)
+{
+   const TermSrvBacklogPage *page;
+   int fd = termd_backlog_page_fd(bl, page_id);
+
+   assert(fd >= 0);
+   page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED, fd, 0);
+   assert(page != MAP_FAILED);
+   assert(page->magic == TERMSRV_BACKLOG_MAGIC);
+   assert(page->version == TERMSRV_BACKLOG_VERSION);
+   assert(page->page_id == page_id);
+   return page;
+}
+
+static const TermSrvCell *
+_test_page_cells(const TermSrvBacklogPage *page)
+{
+   return (const TermSrvCell *)
+      ((const unsigned char *)page + TERMSRV_BACKLOG_HEADER_SIZE);
+}
+
+static void
+_test_page_unmap(const TermSrvBacklogPage *page)
+{
+   munmap((void *)page, TERMSRV_BACKLOG_PAGE_SIZE);
+}
+
+/* Test: entries fill pages of TERMSRV_BACKLOG_PAGE_ENTRIES, a row that
+ * extends grows the last entry in place, and a client mapping the head
+ * follows it live. A page also seals on TERMSRV_BACKLOG_PAGE_SEAL_CELLS,
+ * an entry stops growing at TERMSRV_BACKLOG_ENTRY_MAX_CELLS, and extends
+ * with nothing to extend starts an entry. */
+int
+tytest_termd_backlog_pages(void)
+{
+   const TermSrvBacklogPage *page;
+   const TermSrvCell *page_cells;
+   TermDBacklog *bl;
+   Termcell row[10], *big;
+   uint32_t i, last;
+
+   bl = termd_backlog_new("test", 100000);
+   assert(bl);
+   assert(termd_backlog_count(bl) == 0);
+   for (i = 0; i < 600; i++)
+     {
+        _test_row_fill(row, 1, i);
+        assert(termd_backlog_append(bl, row, 1, EINA_FALSE));
+     }
+   assert(termd_backlog_count(bl) == 3);
+   assert(termd_backlog_first_id(bl) == 0);
+   page = _test_page_map(bl, 2);
+   assert(atomic_load(&page->num_entries) == 600 - 2 * 256);
+
+   for (i = 0; i < 3; i++)
+     {
+        _test_row_fill(row, 10, 'a' + 10 * i);
+        assert(termd_backlog_append(bl, row, 10, i > 0));
+     }
+   assert(termd_backlog_count(bl) == 3);
+   assert(atomic_load(&page->num_entries) == 600 - 2 * 256 + 1);
+   last = atomic_load(&page->num_entries) - 1;
+   assert(page->entry_off[last] == 600 - 2 * 256);
+   assert(atomic_load(&page->entry_w[last]) == 30);
+   assert(atomic_load(&page->entry_w[last - 1]) == 1);
+   page_cells = _test_page_cells(page);
+   assert(page_cells[page->entry_off[last - 1]].codepoint == 599);
+   for (i = 0; i < 30; i++)
+     assert(page_cells[page->entry_off[last] + i].codepoint == 'a' + i);
+   _test_page_unmap(page);
+
+   /* the memfd is writable by the clients: what they put in the head must
+    * not steer the daemon */
+     {
+        TermSrvBacklogPage *rw = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE,
+                                      PROT_READ | PROT_WRITE, MAP_SHARED,
+                                      termd_backlog_page_fd(bl, 2), 0);
+
+        assert(rw != MAP_FAILED);
+        atomic_store(&rw->num_entries, 0x7fffffff);
+        atomic_store(&rw->entry_w[last], 0xffffffff);
+        _test_row_fill(row, 10, 'a');
+        assert(termd_backlog_append(bl, row, 10, EINA_TRUE));
+        assert(atomic_load(&rw->entry_w[last]) == 40);
+        assert(termd_backlog_append(bl, row, 10, EINA_FALSE));
+        assert(atomic_load(&rw->num_entries) == last + 2);
+        munmap(rw, TERMSRV_BACKLOG_PAGE_SIZE);
+     }
+
+   assert(termd_backlog_page_fd(bl, 3) < 0);
+   termd_backlog_hold(bl, 3);
+   termd_backlog_release(bl, 3);
+   termd_backlog_free(bl);
+
+   big = calloc(TERMSRV_BACKLOG_ENTRY_MAX_CELLS, sizeof(Termcell));
+   assert(big);
+   big[0].codepoint = 'x';
+   bl = termd_backlog_new("test", 100000);
+   assert(bl);
+   assert(termd_backlog_append(bl, big, TERMSRV_BACKLOG_ENTRY_MAX_CELLS - 5,
+                               EINA_TRUE));
+   _test_row_fill(row, 10, 'a');
+   assert(termd_backlog_append(bl, row, 10, EINA_TRUE));
+   page = _test_page_map(bl, 0);
+   assert(atomic_load(&page->num_entries) == 1);
+   assert(atomic_load(&page->entry_w[0]) == TERMSRV_BACKLOG_ENTRY_MAX_CELLS);
+   page_cells = _test_page_cells(page);
+   assert(page_cells[0].codepoint == 'x');
+   assert(page_cells[TERMSRV_BACKLOG_ENTRY_MAX_CELLS - 1].codepoint == 'e');
+   _test_page_unmap(page);
+
+   assert(termd_backlog_append(bl, row, 1, EINA_FALSE));
+   assert(termd_backlog_count(bl) == 2);
+   page = _test_page_map(bl, 1);
+   assert(atomic_load(&page->num_entries) == 1);
+   assert(page->entry_off[0] == 0);
+   assert(_test_page_cells(page)[0].codepoint == 'a');
+   _test_page_unmap(page);
+   termd_backlog_free(bl);
+   free(big);
+   return 0;
+}
+
+/* Test: the oldest pages go once the newer ones hold max_entries, held or
+ * not, and a client that got a page's memfd before can still read it. */
+int
+tytest_termd_backlog_evict(void)
+{
+   const TermSrvBacklogPage *page;
+   TermDBacklog *bl;
+   Termcell row[1];
+   uint32_t i, kept = 0, buf[64];
+   TermSrvMsgBacklogMapReq *echo = (TermSrvMsgBacklogMapReq *)buf;
+   int page0_fd;
+
+   bl = termd_backlog_new("test", 300);
+   assert(bl);
+   _test_row_fill(row, 1, 0);
+   assert(termd_backlog_append(bl, row, 1, EINA_FALSE));
+   page0_fd = dup(termd_backlog_page_fd(bl, 0));
+   assert(page0_fd >= 0);
+   termd_backlog_hold(bl, 0);
+   for (i = 1; i < 1000; i++)
+     {
+        _test_row_fill(row, 1, i);
+        assert(termd_backlog_append(bl, row, 1, EINA_FALSE));
+     }
+
+   assert(termd_backlog_first_id(bl) > 0);
+   assert(termd_backlog_page_fd(bl, 0) < 0);
+   for (i = 0; i < termd_backlog_count(bl); i++)
+     {
+        page = _test_page_map(bl, termd_backlog_first_id(bl) + i);
+        kept += atomic_load(&page->num_entries);
+        _test_page_unmap(page);
+     }
+   assert(kept >= 300 && kept < 300 + TERMSRV_BACKLOG_PAGE_ENTRIES);
+   termd_backlog_release(bl, 0);
+
+   assert(termd_backlog_map_write(bl, 0, 100, buf, sizeof(buf)) >=
+          sizeof(*echo));
+   assert(echo->first_page_id == termd_backlog_first_id(bl));
+
+   page = mmap(NULL, TERMSRV_BACKLOG_PAGE_SIZE, PROT_READ, MAP_SHARED,
+               page0_fd, 0);
+   assert(page != MAP_FAILED);
+   assert(page->magic == TERMSRV_BACKLOG_MAGIC && page->page_id == 0);
+   assert(atomic_load(&page->num_entries) == TERMSRV_BACKLOG_PAGE_ENTRIES);
+   assert(_test_page_cells(page)[TERMSRV_BACKLOG_PAGE_ENTRIES - 1].codepoint ==
+          TERMSRV_BACKLOG_PAGE_ENTRIES - 1);
+   _test_page_unmap(page);
+   close(page0_fd);
+   termd_backlog_free(bl);
+   return 0;
+}
+
+static uint32_t
+_test_map_width(uint32_t page_id, uint32_t entry)
+{
+   return (page_id * TERMSRV_BACKLOG_PAGE_ENTRIES + entry) % 7;
+}
+
+/* Test: termd_backlog_map_write() lists the widths of the pages asked for
+ * that exist, as many as fit, and echoes the range it covers. */
+int
+tytest_termd_backlog_map(void)
+{
+   static uint32_t buf[TERMSRV_MAX_MSG_PAYLOAD / sizeof(uint32_t)];
+   TermSrvMsgBacklogMapReq *echo = (TermSrvMsgBacklogMapReq *)buf;
+   const uint32_t *widths;
+   TermDBacklog *bl;
+   Termcell row[7];
+   uint32_t i, j, len, off, page_id, num_entries, two_pages;
+
+   bl = termd_backlog_new("test", 100000);
+   assert(bl);
+   _test_row_fill(row, 7, 'a');
+   for (i = 0; i < 600; i++)
+     assert(termd_backlog_append(bl, row, i % 7, EINA_FALSE));
+   assert(termd_backlog_count(bl) == 3);
+
+   len = termd_backlog_map_write(bl, 0, 100, buf, sizeof(buf));
+   assert(echo->first_page_id == 0 && echo->count == 3);
+   off = sizeof(*echo);
+   for (i = 0; i < 3; i++)
+     {
+        assert(termsrv_backlog_map_page_next(buf, len, &off, &page_id,
+                                             &num_entries, &widths) == 1);
+        assert(page_id == i);
+        assert(num_entries == (i < 2 ? TERMSRV_BACKLOG_PAGE_ENTRIES :
+                               600 - 2 * TERMSRV_BACKLOG_PAGE_ENTRIES));
+        for (j = 0; j < num_entries; j++)
+          assert(widths[j] == _test_map_width(i, j));
+     }
+   assert(termsrv_backlog_map_page_next(buf, len, &off, &page_id,
+                                        &num_entries, &widths) == 0);
+
+   two_pages = sizeof(*echo) +
+      2 * (2 + TERMSRV_BACKLOG_PAGE_ENTRIES) * sizeof(uint32_t);
+   len = termd_backlog_map_write(bl, 0, 3, buf, two_pages + 4);
+   assert(len == two_pages);
+   assert(echo->first_page_id == 0 && echo->count == 2);
+
+   len = termd_backlog_map_write(bl, 1, UINT32_MAX, buf, sizeof(buf));
+   assert(echo->first_page_id == 1 && echo->count == 2);
+   off = sizeof(*echo);
+   assert(termsrv_backlog_map_page_next(buf, len, &off, &page_id,
+                                        &num_entries, &widths) == 1);
+   assert(page_id == 1);
+
+   len = termd_backlog_map_write(bl, 3, 5, buf, sizeof(buf));
+   assert(len == sizeof(*echo) && echo->count == 0);
+   len = termd_backlog_map_write(bl, 0, 0, buf, sizeof(buf));
+   assert(len == sizeof(*echo) && echo->count == 0);
+   assert(termd_backlog_map_write(bl, 0, 3, buf, sizeof(*echo) - 1) == 0);
+   termd_backlog_free(bl);
+   return 0;
+}
+#endif
diff --git a/src/bin/termd_backlog.h b/src/bin/termd_backlog.h
new file mode 100644
index 00000000..e0407083
--- /dev/null
+++ b/src/bin/termd_backlog.h
@@ -0,0 +1,45 @@
+#ifndef TERMINOLOGY_TERMD_BACKLOG_H_
+#define TERMINOLOGY_TERMD_BACKLOG_H_ 1
+
+/* The scrollback of a tymuxd session, kept once in memfd pages that its
+ * clients map read-only (see TermSrvBacklogPage in termsrv.h). Pages are
+ * identified by an id that only grows; the last page is the head, the only
+ * one still written to. Include termpty.h and termsrv.h first. */
+typedef struct tag_TermDBacklog TermDBacklog;
+
+/* A store whose memfds are named "ty-backlog-<name>-<id>", that evicts its
+ * oldest pages once the others still hold max_entries entries. Returns NULL
+ * on failure. */
+TermDBacklog *termd_backlog_new(const char *name, uint32_t max_entries);
+
+void termd_backlog_free(TermDBacklog *bl);
+
+/* Store a row of w cells that left the screen. If extends, the row
+ * continues the last entry (clamped to TERMSRV_BACKLOG_ENTRY_MAX_CELLS),
+ * otherwise it starts a new one. Returns EINA_FALSE if the row could not be
+ * stored. */
+Eina_Bool termd_backlog_append(TermDBacklog *bl, const Termcell *cells,
+                               ssize_t w, Eina_Bool extends);
+
+/* The id of the oldest page, and how many pages there are up to the head. */
+uint32_t termd_backlog_first_id(const TermDBacklog *bl);
+uint32_t termd_backlog_count(const TermDBacklog *bl);
+
+/* The memfd of page_id, owned by the store: send it, do not close it.
+ * Returns -1 for an unknown or evicted page. */
+int termd_backlog_page_fd(TermDBacklog *bl, uint32_t page_id);
+
+/* Count a client holding page_id, or one letting go of it. Unknown ids are
+ * ignored. */
+void termd_backlog_hold(TermDBacklog *bl, uint32_t page_id);
+void termd_backlog_release(TermDBacklog *bl, uint32_t page_id);
+
+/* Write a TSRV_MSG_BACKLOG_MAP payload into buf for the pages in
+ * [first, first + count): the TermSrvMsgBacklogMapReq echo of the range
+ * covered, clipped to the pages that exist and to what fits in bufsz, then
+ * those pages. Returns the bytes written, 0 if bufsz cannot hold the
+ * echo. */
+uint32_t termd_backlog_map_write(TermDBacklog *bl, uint32_t first,
+                                 uint32_t count, void *buf, uint32_t bufsz);
+
+#endif
diff --git a/src/bin/tytest.c b/src/bin/tytest.c
index 0f1b1735..5d91f353 100644
--- a/src/bin/tytest.c
+++ b/src/bin/tytest.c
@@ -178,6 +178,9 @@ static struct {
        { "termsrv_backlog_line", tytest_termsrv_backlog_line},
        { "termsrv_backlog_page_fd", tytest_termsrv_backlog_page_fd},
        { "termsrv_backlog_map", tytest_termsrv_backlog_map},
+       { "termd_backlog_pages", tytest_termd_backlog_pages},
+       { "termd_backlog_evict", tytest_termd_backlog_evict},
+       { "termd_backlog_map", tytest_termd_backlog_map},
        { "termtymux_frame_apply_same_size", tytest_termtymux_frame_apply_same_size},
        { "termtymux_frame_apply_resize", tytest_termtymux_frame_apply_resize},
        { "termtymux_frame_apply_shrink_grow", tytest_termtymux_frame_apply_shrink_grow},
diff --git a/src/bin/unit_tests.h b/src/bin/unit_tests.h
index 9ba5f5ee..d472f551 100644
--- a/src/bin/unit_tests.h
+++ b/src/bin/unit_tests.h
@@ -133,6 +133,9 @@ int tytest_termsrv_cell_layout(void);
 int tytest_termsrv_backlog_line(void);
 int tytest_termsrv_backlog_page_fd(void);
 int tytest_termsrv_backlog_map(void);
+int tytest_termd_backlog_pages(void);
+int tytest_termd_backlog_evict(void);
+int tytest_termd_backlog_map(void);
 int tytest_termtymux_frame_apply_same_size(void);
 int tytest_termtymux_frame_apply_resize(void);
 int tytest_termtymux_frame_apply_shrink_grow(void);

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

Reply via email to