Skip to content

Commit

Permalink
Font: Clean up AK::MappedFile and use it for mapping font files.
Browse files Browse the repository at this point in the history
  • Loading branch information
awesomekling committed Apr 3, 2019
1 parent ab6bd38 commit 3dc3754
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 37 deletions.
45 changes: 34 additions & 11 deletions AK/MappedFile.cpp
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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)
Expand All @@ -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;
}

}

5 changes: 4 additions & 1 deletion AK/MappedFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
1 change: 1 addition & 0 deletions LibC/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ AK_OBJS = \
../AK/StringBuilder.o \
../AK/FileSystemPath.o \
../AK/StdLibExtras.o \
../AK/MappedFile.o \
../AK/kmalloc.o

LIBC_OBJS = \
Expand Down
27 changes: 5 additions & 22 deletions SharedGraphics/Font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <AK/kmalloc.h>
#include <AK/BufferStream.h>
#include <AK/StdLibExtras.h>

#include <AK/MappedFile.h>
#include <LibC/unistd.h>
#include <LibC/stdio.h>
#include <LibC/fcntl.h>
Expand Down Expand Up @@ -90,10 +90,6 @@ Font::Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width

Font::~Font()
{
if (m_mmap_ptr) {
int rc = munmap(m_mmap_ptr, 4096 * 3);
ASSERT(rc == 0);
}
}

RetainPtr<Font> Font::load_from_memory(const byte* data)
Expand All @@ -119,25 +115,12 @@ RetainPtr<Font> Font::load_from_memory(const byte* data)

RetainPtr<Font> Font::load_from_file(const String& path)
{
int fd = open(path.characters(), O_RDONLY, 0644);
if (fd < 0) {
dbgprintf("open(%s) got fd=%d, failed: %s\n", path.characters(), fd, strerror(errno));
perror("open");
MappedFile mapped_file(path);
if (!mapped_file.is_valid())
return nullptr;
}

auto* mapped_file = (byte*)mmap(nullptr, 4096 * 3, PROT_READ, MAP_SHARED, fd, 0);
if (mapped_file == MAP_FAILED) {
int rc = close(fd);
ASSERT(rc == 0);
return nullptr;
}

auto font = load_from_memory(mapped_file);
font->m_mmap_ptr = mapped_file;

int rc = close(fd);
ASSERT(rc == 0);
auto font = load_from_memory((const byte*)mapped_file.pointer());
font->m_mapped_file = move(mapped_file);
return font;
}

Expand Down
7 changes: 4 additions & 3 deletions SharedGraphics/Font.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <AK/Retainable.h>
#include <AK/RetainPtr.h>
#include <AK/AKString.h>
#include <AK/MappedFile.h>
#include <AK/Types.h>

// FIXME: Make a MutableGlyphBitmap buddy class for FontEditor instead?
Expand Down Expand Up @@ -47,8 +48,6 @@ class Font : public Retainable<Font> {

RetainPtr<Font> clone() const;

static RetainPtr<Font> load_from_memory(const byte*);

static RetainPtr<Font> load_from_file(const String& path);
bool write_to_file(const String& path);

Expand Down Expand Up @@ -78,11 +77,13 @@ class Font : public Retainable<Font> {
private:
Font(const String& name, unsigned* rows, byte* widths, bool is_fixed_width, byte glyph_width, byte glyph_height);

static RetainPtr<Font> load_from_memory(const byte*);

String m_name;

unsigned* m_rows { nullptr };
byte* m_glyph_widths { nullptr };
void* m_mmap_ptr { nullptr };
MappedFile m_mapped_file;

byte m_glyph_width { 0 };
byte m_glyph_height { 0 };
Expand Down

0 comments on commit 3dc3754

Please sign in to comment.