mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 19:38:12 +00:00
LibCore+AK: Move MappedFile from AK to LibCore
MappedFile is strictly a userspace thing, so it doesn't belong in AK (which is supposed to be user/kernel agnostic.)
This commit is contained in:
parent
c1c9da6c35
commit
58fb3ebf66
48 changed files with 101 additions and 103 deletions
67
Userland/Libraries/LibCore/MappedFile.cpp
Normal file
67
Userland/Libraries/LibCore/MappedFile.cpp
Normal file
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ScopeGuard.h>
|
||||
#include <AK/String.h>
|
||||
#include <LibCore/MappedFile.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace Core {
|
||||
|
||||
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map(String const& path)
|
||||
{
|
||||
int fd = open(path.characters(), O_RDONLY | O_CLOEXEC, 0);
|
||||
if (fd < 0)
|
||||
return Error::from_errno(errno);
|
||||
|
||||
return map_from_fd_and_close(fd, path);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path)
|
||||
{
|
||||
if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0)
|
||||
return Error::from_errno(errno);
|
||||
|
||||
ScopeGuard fd_close_guard = [fd] {
|
||||
close(fd);
|
||||
};
|
||||
|
||||
struct stat st;
|
||||
if (fstat(fd, &st) < 0)
|
||||
return Error::from_errno(errno);
|
||||
|
||||
auto size = st.st_size;
|
||||
auto* ptr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
|
||||
|
||||
if (ptr == MAP_FAILED)
|
||||
return Error::from_errno(errno);
|
||||
|
||||
#ifdef __serenity__
|
||||
if (set_mmap_name(ptr, size, path.characters()) < 0) {
|
||||
perror("set_mmap_name");
|
||||
}
|
||||
#endif
|
||||
|
||||
return adopt_ref(*new MappedFile(ptr, size));
|
||||
}
|
||||
|
||||
MappedFile::MappedFile(void* ptr, size_t size)
|
||||
: m_data(ptr)
|
||||
, m_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
MappedFile::~MappedFile()
|
||||
{
|
||||
auto rc = munmap(m_data, m_size);
|
||||
VERIFY(rc == 0);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue