From e750083a537bd4a948ac3c1a46f951b74c1929b7 Mon Sep 17 00:00:00 2001 From: rachytski Date: Sun, 19 Dec 2010 22:22:54 +0200 Subject: [PATCH] introduced memory_mapped_file class. --- base/base.pro | 6 +++-- base/memory_mapped_file.cpp | 47 +++++++++++++++++++++++++++++++++++++ base/memory_mapped_file.hpp | 33 ++++++++++++++++++++++++++ yg/glyph_cache_impl.cpp | 14 +++-------- yg/glyph_cache_impl.hpp | 10 ++++---- 5 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 base/memory_mapped_file.cpp create mode 100644 base/memory_mapped_file.hpp diff --git a/base/base.pro b/base/base.pro index 7720f58b57..9b26ce8a41 100644 --- a/base/base.pro +++ b/base/base.pro @@ -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 diff --git a/base/memory_mapped_file.cpp b/base/memory_mapped_file.cpp new file mode 100644 index 0000000000..f62f83b1a9 --- /dev/null +++ b/base/memory_mapped_file.cpp @@ -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; +} diff --git a/base/memory_mapped_file.hpp b/base/memory_mapped_file.hpp new file mode 100644 index 0000000000..49f4cf2828 --- /dev/null +++ b/base/memory_mapped_file.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include "../std/target_os.hpp" + +#ifdef OMIM_OS_WINDOWS_NATIVE + #include "../std/windows.hpp" +#else + #include + #include + #include + #include +#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; +}; diff --git a/yg/glyph_cache_impl.cpp b/yg/glyph_cache_impl.cpp index 20c9e7519a..6b02ea5051 100644 --- a/yg/glyph_cache_impl.cpp +++ b/yg/glyph_cache_impl.cpp @@ -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) diff --git a/yg/glyph_cache_impl.hpp b/yg/glyph_cache_impl.hpp index 3f056b2651..6d66acc832 100644 --- a/yg/glyph_cache_impl.hpp +++ b/yg/glyph_cache_impl.hpp @@ -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 m_fontData; + + MemoryMappedFile m_fontData; + /// information about symbol ranges /// ... /// constructor @@ -32,7 +34,7 @@ namespace yg FTC_CMapCache m_charMapCache; - typedef vector TFonts; + typedef vector > TFonts; TFonts m_fonts; static FT_Error RequestFace(FTC_FaceID faceID, FT_Library library, FT_Pointer requestData, FT_Face * face);