mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 21:57:35 +00:00
PNGLoader: Use MappedFile.
This commit is contained in:
parent
3dc3754cde
commit
c0009e3173
3 changed files with 21 additions and 46 deletions
|
@ -5,25 +5,29 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
//#define DEBUG_MAPPED_FILE
|
||||||
|
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
MappedFile::MappedFile(const String& file_name)
|
MappedFile::MappedFile(const String& file_name)
|
||||||
: m_file_name(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);
|
m_fd = open(m_file_name.characters(), O_RDONLY);
|
||||||
|
|
||||||
if (m_fd != -1) {
|
if (m_fd != -1) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
fstat(m_fd, &st);
|
fstat(m_fd, &st);
|
||||||
m_file_length = st.st_size;
|
m_size = st.st_size;
|
||||||
m_map = mmap(nullptr, m_file_length, PROT_READ, MAP_SHARED, m_fd, 0);
|
m_map = mmap(nullptr, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
|
||||||
|
|
||||||
if (m_map == MAP_FAILED)
|
if (m_map == MAP_FAILED)
|
||||||
perror("");
|
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()
|
MappedFile::~MappedFile()
|
||||||
|
@ -36,21 +40,23 @@ void MappedFile::unmap()
|
||||||
if (!is_valid())
|
if (!is_valid())
|
||||||
return;
|
return;
|
||||||
ASSERT(m_fd != -1);
|
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);
|
ASSERT(rc == 0);
|
||||||
m_file_name = { };
|
m_file_name = { };
|
||||||
m_file_length = 0;
|
m_size = 0;
|
||||||
m_fd = -1;
|
m_fd = -1;
|
||||||
m_map = (void*)-1;
|
m_map = (void*)-1;
|
||||||
}
|
}
|
||||||
|
|
||||||
MappedFile::MappedFile(MappedFile&& other)
|
MappedFile::MappedFile(MappedFile&& other)
|
||||||
: m_file_name(move(other.m_file_name))
|
: 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_fd(other.m_fd)
|
||||||
, m_map(other.m_map)
|
, m_map(other.m_map)
|
||||||
{
|
{
|
||||||
other.m_file_length = 0;
|
other.m_size = 0;
|
||||||
other.m_fd = -1;
|
other.m_fd = -1;
|
||||||
other.m_map = (void*)-1;
|
other.m_map = (void*)-1;
|
||||||
}
|
}
|
||||||
|
@ -61,7 +67,7 @@ MappedFile& MappedFile::operator=(MappedFile&& other)
|
||||||
return *this;
|
return *this;
|
||||||
unmap();
|
unmap();
|
||||||
swap(m_file_name, other.m_file_name);
|
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_fd, other.m_fd);
|
||||||
swap(m_map, other.m_map);
|
swap(m_map, other.m_map);
|
||||||
return *this;
|
return *this;
|
||||||
|
|
|
@ -18,11 +18,11 @@ public:
|
||||||
|
|
||||||
void* pointer() { return m_map; }
|
void* pointer() { return m_map; }
|
||||||
const void* pointer() const { return m_map; }
|
const void* pointer() const { return m_map; }
|
||||||
size_t file_length() const { return m_file_length; }
|
size_t size() const { return m_size; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
String m_file_name;
|
String m_file_name;
|
||||||
size_t m_file_length { 0 };
|
size_t m_size { 0 };
|
||||||
int m_fd { -1 };
|
int m_fd { -1 };
|
||||||
void* m_map { (void*)-1 };
|
void* m_map { (void*)-1 };
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#include <SharedGraphics/PNGLoader.h>
|
#include <SharedGraphics/PNGLoader.h>
|
||||||
#include <AK/NetworkOrdered.h>
|
#include <AK/NetworkOrdered.h>
|
||||||
|
#include <AK/MappedFile.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
@ -101,42 +102,10 @@ static bool process_chunk(Streamer&, PNGLoadingContext& context);
|
||||||
|
|
||||||
RetainPtr<GraphicsBitmap> load_png(const String& path)
|
RetainPtr<GraphicsBitmap> load_png(const String& path)
|
||||||
{
|
{
|
||||||
int fd = open(path.characters(), O_RDONLY);
|
MappedFile mapped_file(path);
|
||||||
if (fd < 0) {
|
if (!mapped_file.is_valid())
|
||||||
perror("open");
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
return load_png_impl((const byte*)mapped_file.pointer(), mapped_file.size());
|
||||||
|
|
||||||
struct stat st;
|
|
||||||
if (fstat(fd, &st) < 0) {
|
|
||||||
perror("fstat");
|
|
||||||
if (close(fd) < 0)
|
|
||||||
perror("close");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (st.st_size < 8) {
|
|
||||||
if (close(fd) < 0)
|
|
||||||
perror("close");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* mapped_file = (byte*)mmap(nullptr, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
|
|
||||||
if (mapped_file == MAP_FAILED) {
|
|
||||||
if (close(fd) < 0)
|
|
||||||
perror("close");
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto bitmap = load_png_impl(mapped_file, st.st_size);
|
|
||||||
|
|
||||||
if (munmap(mapped_file, st.st_size) < 0)
|
|
||||||
perror("munmap");
|
|
||||||
|
|
||||||
if (close(fd) < 0)
|
|
||||||
perror("close");
|
|
||||||
|
|
||||||
return bitmap;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[[gnu::always_inline]] static inline byte paeth_predictor(int a, int b, int c)
|
[[gnu::always_inline]] static inline byte paeth_predictor(int a, int b, int c)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue