blob: ff34d006c3236634742ee39dcc9cd85e9f634bd3 [file] [log] [blame]
William A. Kennington IIIbd1e0352023-12-22 16:44:55 -08001#include <sys/mman.h>
2
3#include <stdplus/fd/managed.hpp>
4#include <stdplus/fd/ops.hpp>
5#include <stdplus/util/cexec.hpp>
6
7#include <algorithm>
8#include <string_view>
9
10namespace stdplus::fd
11{
12
13inline ManagedFd makeMemfd(std::string_view contents)
14{
15 auto fd = ManagedFd(CHECK_ERRNO(memfd_create("test", 0), "memfd_create"));
16 write(fd, contents);
17 lseek(fd, 0, Whence::Set);
18 return fd;
19}
20
21constexpr auto readSv(std::string_view s) noexcept
22{
23 return [s](std::span<std::byte> buf) {
24 auto end = std::copy_n(reinterpret_cast<const std::byte*>(s.data()),
25 std::min(buf.size(), s.size()), buf.begin());
26 return std::span(buf.begin(), end);
27 };
28}
29
30} // namespace stdplus::fd