diff options
author | Filiph Sandström <filiph.sandstrom@filfatstudios.com> | 2022-02-13 00:28:43 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-02-13 17:54:34 +0000 |
commit | 3ebb3d9d52bbfae294572542490634e49b4374a4 (patch) | |
tree | 097475d8cf3cb304ccaca852081a573090291a4d /Userland/Libraries/LibCore | |
parent | 7e63f0eb32be639fd35e9f3c99466a3a2ab4fac0 (diff) | |
download | serenity-3ebb3d9d52bbfae294572542490634e49b4374a4.zip |
LibCore: Add Darwin anon_create support
Diffstat (limited to 'Userland/Libraries/LibCore')
-rw-r--r-- | Userland/Libraries/LibCore/System.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index 872bbab715..70c93e27ba 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -35,6 +35,10 @@ static int memfd_create(const char* name, unsigned int flags) } #endif +#if defined(__APPLE__) +# include <sys/mman.h> +#endif + #define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \ if ((rc) < 0) { \ return Error::from_syscall(syscall_name, rc); \ @@ -290,6 +294,33 @@ ErrorOr<int> anon_create([[maybe_unused]] size_t size, [[maybe_unused]] int opti TRY(close(fd)); return Error::from_errno(saved_errno); } +#elif defined(__APPLE__) + struct timespec time; + clock_gettime(CLOCK_REALTIME, &time); + auto name = String::formatted("/shm-{}{}", (unsigned long)time.tv_sec, (unsigned long)time.tv_nsec); + fd = shm_open(name.characters(), O_RDWR | O_CREAT | options, 0600); + + if (shm_unlink(name.characters()) == -1) { + auto saved_errno = errno; + TRY(close(fd)); + return Error::from_errno(saved_errno); + } + + if (fd < 0) + return Error::from_errno(errno); + + if (::ftruncate(fd, size) < 0) { + auto saved_errno = errno; + TRY(close(fd)); + return Error::from_errno(saved_errno); + } + + void* addr = ::mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0); + if (addr == MAP_FAILED) { + auto saved_errno = errno; + TRY(close(fd)); + return Error::from_errno(saved_errno); + } #endif if (fd < 0) return Error::from_errno(errno); |