librime: mapped_file.h Source File

| librime 1.2 Rime Input Method Engine, the core library

mapped_file.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-06-27 GONG Chen <chen.sst@gmail.com>
6//
7#ifndef RIME_MAPPED_FILE_H_
8#define RIME_MAPPED_FILE_H_
9
10#include <stdint.h>
11#include <algorithm>
12#include <cstring>
13#include <rime_api.h>
14#include <rime/common.h>
15
16namespace rime {
17
18// basic data structure
19
20// Limitation: cannot point to itself (zero is used to represent NULL pointer)
21template <class T = char, class Offset = int32_t>
22 class OffsetPtr {
23 public:
24 OffsetPtr() = default;
25 OffsetPtr(Offset offset) : offset_(offset) {}
26 OffsetPtr(const T* ptr) : OffsetPtr(to_offset(ptr)) {}
27 OffsetPtr(const OffsetPtr<T>& ptr) : OffsetPtr(ptr.get()) {}
28 OffsetPtr<T>& operator=(const OffsetPtr<T>& ptr) {
29 offset_ = to_offset(ptr.get());
30 return *this;
31 }
32 OffsetPtr<T>& operator=(const T* ptr) {
33 offset_ = to_offset(ptr);
34 return *this;
35 }
36 operator bool() const { return !!offset_; }
37 T* operator->() const { return get(); }
38 T& operator*() const { return *get(); }
39 T& operator[](size_t index) const { return *(get() + index); }
40 T* get() const {
41 if (!offset_)
42 return NULL;
43 return reinterpret_cast<T*>((char*)&offset_ + offset_);
44 }
45
46 private:
47 Offset to_offset(const T* ptr) const {
48 return ptr ? (char*)ptr - (char*)(&offset_) : 0;
49 }
50 Offset offset_ = 0;
51};
52
53 struct String {
54 OffsetPtr<char> data;
55 const char* c_str() const { return data.get(); }
56 size_t length() const { return c_str() ? strlen(c_str()) : 0; }
57 bool empty() const { return !data || !data[0]; }
58};
59
60template <class T, class Size = uint32_t>
61 struct Array {
62 Size size;
63 T at[1];
64 T* begin() { return &at[0]; }
65 T* end() { return &at[0] + size; }
66 const T* begin() const { return &at[0]; }
67 const T* end() const { return &at[0] + size; }
68};
69
70template <class T, class Size = uint32_t>
71 struct List {
72 Size size;
73 OffsetPtr<T> at;
74 T* begin() { return &at[0]; }
75 T* end() { return &at[0] + size; }
76 const T* begin() const { return &at[0]; }
77 const T* end() const { return &at[0] + size; }
78};
79
80// MappedFile class definition
81
82class MappedFileImpl;
83
84 class RIME_DLL MappedFile {
85 protected:
86 explicit MappedFile(const path& file_path);
87 virtual ~MappedFile();
88
89 bool Create(size_t capacity);
90 bool OpenReadOnly();
91 bool OpenReadWrite();
92 bool Flush();
93 bool Resize(size_t capacity);
94 bool ShrinkToFit();
95
96 template <class T>
97 T* Allocate(size_t count = 1);
98
99 template <class T>
100 Array<T>* CreateArray(size_t array_size);
101
102 String* CreateString(const string& str);
103 bool CopyString(const string& src, String* dest);
104
105 size_t capacity() const;
106 char* address() const;
107
108 public:
109 // noncpyable
110 MappedFile(const MappedFile&) = delete;
111 MappedFile& operator=(const MappedFile&) = delete;
112
113 bool Exists() const;
114 bool IsOpen() const;
115 void Close();
116 bool Remove();
117
118 template <class T>
119 T* Find(size_t offset);
120
121 const path& file_path() const { return file_path_; }
122 size_t file_size() const { return size_; }
123
124 private:
125 path file_path_;
126 size_t size_ = 0;
127 the<MappedFileImpl> file_;
128};
129
130// member function definitions
131
132#define RIME_ALIGNED(size, T) ((size + alignof(T) - 1) & ~(alignof(T) - 1))
133
134template <class T>
135T* MappedFile::Allocate(size_t count) {
136 if (!IsOpen())
137 return NULL;
138
139 size_t used_space = RIME_ALIGNED(size_, T);
140 size_t required_space = sizeof(T) * count;
141 size_t file_size = capacity();
142 if (used_space + required_space > file_size) {
143 // not enough space; grow the file
144 size_t new_size = (std::max)(used_space + required_space, file_size * 2);
145 if (!Resize(new_size) || !OpenReadWrite())
146 return NULL;
147 }
148 T* ptr = reinterpret_cast<T*>(address() + used_space);
149 std::memset((void*)ptr, 0, required_space);
150 size_ = used_space + required_space;
151 return ptr;
152}
153
154template <class T>
155T* MappedFile::Find(size_t offset) {
156 if (!IsOpen() || offset > size_)
157 return NULL;
158 return reinterpret_cast<T*>(address() + offset);
159}
160
161template <class T>
162 Array<T>* MappedFile::CreateArray(size_t array_size) {
163 size_t num_bytes = sizeof(Array<T>) + sizeof(T) * (array_size - 1);
164 Array<T>* ret = reinterpret_cast<Array<T>*>(Allocate<char>(num_bytes));
165 if (!ret)
166 return NULL;
167 ret->size = array_size;
168 return ret;
169}
170
171} // namespace rime
172
173#endif // RIME_MAPPED_FILE_H_
common.h
RIME_ALIGNED
#define RIME_ALIGNED(size, T)
Definition mapped_file.h:132
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::path
Definition common.h:84
rime::OffsetPtr
Definition mapped_file.h:22
rime::OffsetPtr::OffsetPtr
OffsetPtr(const OffsetPtr< T > &ptr)
Definition mapped_file.h:27
rime::OffsetPtr::operator=
OffsetPtr< T > & operator=(const OffsetPtr< T > &ptr)
Definition mapped_file.h:28
rime::OffsetPtr::operator->
T * operator->() const
Definition mapped_file.h:37
rime::OffsetPtr::get
T * get() const
Definition mapped_file.h:40
rime::OffsetPtr::OffsetPtr
OffsetPtr(const T *ptr)
Definition mapped_file.h:26
rime::OffsetPtr::operator[]
T & operator[](size_t index) const
Definition mapped_file.h:39
rime::OffsetPtr::operator=
OffsetPtr< T > & operator=(const T *ptr)
Definition mapped_file.h:32
rime::OffsetPtr::operator*
T & operator*() const
Definition mapped_file.h:38
rime::OffsetPtr::OffsetPtr
OffsetPtr(Offset offset)
Definition mapped_file.h:25
rime::OffsetPtr::OffsetPtr
OffsetPtr()=default
rime::String
Definition mapped_file.h:53
rime::String::c_str
const char * c_str() const
Definition mapped_file.h:55
rime::String::length
size_t length() const
Definition mapped_file.h:56
rime::String::empty
bool empty() const
Definition mapped_file.h:57
rime::String::data
OffsetPtr< char > data
Definition mapped_file.h:54
rime::Array
Definition mapped_file.h:61
rime::Array< SpellingMapItem >::size
uint32_t size
Definition mapped_file.h:62
rime::Array::begin
const T * begin() const
Definition mapped_file.h:66
rime::Array::end
const T * end() const
Definition mapped_file.h:67
rime::Array< SpellingMapItem >::at
SpellingMapItem at[1]
Definition mapped_file.h:63
rime::Array::end
T * end()
Definition mapped_file.h:65
rime::Array::begin
T * begin()
Definition mapped_file.h:64
rime::List
Definition mapped_file.h:71
rime::List< SpellingDescriptor >::at
OffsetPtr< SpellingDescriptor > at
Definition mapped_file.h:73
rime::List< SpellingDescriptor >::size
uint32_t size
Definition mapped_file.h:72
rime::List::begin
T * begin()
Definition mapped_file.h:74
rime::List::end
const T * end() const
Definition mapped_file.h:77
rime::List::begin
const T * begin() const
Definition mapped_file.h:76
rime::List::end
T * end()
Definition mapped_file.h:75
rime::MappedFile::Resize
bool Resize(size_t capacity)
Definition mapped_file.cc:128
rime::MappedFile::file_path
const path & file_path() const
Definition mapped_file.h:121
rime::MappedFile::capacity
size_t capacity() const
Definition mapped_file.cc:160
rime::MappedFile::Allocate
T * Allocate(size_t count=1)
Definition mapped_file.h:135
rime::MappedFile::Close
void Close()
Definition mapped_file.cc:96
rime::MappedFile::file_size
size_t file_size() const
Definition mapped_file.h:122
rime::MappedFile::CreateString
String * CreateString(const string &str)
Definition mapped_file.cc:140
rime::MappedFile::operator=
MappedFile & operator=(const MappedFile &)=delete
rime::MappedFile::CreateArray
Array< T > * CreateArray(size_t array_size)
Definition mapped_file.h:162
rime::MappedFile::address
char * address() const
Definition mapped_file.cc:164
rime::MappedFile::Remove
bool Remove()
Definition mapped_file.cc:122
rime::MappedFile::Exists
bool Exists() const
Definition mapped_file.cc:103
rime::MappedFile::ShrinkToFit
bool ShrinkToFit()
Definition mapped_file.cc:117
rime::MappedFile::CopyString
bool CopyString(const string &src, String *dest)
Definition mapped_file.cc:148
rime::MappedFile::OpenReadWrite
bool OpenReadWrite()
Definition mapped_file.cc:86
rime::MappedFile::OpenReadOnly
bool OpenReadOnly()
Definition mapped_file.cc:76
rime::MappedFile::Create
bool Create(size_t capacity)
Definition mapped_file.cc:54
rime::MappedFile::MappedFile
MappedFile(const path &file_path)
Definition mapped_file.cc:46
rime::MappedFile::IsOpen
bool IsOpen() const
Definition mapped_file.cc:107
rime::MappedFile::Flush
bool Flush()
Definition mapped_file.cc:111
rime::MappedFile::MappedFile
MappedFile(const MappedFile &)=delete
rime::MappedFile::Find
T * Find(size_t offset)
Definition mapped_file.h:155