forked from organicmaps/organicmaps-tmp
introduced memory_mapped_file class.
This commit is contained in:
parent
02671ea910
commit
e750083a53
5 changed files with 93 additions and 17 deletions
|
@ -15,7 +15,8 @@ SOURCES += base.cpp \
|
|||
string_utils.cpp \
|
||||
profiler.cpp \
|
||||
commands_queue.cpp \
|
||||
shared_buffer_manager.cpp
|
||||
shared_buffer_manager.cpp \
|
||||
memory_mapped_file.cpp
|
||||
|
||||
HEADERS += SRC_FIRST.hpp \
|
||||
assert.hpp \
|
||||
|
@ -53,4 +54,5 @@ HEADERS += SRC_FIRST.hpp \
|
|||
ptr_utils.hpp \
|
||||
stats.hpp \
|
||||
monitor.hpp \
|
||||
shared_buffer_manager.hpp
|
||||
shared_buffer_manager.hpp \
|
||||
memory_mapped_file.hpp
|
||||
|
|
47
base/memory_mapped_file.cpp
Normal file
47
base/memory_mapped_file.cpp
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include "../base/SRC_FIRST.hpp"
|
||||
#include "memory_mapped_file.hpp"
|
||||
|
||||
MemoryMappedFile::MemoryMappedFile(char const * fileName, bool isReadOnly)
|
||||
: m_isReadOnly(isReadOnly)
|
||||
{
|
||||
#ifdef OMIM_OS_WINDOWS_NATIVE
|
||||
m_fp = fopen(name, isReadOnly ? "r" : "w");
|
||||
fseek(m_fp, SEEK_END);
|
||||
m_size = ftell(m_fp);
|
||||
fseek(m_fp, SEEK_SET);
|
||||
|
||||
m_data = malloc(m_size);
|
||||
fread(m_data, 1, m_size, m_fp);
|
||||
#else
|
||||
struct stat s;
|
||||
stat(fileName, &s);
|
||||
m_size = s.st_size;
|
||||
m_fd = open(fileName, isReadOnly ? O_RDONLY : O_RDWR);
|
||||
m_data = mmap(0, m_size, isReadOnly ? PROT_READ : (PROT_READ | PROT_WRITE), MAP_SHARED, m_fd, 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
MemoryMappedFile::~MemoryMappedFile()
|
||||
{
|
||||
#ifdef OMIM_OS_WINDOWS_NATIVE
|
||||
if (!m_isReadOnly)
|
||||
{
|
||||
fwrite(m_data, 1, m_size, m_fp);
|
||||
}
|
||||
fclose(m_fp);
|
||||
free(m_data);
|
||||
#else
|
||||
munmap(m_data, m_size);
|
||||
close(m_fd);
|
||||
#endif
|
||||
}
|
||||
|
||||
void * MemoryMappedFile::data()
|
||||
{
|
||||
return m_data;
|
||||
}
|
||||
|
||||
size_t MemoryMappedFile::size() const
|
||||
{
|
||||
return m_size;
|
||||
}
|
33
base/memory_mapped_file.hpp
Normal file
33
base/memory_mapped_file.hpp
Normal file
|
@ -0,0 +1,33 @@
|
|||
#pragma once
|
||||
|
||||
#include "../std/target_os.hpp"
|
||||
|
||||
#ifdef OMIM_OS_WINDOWS_NATIVE
|
||||
#include "../std/windows.hpp"
|
||||
#else
|
||||
#include <sys/mman.h>
|
||||
#include <sys/errno.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/fcntl.h>
|
||||
#endif
|
||||
|
||||
class MemoryMappedFile
|
||||
{
|
||||
private:
|
||||
|
||||
bool m_isReadOnly;
|
||||
|
||||
#ifdef OMIM_OS_WINDOWS_NATIVE
|
||||
FILE * m_fp;
|
||||
#else
|
||||
int m_fd;
|
||||
void * m_data;
|
||||
size_t m_size;
|
||||
#endif
|
||||
|
||||
public:
|
||||
MemoryMappedFile(char const * fileName, bool isReadOnly);
|
||||
~MemoryMappedFile();
|
||||
void * data();
|
||||
size_t size() const;
|
||||
};
|
|
@ -7,24 +7,16 @@
|
|||
#include <../cache/ftccback.h>
|
||||
#include <../cache/ftccache.h>
|
||||
|
||||
|
||||
namespace yg
|
||||
{
|
||||
Font::Font(char const * name) : m_name(name)
|
||||
Font::Font(char const * name) : m_name(name), m_fontData(name, true)
|
||||
{
|
||||
FILE * fp = fopen(name, "rb");
|
||||
fseek(fp, 0, SEEK_END);
|
||||
int size = ftell(fp);
|
||||
fseek(fp, 0, SEEK_SET);
|
||||
|
||||
m_fontData.resize(size);
|
||||
fread(&m_fontData[0], 1, size, fp);
|
||||
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
FT_Error Font::CreateFaceID(FT_Library library, FT_Face *face)
|
||||
{
|
||||
return FT_New_Memory_Face(library, &m_fontData[0], m_fontData.size(), 0, face);
|
||||
return FT_New_Memory_Face(library, (unsigned char*)m_fontData.data(), m_fontData.size(), 0, face);
|
||||
}
|
||||
|
||||
GlyphCacheImpl::GlyphCacheImpl(size_t maxSize)
|
||||
|
|
|
@ -3,15 +3,17 @@
|
|||
#include "ft2_debug.hpp"
|
||||
#include "../std/string.hpp"
|
||||
#include "../std/vector.hpp"
|
||||
|
||||
|
||||
#include "../std/shared_ptr.hpp"
|
||||
#include "../base/memory_mapped_file.hpp"
|
||||
|
||||
namespace yg
|
||||
{
|
||||
struct Font
|
||||
{
|
||||
string m_name;
|
||||
vector<unsigned char> m_fontData;
|
||||
|
||||
MemoryMappedFile m_fontData;
|
||||
|
||||
/// information about symbol ranges
|
||||
/// ...
|
||||
/// constructor
|
||||
|
@ -32,7 +34,7 @@ namespace yg
|
|||
|
||||
FTC_CMapCache m_charMapCache;
|
||||
|
||||
typedef vector<Font> TFonts;
|
||||
typedef vector<shared_ptr<Font> > TFonts;
|
||||
TFonts m_fonts;
|
||||
|
||||
static FT_Error RequestFace(FTC_FaceID faceID, FT_Library library, FT_Pointer requestData, FT_Face * face);
|
||||
|
|
Loading…
Add table
Reference in a new issue