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

LibCore: Add syscall wrapper for mmap()

For convenience on SerenityOS, this also takes a custom alignment
request, and a memory region name. These are non-POSIX extensions.
This commit is contained in:
Andreas Kling 2021-11-23 11:47:32 +01:00
parent dd6e73176d
commit 53e9b9758e
2 changed files with 20 additions and 0 deletions

View file

@ -8,6 +8,7 @@
#include <LibSystem/syscall.h>
#include <fcntl.h>
#include <stdarg.h>
#include <sys/mman.h>
#define HANDLE_SYSCALL_RETURN_VALUE(syscall_name, rc, success_value) \
if ((rc) < 0) { \
@ -66,4 +67,22 @@ ErrorOr<int> fcntl(int fd, int command, ...)
return rc;
}
ErrorOr<void*> mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, [[maybe_unused]] size_t alignment, [[maybe_unused]] StringView name)
{
#ifdef __serenity__
Syscall::SC_mmap_params params { (uintptr_t)address, size, alignment, protection, flags, fd, offset, { name.characters_without_null_termination(), name.length() } };
ptrdiff_t rc = syscall(SC_mmap, &params);
if (rc < 0 && rc > -EMAXERRNO)
return Error::from_syscall("mmap"sv, rc);
return reinterpret_cast<void*>(rc);
#else
// NOTE: Regular POSIX mmap() doesn't support custom alignment requests.
VERIFY(!alignment);
auto* ptr = ::mmap(address, size, protection, flags, fd, offset);
if (ptr == MAP_FAILED)
return Error::from_syscall("mmap"sv, -errno);
return ptr;
#endif
}
}