mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-14 20:03:29 +00:00
LibCore: Convert AnonymousBuffer to use System::anon_create
This commit is contained in:
parent
cf1f58d51c
commit
6409618413
|
@ -6,44 +6,16 @@
|
|||
|
||||
#include <AK/Try.h>
|
||||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/File.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
#if defined(__serenity__)
|
||||
# include <serenity.h>
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) && !defined(MFD_CLOEXEC)
|
||||
# include <linux/memfd.h>
|
||||
# include <sys/syscall.h>
|
||||
|
||||
static int memfd_create(const char* name, unsigned int flags)
|
||||
{
|
||||
return syscall(SYS_memfd_create, name, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)
|
||||
{
|
||||
int fd = -1;
|
||||
#if defined(__serenity__)
|
||||
fd = anon_create(round_up_to_power_of_two(size, PAGE_SIZE), O_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return Error::from_errno(errno);
|
||||
#elif defined(__linux__)
|
||||
fd = memfd_create("", MFD_CLOEXEC);
|
||||
if (fd < 0)
|
||||
return Error::from_errno(errno);
|
||||
if (ftruncate(fd, size) < 0) {
|
||||
close(fd);
|
||||
return Error::from_errno(errno);
|
||||
}
|
||||
#endif
|
||||
if (fd < 0)
|
||||
return Error::from_errno(errno);
|
||||
auto fd = TRY(Core::System::anon_create(size, O_CLOEXEC));
|
||||
return create_from_anon_fd(fd, size);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue