Reduce code duplication. (#220)

* Reduce code duplication.

The Apple and Haiku PALs both had *almost* identical code to the POSIX
PAL, differing only in some small argument variables.  This is fragile
and easy to accidentally get out of sync.  For example, the changes to
`reserve_at_least` involved copying identical code into multiple PALs
and it's easy to accidentally miss one.

This change introduces two optional fields on POSIX-derived PALs:

 - `default_mmap_flags` allows a PAL to provide additional `MAP_*`
   flags to all `mmap` calls.
 - `anonymous_memory_fd` allows the PAL to override the default file
   descriptor used for memory mappings.

If a PAL does not provide these, default values are used.

Fixes #219
This commit is contained in:
David Chisnall
2020-06-30 13:33:50 +01:00
committed by GitHub
parent 4a3102fedb
commit 5199556263
4 changed files with 82 additions and 111 deletions

View File

@@ -243,6 +243,16 @@ The [Windows](src/pal/pal_windows.h), and
[FreeBSD kernel](src/pal/pal_freebsd_kernel.h) implementations give examples of
non-POSIX environments that snmalloc supports.
The POSIX PAL uses `mmap` to map memory.
Some POSIX or POSIX-like systems require minor tweaks to this behaviour.
Rather than requiring these to copy and paste the code, a PAL that inherits from the POSIX PAL can define one or both of these (`static constexpr`) fields to customise the `mmap` behaviour.
- `default_mmap_flags` allows a PAL to provide additional `MAP_*`
flags to all `mmap` calls.
- `anonymous_memory_fd` allows the PAL to override the default file
descriptor used for memory mappings.
# Contributing
This project welcomes contributions and suggestions. Most contributions require you to agree to a