mirror of
https://github.com/fergalmoran/ladybird.git
synced 2025-12-22 09:19:03 +00:00
Everywhere: Hoist the Libraries folder to the top-level
This commit is contained in:
committed by
Andreas Kling
parent
950e819ee7
commit
93712b24bf
53
Libraries/LibCore/AnonymousBuffer.cpp
Normal file
53
Libraries/LibCore/AnonymousBuffer.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <andreas@ladybird.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Try.h>
|
||||
#include <LibCore/AnonymousBuffer.h>
|
||||
#include <LibCore/System.h>
|
||||
#include <LibIPC/File.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_with_size(size_t size)
|
||||
{
|
||||
auto fd = TRY(Core::System::anon_create(size, O_CLOEXEC));
|
||||
return create_from_anon_fd(fd, size);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<AnonymousBufferImpl>> AnonymousBufferImpl::create(int fd, size_t size)
|
||||
{
|
||||
auto* data = mmap(nullptr, round_up_to_power_of_two(size, PAGE_SIZE), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||
if (data == MAP_FAILED)
|
||||
return Error::from_errno(errno);
|
||||
return AK::adopt_nonnull_ref_or_enomem(new (nothrow) AnonymousBufferImpl(fd, size, data));
|
||||
}
|
||||
|
||||
AnonymousBufferImpl::~AnonymousBufferImpl()
|
||||
{
|
||||
if (m_fd != -1) {
|
||||
auto rc = close(m_fd);
|
||||
VERIFY(rc == 0);
|
||||
}
|
||||
auto rc = munmap(m_data, round_up_to_power_of_two(m_size, PAGE_SIZE));
|
||||
VERIFY(rc == 0);
|
||||
}
|
||||
|
||||
ErrorOr<AnonymousBuffer> AnonymousBuffer::create_from_anon_fd(int fd, size_t size)
|
||||
{
|
||||
auto impl = TRY(AnonymousBufferImpl::create(fd, size));
|
||||
return AnonymousBuffer(move(impl));
|
||||
}
|
||||
|
||||
AnonymousBufferImpl::AnonymousBufferImpl(int fd, size_t size, void* data)
|
||||
: m_fd(fd)
|
||||
, m_size(size)
|
||||
, m_data(data)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user