2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-03 14:51:49 +03:00
|
|
|
#include <AK/MappedFile.h>
|
2020-07-31 01:13:15 +03:00
|
|
|
#include <AK/String.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdio.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2019-04-03 14:51:49 +03:00
|
|
|
#include <unistd.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2019-04-03 15:15:35 +03:00
|
|
|
//#define DEBUG_MAPPED_FILE
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
namespace AK {
|
|
|
|
|
2019-06-02 15:58:02 +03:00
|
|
|
MappedFile::MappedFile(const StringView& file_name)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2019-08-05 15:26:56 +03:00
|
|
|
int fd = open_with_path_length(file_name.characters_without_null_termination(), file_name.length(), O_RDONLY | O_CLOEXEC, 0);
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-08-05 15:26:56 +03:00
|
|
|
if (fd == -1) {
|
2020-07-22 18:19:47 +03:00
|
|
|
m_errno = errno;
|
2019-08-05 15:26:56 +03:00
|
|
|
perror("open");
|
|
|
|
return;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 15:26:56 +03:00
|
|
|
struct stat st;
|
|
|
|
fstat(fd, &st);
|
|
|
|
m_size = st.st_size;
|
|
|
|
m_map = mmap(nullptr, m_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
|
|
|
2020-07-22 18:19:47 +03:00
|
|
|
if (m_map == MAP_FAILED) {
|
|
|
|
m_errno = errno;
|
2019-08-05 15:26:56 +03:00
|
|
|
perror("mmap");
|
2020-07-22 18:19:47 +03:00
|
|
|
}
|
2019-08-05 15:26:56 +03:00
|
|
|
|
2019-04-03 15:15:35 +03:00
|
|
|
#ifdef DEBUG_MAPPED_FILE
|
2020-08-27 01:30:19 +03:00
|
|
|
dbgprintf("MappedFile{%s} := { fd=%d, m_size=%zu, m_map=%p }\n", file_name.to_string().characters(), fd, m_size, m_map);
|
2019-04-03 15:15:35 +03:00
|
|
|
#endif
|
2019-08-05 15:26:56 +03:00
|
|
|
|
|
|
|
close(fd);
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MappedFile::~MappedFile()
|
|
|
|
{
|
2019-04-03 14:51:49 +03:00
|
|
|
unmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
void MappedFile::unmap()
|
|
|
|
{
|
|
|
|
if (!is_valid())
|
|
|
|
return;
|
2019-04-03 15:15:35 +03:00
|
|
|
int rc = munmap(m_map, m_size);
|
|
|
|
ASSERT(rc == 0);
|
|
|
|
m_size = 0;
|
2019-04-03 14:51:49 +03:00
|
|
|
m_map = (void*)-1;
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MappedFile::MappedFile(MappedFile&& other)
|
2019-06-02 15:58:02 +03:00
|
|
|
: m_size(other.m_size)
|
2018-10-10 12:53:07 +03:00
|
|
|
, m_map(other.m_map)
|
|
|
|
{
|
2019-04-03 15:15:35 +03:00
|
|
|
other.m_size = 0;
|
2018-10-10 12:53:07 +03:00
|
|
|
other.m_map = (void*)-1;
|
|
|
|
}
|
|
|
|
|
2019-04-03 14:51:49 +03:00
|
|
|
MappedFile& MappedFile::operator=(MappedFile&& other)
|
|
|
|
{
|
|
|
|
if (this == &other)
|
|
|
|
return *this;
|
|
|
|
unmap();
|
2019-04-03 15:15:35 +03:00
|
|
|
swap(m_size, other.m_size);
|
2019-04-03 14:51:49 +03:00
|
|
|
swap(m_map, other.m_map);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|