1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:17:44 +00:00

LibCore: Add Darwin anon_create support

This commit is contained in:
Filiph Sandström 2022-02-13 00:28:43 +01:00 committed by Linus Groh
parent 7e63f0eb32
commit 3ebb3d9d52

View file

@ -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);