mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
AK: Do not keep an open FD in MmappedFile
We only need an open FD while establishing the mapping, and can close it immediately after mmap() call.
This commit is contained in:
parent
837b476283
commit
55d7810fab
2 changed files with 15 additions and 18 deletions
|
@ -13,21 +13,26 @@ namespace AK {
|
||||||
MappedFile::MappedFile(const StringView& file_name)
|
MappedFile::MappedFile(const StringView& file_name)
|
||||||
{
|
{
|
||||||
m_size = PAGE_SIZE;
|
m_size = PAGE_SIZE;
|
||||||
m_fd = open_with_path_length(file_name.characters_without_null_termination(), file_name.length(), O_RDONLY | O_CLOEXEC, 0);
|
int fd = open_with_path_length(file_name.characters_without_null_termination(), file_name.length(), O_RDONLY | O_CLOEXEC, 0);
|
||||||
|
|
||||||
if (m_fd != -1) {
|
if (fd == -1) {
|
||||||
struct stat st;
|
perror("open");
|
||||||
fstat(m_fd, &st);
|
return;
|
||||||
m_size = st.st_size;
|
|
||||||
m_map = mmap(nullptr, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
|
|
||||||
|
|
||||||
if (m_map == MAP_FAILED)
|
|
||||||
perror("");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct stat st;
|
||||||
|
fstat(fd, &st);
|
||||||
|
m_size = st.st_size;
|
||||||
|
m_map = mmap(nullptr, m_size, PROT_READ, MAP_SHARED, fd, 0);
|
||||||
|
|
||||||
|
if (m_map == MAP_FAILED)
|
||||||
|
perror("mmap");
|
||||||
|
|
||||||
#ifdef DEBUG_MAPPED_FILE
|
#ifdef DEBUG_MAPPED_FILE
|
||||||
dbgprintf("MappedFile{%s} := { m_fd=%d, m_size=%u, m_map=%p }\n", file_name.characters(), m_fd, m_size, m_map);
|
dbgprintf("MappedFile{%s} := { fd=%d, m_size=%u, m_map=%p }\n", file_name.characters(), fd, m_size, m_map);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
close(fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::~MappedFile()
|
MappedFile::~MappedFile()
|
||||||
|
@ -39,23 +44,17 @@ void MappedFile::unmap()
|
||||||
{
|
{
|
||||||
if (!is_valid())
|
if (!is_valid())
|
||||||
return;
|
return;
|
||||||
ASSERT(m_fd != -1);
|
|
||||||
int rc = munmap(m_map, m_size);
|
int rc = munmap(m_map, m_size);
|
||||||
ASSERT(rc == 0);
|
ASSERT(rc == 0);
|
||||||
rc = close(m_fd);
|
|
||||||
ASSERT(rc == 0);
|
|
||||||
m_size = 0;
|
m_size = 0;
|
||||||
m_fd = -1;
|
|
||||||
m_map = (void*)-1;
|
m_map = (void*)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::MappedFile(MappedFile&& other)
|
MappedFile::MappedFile(MappedFile&& other)
|
||||||
: m_size(other.m_size)
|
: m_size(other.m_size)
|
||||||
, m_fd(other.m_fd)
|
|
||||||
, m_map(other.m_map)
|
, m_map(other.m_map)
|
||||||
{
|
{
|
||||||
other.m_size = 0;
|
other.m_size = 0;
|
||||||
other.m_fd = -1;
|
|
||||||
other.m_map = (void*)-1;
|
other.m_map = (void*)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +64,6 @@ MappedFile& MappedFile::operator=(MappedFile&& other)
|
||||||
return *this;
|
return *this;
|
||||||
unmap();
|
unmap();
|
||||||
swap(m_size, other.m_size);
|
swap(m_size, other.m_size);
|
||||||
swap(m_fd, other.m_fd);
|
|
||||||
swap(m_map, other.m_map);
|
swap(m_map, other.m_map);
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,6 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
size_t m_size { 0 };
|
size_t m_size { 0 };
|
||||||
int m_fd { -1 };
|
|
||||||
void* m_map { (void*)-1 };
|
void* m_map { (void*)-1 };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue