diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-03 13:51:49 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-03 13:51:49 +0200 |
commit | 3dc3754cdebd369fea0e57d899bf0e4ee2debc2b (patch) | |
tree | 16a868b3060243f8f003843b4c249f5df9d770b8 /AK | |
parent | ab6bd3872bd4024a008746dc2c36a59ff8522d2c (diff) | |
download | serenity-3dc3754cdebd369fea0e57d899bf0e4ee2debc2b.zip |
Font: Clean up AK::MappedFile and use it for mapping font files.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/MappedFile.cpp | 45 | ||||
-rw-r--r-- | AK/MappedFile.h | 5 |
2 files changed, 38 insertions, 12 deletions
diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp index 3cf7570bc1..e279ad12e8 100644 --- a/AK/MappedFile.cpp +++ b/AK/MappedFile.cpp @@ -1,15 +1,16 @@ -#include "MappedFile.h" +#include <AK/MappedFile.h> #include <sys/mman.h> #include <sys/stat.h> #include <fcntl.h> -#include <cstdio> +#include <unistd.h> +#include <stdio.h> namespace AK { -MappedFile::MappedFile(String&& file_name) - : m_file_name(std::move(file_name)) +MappedFile::MappedFile(const String& file_name) + : m_file_name(file_name) { - m_file_length = 1024; + m_file_length = PAGE_SIZE; m_fd = open(m_file_name.characters(), O_RDONLY); if (m_fd != -1) { @@ -22,19 +23,29 @@ MappedFile::MappedFile(String&& file_name) perror(""); } - printf("MappedFile{%s} := { m_fd=%d, m_file_length=%zu, m_map=%p }\n", m_file_name.characters(), m_fd, m_file_length, m_map); + dbgprintf("MappedFile{%s} := { m_fd=%d, m_file_length=%zu, m_map=%p }\n", m_file_name.characters(), m_fd, m_file_length, m_map); } MappedFile::~MappedFile() { - if (m_map != (void*)-1) { - ASSERT(m_fd != -1); - munmap(m_map, m_file_length); - } + unmap(); +} + +void MappedFile::unmap() +{ + if (!is_valid()) + return; + ASSERT(m_fd != -1); + int rc = munmap(m_map, m_file_length); + ASSERT(rc == 0); + m_file_name = { }; + m_file_length = 0; + m_fd = -1; + m_map = (void*)-1; } MappedFile::MappedFile(MappedFile&& other) - : m_file_name(std::move(other.m_file_name)) + : m_file_name(move(other.m_file_name)) , m_file_length(other.m_file_length) , m_fd(other.m_fd) , m_map(other.m_map) @@ -44,5 +55,17 @@ MappedFile::MappedFile(MappedFile&& other) other.m_map = (void*)-1; } +MappedFile& MappedFile::operator=(MappedFile&& other) +{ + if (this == &other) + return *this; + unmap(); + swap(m_file_name, other.m_file_name); + swap(m_file_length, other.m_file_length); + swap(m_fd, other.m_fd); + swap(m_map, other.m_map); + return *this; +} + } diff --git a/AK/MappedFile.h b/AK/MappedFile.h index 5eee9ed939..246913a27d 100644 --- a/AK/MappedFile.h +++ b/AK/MappedFile.h @@ -7,11 +7,14 @@ namespace AK { class MappedFile { public: MappedFile() { } - explicit MappedFile(String&& file_name); + explicit MappedFile(const String& file_name); MappedFile(MappedFile&&); ~MappedFile(); + MappedFile& operator=(MappedFile&&); + bool is_valid() const { return m_map != (void*)-1; } + void unmap(); void* pointer() { return m_map; } const void* pointer() const { return m_map; } |