William A. Kennington III | bd1e035 | 2023-12-22 16:44:55 -0800 | [diff] [blame] | 1 | #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 | |
| 10 | namespace stdplus::fd |
| 11 | { |
| 12 | |
| 13 | inline 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 | |
| 21 | constexpr 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 |