Skip to content

Add mmap allocator + pure-Silver memory intrinsics (Wave 1 unit 2) - #7

Merged
CierCier merged 8 commits into
mainfrom
std/mmap-allocator
Jul 20, 2026
Merged

CierCier merged 8 commits into
mainfrom
std/mmap-allocator

Conversation

@CierCier

@CierCier CierCier commented Jul 20, 2026 •

Copy link
Copy Markdown
Owner

Summary

Wave 1 unit 2 of docs/runtime-migration.md.

  • std/mem/alloc.ag — page-aligned free-list allocator over sys_mmap/sys_munmap
  • std/mem/memory.ag — pure-Silver memset/memcpy/memmove/strlen
  • Exports frozen C-ABI surface: silver_rt_alloc, silver_rt_alloc_zeroed, silver_rt_realloc, silver_rt_dealloc, silver_rt_memset, silver_rt_memcpy, silver_rt_memmove, silver_rt_strlen (size-0 → dangling non-null; null-tolerant dealloc)
  • Internal alloc<T>() generic API signature unchanged
  • tests/mem_test.ag — 49 cases covering free-list reuse, huge allocs, ABI edge cases, arena alignment

Test plan

Greptile Summary

This wave adds a pure-Silver, libc-free allocator (std/mem/alloc.ag) built on sys_mmap/sys_munmap, and pure-Silver memory intrinsics (std/mem/memory.ag). The allocation surface (silver_rt_alloc, silver_rt_alloc_zeroed, silver_rt_realloc, silver_rt_dealloc) plus libc-compatible shims (malloc, calloc, realloc, free, memset, memcpy, memmove, strlen) replace the former extern \"C\" stubs.

  • std/mem/alloc.ag: page-aligned free-list allocator with 9 size classes (16\u20134096 bytes), dedicated mmap for huge/over-aligned allocations, and a lazily-initialized single-page head table. The mem_dangling cap at MEM_MIN_VALID_ADDR resolves the prior dangling-pointer detection issues from earlier review threads.
  • std/mem/memory.ag: plain byte-loop implementations of memset, memcpy, memmove, and strlen; all null-tolerant; memmove direction logic is correct for both overlap directions.
  • tests/mem_test.ag: 49 new cases covering zeroed-block recycling, free-list reuse, huge-alloc realloc, frozen ABI edge cases, and the aligned zero-sized free regression.

Confidence Score: 5/5

The allocator and intrinsic logic is correct; the prior dangling-pointer and calloc-overflow issues are resolved. Both findings are cosmetic.

The core allocation, free, and realloc paths have been traced through all tag combinations and the arithmetic checks out. The mem_dangling cap correctly keeps all sentinels below MEM_MIN_VALID_ADDR, mem_free_impl safely ignores them, huge-block offset encoding and recovery are symmetric, and the memmove direction logic is sound. The only findings are a stale doc comment on mem_dangling and three redundant extern C declarations that were not cleaned up alongside malloc/calloc/free/realloc.

The two findings are both in std/mem/alloc.ag: the mem_dangling doc comment and the leftover extern C block for memset/memcpy/memmove.

Important Files Changed

Filename Overview
std/mem/alloc.ag Core allocator — 325 lines of new Silver. The mem_dangling fix (cap sentinel below MEM_MIN_VALID_ADDR) addresses prior review concerns. Minor: doc comment still references zero page rather than the actual 32 MiB guard, and three extern C declarations for functions now defined in Silver were not removed.
std/mem/memory.ag New file — pure-Silver memset/memcpy/memmove/strlen. All four routines are null-tolerant; memmove direction selection is correct for overlapping regions in both directions.
tests/mem_test.ag Expanded test suite with 151 lines of new cases covering zeroed-block recycling, free-list reuse counting, huge-alloc realloc, frozen ABI edge cases, null-tolerance of byte ops, and the aligned zero-sized free regression check.
std/mem.ag One-line change: pub import std.mem.memory added so the new intrinsics are part of the public std.mem surface.
std/string.ag Removed strlen from the extern C block; now sourced from std.mem.memory. A clarifying comment replaces the declaration.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A["silver_rt_alloc(size, align)"] --> B{size <= 0?}
    B -- yes --> C["mem_dangling: return ptr in 0..32MiB"]
    B -- no --> D{align<=16 AND size<=4096?}
    D -- yes --> E["size-class path: idx=ceil-log2(size/16)"]
    E --> F{heads-idx empty?}
    F -- yes --> G["mem_refill: mmap 64KiB chunk, carve blocks"]
    G --> H["pop head block from free list"]
    F -- no --> H
    D -- no --> I["huge path: mmap page-rounded(size+16+align)"]
    I --> J["place header at aligned offset, tag=-1-offset"]
    H --> K["return user ptr"]
    J --> K
    L["mem_free_impl(ptr)"] --> M{ptr < 32MiB?}
    M -- yes --> N["no-op: null or dangling sentinel"]
    M -- no --> O{tag in 0..8?}
    O -- yes --> P["push to free-list heads-tag"]
    O -- no --> Q{tag < 0?}
    Q -- yes --> R["munmap base, huge block released"]
    Q -- no --> S["unknown tag: defensive no-op"]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A["silver_rt_alloc(size, align)"] --> B{size <= 0?}
    B -- yes --> C["mem_dangling: return ptr in 0..32MiB"]
    B -- no --> D{align<=16 AND size<=4096?}
    D -- yes --> E["size-class path: idx=ceil-log2(size/16)"]
    E --> F{heads-idx empty?}
    F -- yes --> G["mem_refill: mmap 64KiB chunk, carve blocks"]
    G --> H["pop head block from free list"]
    F -- no --> H
    D -- no --> I["huge path: mmap page-rounded(size+16+align)"]
    I --> J["place header at aligned offset, tag=-1-offset"]
    H --> K["return user ptr"]
    J --> K
    L["mem_free_impl(ptr)"] --> M{ptr < 32MiB?}
    M -- yes --> N["no-op: null or dangling sentinel"]
    M -- no --> O{tag in 0..8?}
    O -- yes --> P["push to free-list heads-tag"]
    O -- no --> Q{tag < 0?}
    Q -- yes --> R["munmap base, huge block released"]
    Q -- no --> S["unknown tag: defensive no-op"]
Loading

Reviews (3): Last reviewed commit: "Merge origin/main into std/mmap-allocato..." | Re-trigger Greptile

Rewrite std/mem/alloc.ag over sys_mmap/sys_munmap with a page-aligned
free-list, add std/mem/memory.ag (memset/memcpy/memmove/strlen), and
export the frozen silver_rt_alloc* / silver_rt_mem* ABI surface.
Internal alloc<T>() signature is unchanged so Vec/String/Box stay stable.

Co-Authored-By: Claude Fable 5 <[email protected]>
Copilot AI review requested due to automatic review settings July 20, 2026 16:06

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread std/mem/alloc.ag Resolved
Comment thread std/mem/alloc.ag Resolved
Comment thread std/mem/alloc.ag Resolved
@CierCier
CierCier merged commit bc6b536 into main Jul 20, 2026
2 checks passed
@CierCier
CierCier deleted the std/mmap-allocator branch July 21, 2026 10:16
CierCier added a commit that referenced this pull request Aug 1, 2026
Add mmap allocator + pure-Silver memory intrinsics (Wave 1 unit 2)
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.

2 participants