Skip to content

Abstract mmap/munmap behind platform guards for WASM and embedded targets#50

Open
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
cluesurf:platform-abstract-mmap
Open

Abstract mmap/munmap behind platform guards for WASM and embedded targets#50
lancejpollard wants to merge 1 commit intoHigherOrderCO:mainfrom
cluesurf:platform-abstract-mmap

Conversation

@lancejpollard
Copy link
Copy Markdown

@lancejpollard lancejpollard commented Mar 2, 2026

Adds #ifdef guards to sys/mmap_anon.c and sys/munmap_anon.c that fall back to calloc/free when __EMSCRIPTEN__ or HVM_NO_MMAP is defined.

Benefits

  • Enables compiling HVM4 to WASM via Emscripten for browser-based IC evaluation.
  • Enables compiling for embedded targets without virtual memory.
  • Establishes a platform abstraction pattern for future targets (CUDA WNF in CUDA #33, Metal WNF in Metal #30).

Why calloc (not malloc)

mmap(MAP_ANONYMOUS) returns zero-initialized pages. Several callers depend on this: uset_init uses the allocation as a bitset (all bits must start clear), and the heap is expected to start zeroed. calloc preserves this guarantee.

Note on lazy allocation

mmap with MAP_NORESERVE allocates virtual address space lazily (physical pages allocated on first access). calloc does not have this property. On platforms using this fallback, HEAP_CAP should be reduced to a size that fits in physical memory.

Changes

Two files, 29 lines added, 0 removed from existing code paths. The existing mmap code is wrapped in #else and is unchanged.

  • clang/sys/mmap_anon.c: calloc(1, bytes) fallback
  • clang/sys/munmap_anon.c: free(ptr) fallback

Risk

Don't think there are any on existing platforms. Guards only trigger when __EMSCRIPTEN__ or HVM_NO_MMAP is defined. Neither is defined in normal desktop/server builds. All existing tests pass.

…gets

Add #ifdef guards to sys/mmap_anon.c and sys/munmap_anon.c that fall
back to calloc/free on platforms without mmap (__EMSCRIPTEN__ or
HVM_NO_MMAP). Uses calloc to preserve mmap's zero-initialization
guarantee.

Existing behavior on POSIX platforms is unchanged.
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