txt: ## dist_zef-massa-Mmap-Native.md
  [![Actions Status](https://github.com/massa/Mmap-Native/actions/workflows/linux.yml/badge.svg)](https://github.com/massa/Mmap-Native/actions) [![Actions Status](https://github.com/massa/Mmap-Native/actions/workflows/macos.yml/badge.svg)](https://github.com/massa/Mmap-Native/actions)
  
  # NAME
  
  Mmap::Native - interface to posix mmap() and mmunmap() calls
  
  # SYNOPSIS
  
  ```
  use Mmap::Native;
  
  my $f = $*CWD.add('META6.json'); $f = $f.open;
  my $d = mmap Pointer, 50, PROT_READ, MAP_SHARED, $f.native-descriptor, 0;
  my $e = blob-from-pointer $d, :50elems, :type(Buf[uint8]);
  
  is $e.decode('utf8'), /'"auth": "zef:massa"'/;
  
  ```
  
  # DESCRIPTION
  
  Mmap::Native is a raw interface to the C library `mmap` and `munmap` calls.
  
  It's mostly used for mmaping `Blob` objects and other raw C pointers. It's recommended that the library using it present a most Raku-esque interface and hide the `mmap`ping altogether. This is just a low level library.
  
  # CONSTANTS
  
  Remember to always use `+|` to combine flag values!
  
  ### The `$prot` argument flags
  
  Describes the desired memory protection of the mapping (and must not conflict with the open mode of the file). It is either PROT\_NONE or the bitwise OR of one or more of the following flags:
  
  | Flag | Description |
  | --- | --- |
  | PROT\_READ | page can be read |
  | PROT\_WRITE | page can be written |
  | PROT\_EXEC | page can be executed |
  | PROT\_SEM | page may be used for atomic ops |
  | PROT\_NONE | page can not be accessed |
  | PROT\_GROWSDOWN | mprotect flag: extend change to start of growsdown vma |
  | PROT\_GROWSUP | mprotect flag: extend change to end of growsup vma |
  
  ### The `$flags` argument
  
  Determines whether updates to the mapping are visible to other processes mapping the same region, and whether updates are carried through to the underlying file. This behavior is determined by including exactly one of the following values:
  
  | Flag | Description |
  | --- | --- |
  | MAP\_SHARED | Share changes |
  | MAP\_PRIVATE | Changes are private |
  | MAP\_SHARED\_VALIDATE | share and validate extension flags |
  | MAP\_TYPE | Mask for type of mapping |
  | MAP\_FIXED | Interpret addr exactly |
  | MAP\_ANONYMOUS | don't use a file |
  | MAP\_GROWSDOWN | stack-like segment |
  | MAP\_DENYWRITE | ETXTBSY |
  | MAP\_EXECUTABLE | mark it as an executable |
  | MAP\_LOCKED | pages are locked |
  | MAP\_NORESERVE | don't check for reservations |
  | MAP\_POPULATE | populate (prefault) pagetables |
  | MAP\_NONBLOCK | do not block on IO |
  | MAP\_STACK | give out an address that is best suited for process/thread stacks |
  | MAP\_HUGETLB | create a huge page mapping |
  | MAP\_SYNC | perform synchronous page faults for the mapping |
  | MAP\_FIXED\_NOREPLACE | MAP\_FIXED which doesn't unmap underlying mapping |
  | MAP\_UNINITIALIZED | For anonymous mmap, memory could be \* uninitialized |
  | MLOCK\_ONFAULT | Lock pages in range after they are faulted in, do not prefault |
  | MS\_ASYNC | sync memory asynchronously |
  | MS\_INVALIDATE | invalidate the caches |
  | MS\_SYNC | synchronous memory sync |
  | MADV\_NORMAL | no further special treatment |
  | MADV\_RANDOM | expect random page references |
  | MADV\_SEQUENTIAL | expect sequential page references |
  | MADV\_WILLNEED | will need these pages |
  | MADV\_DONTNEED | don't need these pages |
  | MADV\_FREE | free pages only if memory pressure |
  | MADV\_REMOVE | remove these pages & resources |
  | MADV\_DONTFORK | don't inherit across fork |
  | MADV\_DOFORK | do inherit across fork |
  | MADV\_HWPOISON | poison a page for testing |
  | MADV\_SOFT\_OFFLINE | soft offline page for testing |
  | MADV\_MERGEABLE | KSM may merge identical pages |
  | MADV\_UNMERGEABLE | KSM may not merge identical pages |
  | MADV\_HUGEPAGE | Worth backing with hugepages |
  | MADV\_NOHUGEPAGE | Not worth backing with hugepages |
  | MADV\_DONTDUMP | Explicity exclude from the core dump, overrides the coredump filter bits |
  | MADV\_DODUMP | Clear the MADV\_DONTDUMP flag |
  | MADV\_WIPEONFORK | Zero memory on fork, child only |
  | MADV\_KEEPONFORK | Undo MADV\_WIPEONFORK |
  | MADV\_COLD | deactivate these pages |
  | MADV\_PAGEOUT | reclaim these pages |
  | MADV\_POPULATE\_READ | populate (prefault) page tables readable |
  | MADV\_POPULATE\_WRITE | populate (prefault) page tables writable |
  | MADV\_DONTNEED\_LOCKED | like DONTNEED, but drop locked pages too |
  | MADV\_COLLAPSE | Synchronous hugepage collapse |
  
  # FUNCTIONS
  
  ### sub mmap
  
  ```
  sub mmap(
      NativeCall::Types::Pointer $addr is rw,
      uint64 $len,
      int64 $prot,
      int64 $flags,
      int64 $fd,
      int64 $offset
  ) returns NativeCall::Types::Pointer
  
  ```
  
  Creates a new mapping in the virtual address space of the calling process. The starting address for the new mapping is specified in `$addr`. The `$length` argument specifies the length of the mapping (which must be greater than 0). If `$addr` is undefined, then the kernel chooses the (page-aligned) address at which to create the mapping; this is the most portable method of creating a new mapping. If `$addr` is defined, then the kernel takes it as a hint about where to place the mapping; on Linux, the kernel will pick a nearby page boundary and attempt to create the mapping there. If another mapping already exists there, the kernel picks a new address that may or may not depend on the hint. The address of the new mapping is returned as the result of the call. The contents of a file mapping are initialized using length bytes starting at offset `$offset` in the file (or other object) referred to by the file descriptor `$fd`. `$offset` must be a multiple of the page size. After the `mmap()` call has returned, the file descriptor, `$fd`, can be closed immediately without invalidating the mapping. The `$prot` argument describes the desired memory protection of the mapping (and must not conflict with the open mode of the file).
  
  ### sub munmap
  
  ```
  sub munmap(
      NativeCall::Types::Pointer $addr is rw,
      uint64 $len
  ) returns uint64
  
  ```
  
  Deletes the mappings for the specified address range, and causes further references to addresses within the range to generate invalid memory references. The region is also automatically unmapped when the process is terminated. On the other hand, closing the file descriptor does not unmap the region. The address `$addr` must be a multiple of the page size (but `$length` need not be). All pages containing a part of the indicated range are unmapped, and subsequent references to these pages will generate `SIGSEGV`. It is not an error if the indicated range does not contain any mapped pages.
  
  # AUTHOR
  
  Humberto Massa `humbertomassa@gmail.com`
  
  # COPYRIGHT AND LICENSE
  
  Copyright © 2024 Humberto Massa
  
  This library is free software; you can redistribute it and/or modify it under either the Artistic License 2.0 or the LGPL v3.0, at your convenience.
  