| 1 |
// SPDX-License-Identifier: MPL-2.0 |
| 2 |
// (c) Hare authors <https://harelang.org> |
| 3 |
|
| 4 |
use errors; |
| 5 |
use io; |
| 6 |
use rt; |
| 7 |
|
| 8 |
// Flags for [[memfd]]. |
| 9 |
export type memfd_flag = enum uint { |
| 10 |
NONE = 0, |
| 11 |
// Unsets the close-on-exec flag when creating a memfd. |
| 12 |
NOCLOEXEC = rt::MFD_CLOEXEC, |
| 13 |
// Allows sealing operations on this file. |
| 14 |
ALLOW_SEALING = rt::MFD_ALLOW_SEALING, |
| 15 |
// Create the memfd with huge pages using hugetlbfs. Linux-only. |
| 16 |
HUGETLB = rt::MFD_HUGETLB, |
| 17 |
}; |
| 18 |
|
| 19 |
// Creates a new anonymous [[io::file]] backed by memory. |
| 20 |
// |
| 21 |
// The initial file size is zero. It can be written to normally, or the size can |
| 22 |
// be set manually with [[io::trunc]]. |
| 23 |
// |
| 24 |
// This function is available on Linux and FreeBSD. |
| 25 |
export fn memfd( |
| 26 |
name: str, |
| 27 |
flags: memfd_flag = memfd_flag::NONE, |
| 28 |
) (io::file | errors::error) = { |
| 29 |
flags ^= memfd_flag::NOCLOEXEC; |
| 30 |
match (rt::memfd_create(name, flags: uint)) { |
| 31 |
case let i: int => |
| 32 |
return i: io::file; |
| 33 |
case let err: rt::errno => |
| 34 |
return errors::errno(err); |
| 35 |
}; |
| 36 |
}; |