Skip to content

Integrate LSF allocator and lazy widget extension#161

Merged
jserv merged 1 commit intomainfrom
consolidate-memory
Apr 25, 2026
Merged

Integrate LSF allocator and lazy widget extension#161
jserv merged 1 commit intomainfrom
consolidate-memory

Conversation

@jserv
Copy link
Copy Markdown
Contributor

@jserv jserv commented Apr 25, 2026

This introduces a twin_raw_* indirection layer between the memory stats tracking (memstats.c) and the underlying allocator. When CONFIG_MEM_TLSF is enabled, allocations route through a self-contained TLSF (Two-Level Segregated Fit) allocator backed by a static pool, providing O(1) bounded-time malloc/free for real-time embedded targets. When disabled, twin_raw_* inlines to libc malloc/free with zero overhead.

The TLSF implementation is stripped to static-pool essentials (pool_init, malloc, realloc, free) and uses the portable twin_clz/twin_clzll helpers instead of raw compiler intrinsics. A key deviation from upstream TLSF: block_find_free() trims to the actual request size rather than the bin minimum, avoiding catastrophic internal fragmentation when small requests hit large bins.

Move optional widget fields (callback, callback_data, want_focus) into a lazily-allocated twin_widget_ext_t block. Widgets that never register a callback keep ext == NULL and save 16 bytes per widget (64-bit) or 8 bytes (32-bit). The copy_geom field stays inline in the base struct for app-level direct access. All call sites in box.c, button.c, and widget.c updated to use inline accessors.


Summary by cubic

Add an optional TLSF-backed allocator and a lazy widget extension to reduce RAM use and improve real-time behavior. Adds a twin_raw_* allocator layer and moves optional widget fields out of the base struct.

  • New Features

    • Optional TLSF allocator behind CONFIG_MEM_TLSF with a fixed pool (CONFIG_MEM_TLSF_POOL_SIZE). O(1) malloc/free; falls back to libc when disabled.
    • TLSF tweak: trims splits to the actual request size to reduce fragmentation.
    • New twin_raw_* allocator indirection; memory stats now track allocations via this layer. The tracking table itself stays on libc to avoid recursion.
    • Widgets: move callback, callback_data, and want_focus into a lazily allocated extension, saving 8–16 bytes per widget. Added twin_widget_set_focusable; twin_widget_set_callback unchanged. Call sites updated.
  • Migration

    • Stop touching widget->callback, widget->callback_data, or widget->want_focus directly. Use twin_widget_set_callback(...) and twin_widget_set_focusable(...).
    • If enabling CONFIG_MEM_TLSF, size CONFIG_MEM_TLSF_POOL_SIZE for your workload (pixmaps can be large). Requests beyond the pool return NULL. No changes needed when TLSF is disabled.

Written for commit f224621. Summary will update on new commits.

This introduces a twin_raw_* indirection layer between the memory stats
tracking (memstats.c) and the underlying allocator. When CONFIG_MEM_TLSF
is enabled, allocations route through a self-contained TLSF (Two-Level
Segregated Fit) allocator backed by a static pool, providing O(1)
bounded-time malloc/free for real-time embedded targets. When disabled,
twin_raw_* inlines to libc malloc/free with zero overhead.

The TLSF implementation is stripped to static-pool essentials (pool_init,
malloc, realloc, free) and uses the portable twin_clz/twin_clzll helpers
instead of raw compiler intrinsics. A key deviation from upstream TLSF:
block_find_free() trims to the actual request size rather than the bin
minimum, avoiding catastrophic internal fragmentation when small requests
hit large bins.

Move optional widget fields (callback, callback_data, want_focus) into a
lazily-allocated twin_widget_ext_t block. Widgets that never register a
callback keep ext == NULL and save 16 bytes per widget (64-bit) or 8
bytes (32-bit). The copy_geom field stays inline in the base struct for
app-level direct access. All call sites in box.c, button.c, and widget.c
updated to use inline accessors.
Copy link
Copy Markdown

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 10 files

@jserv jserv merged commit 4f33b19 into main Apr 25, 2026
9 checks passed
@jserv jserv deleted the consolidate-memory branch April 25, 2026 17:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant