summaryrefslogtreecommitdiff
path: root/AK/MappedFile.cpp
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-04-03 14:15:35 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-04-03 14:15:35 +0200
commitc0009e3173da25eb26a62ac251962ba7e810ec41 (patch)
tree0e8f205a79aef1bd8aba6eec5f669e0afaf2db7c /AK/MappedFile.cpp
parent3dc3754cdebd369fea0e57d899bf0e4ee2debc2b (diff)
downloadserenity-c0009e3173da25eb26a62ac251962ba7e810ec41.zip
PNGLoader: Use MappedFile.
Diffstat (limited to 'AK/MappedFile.cpp')
-rw-r--r--AK/MappedFile.cpp24
1 files changed, 15 insertions, 9 deletions
diff --git a/AK/MappedFile.cpp b/AK/MappedFile.cpp
index e279ad12e8..af5a77020d 100644
--- a/AK/MappedFile.cpp
+++ b/AK/MappedFile.cpp
@@ -5,25 +5,29 @@
#include <unistd.h>
#include <stdio.h>
+//#define DEBUG_MAPPED_FILE
+
namespace AK {
MappedFile::MappedFile(const String& file_name)
: m_file_name(file_name)
{
- m_file_length = PAGE_SIZE;
+ m_size = PAGE_SIZE;
m_fd = open(m_file_name.characters(), O_RDONLY);
if (m_fd != -1) {
struct stat st;
fstat(m_fd, &st);
- m_file_length = st.st_size;
- m_map = mmap(nullptr, m_file_length, PROT_READ, MAP_SHARED, m_fd, 0);
+ m_size = st.st_size;
+ m_map = mmap(nullptr, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
if (m_map == MAP_FAILED)
perror("");
}
- 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);
+#ifdef DEBUG_MAPPED_FILE
+ dbgprintf("MappedFile{%s} := { m_fd=%d, m_size=%u, m_map=%p }\n", m_file_name.characters(), m_fd, m_size, m_map);
+#endif
}
MappedFile::~MappedFile()
@@ -36,21 +40,23 @@ void MappedFile::unmap()
if (!is_valid())
return;
ASSERT(m_fd != -1);
- int rc = munmap(m_map, m_file_length);
+ int rc = munmap(m_map, m_size);
+ ASSERT(rc == 0);
+ rc = close(m_fd);
ASSERT(rc == 0);
m_file_name = { };
- m_file_length = 0;
+ m_size = 0;
m_fd = -1;
m_map = (void*)-1;
}
MappedFile::MappedFile(MappedFile&& other)
: m_file_name(move(other.m_file_name))
- , m_file_length(other.m_file_length)
+ , m_size(other.m_size)
, m_fd(other.m_fd)
, m_map(other.m_map)
{
- other.m_file_length = 0;
+ other.m_size = 0;
other.m_fd = -1;
other.m_map = (void*)-1;
}
@@ -61,7 +67,7 @@ MappedFile& MappedFile::operator=(MappedFile&& other)
return *this;
unmap();
swap(m_file_name, other.m_file_name);
- swap(m_file_length, other.m_file_length);
+ swap(m_size, other.m_size);
swap(m_fd, other.m_fd);
swap(m_map, other.m_map);
return *this;