]> git.saurik.com Git - cydia.git/blob - Cytore.hpp
Add File::mappings_ to Cytore for munmap() and msync().
[cydia.git] / Cytore.hpp
1 // Copyright Notice (GNU Affero GPL) {{{
2 /* Cyndir - (Awesome) Memory Mapped Dictionary
3 * Copyright (C) 2010 Jay Freeman (saurik)
4 */
5
6 /*
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20 // }}}
21
22 #ifndef CYTORE_HPP
23 #define CYTORE_HPP
24
25 #include <fcntl.h>
26
27 #include <sys/mman.h>
28 #include <sys/stat.h>
29
30 #include <cstdio>
31 #include <cstdlib>
32
33 #include <errno.h>
34 #include <stdint.h>
35 #include <unistd.h>
36
37 #define _assert(test) do \
38 if (!(test)) { \
39 fprintf(stderr, "_assert(%d:%s)@%s:%u[%s]\n", errno, #test, __FILE__, __LINE__, __FUNCTION__); \
40 exit(-1); \
41 } \
42 while (false)
43
44 namespace Cytore {
45
46 static const uint32_t Magic = 'cynd';
47
48 struct Header {
49 uint32_t magic_;
50 uint32_t version_;
51 uint32_t size_;
52 uint32_t reserved_;
53 };
54
55 struct Block {
56 };
57
58 template <typename Target_>
59 class Offset {
60 private:
61 uint32_t offset_;
62
63 public:
64 Offset() :
65 offset_(0)
66 {
67 }
68
69 Offset(uint32_t offset) :
70 offset_(offset)
71 {
72 }
73
74 Offset &operator =(uint32_t offset) {
75 offset_ = offset;
76 return *this;
77 }
78
79 uint32_t GetOffset() const {
80 return offset_;
81 }
82
83 bool IsNull() const {
84 return offset_ == 0;
85 }
86 };
87
88 template <typename Type_>
89 static _finline Type_ Round(Type_ value, Type_ size) {
90 Type_ mask(size - 1);
91 return value + mask & ~mask;
92 }
93
94 template <typename Base_>
95 class File {
96 private:
97 static const unsigned Shift_ = 17;
98 static const size_t Block_ = 1 << Shift_;
99 static const size_t Mask_ = Block_ - 1;
100
101 private:
102 int file_;
103
104 typedef std::vector<uint8_t *> BlockVector_;
105 BlockVector_ blocks_;
106
107 struct Mapping_ {
108 uint8_t *data_;
109 size_t size_;
110
111 Mapping_(uint8_t *data, size_t size) :
112 data_(data),
113 size_(size)
114 {
115 }
116 };
117
118 typedef std::vector<Mapping_> MappingVector_;
119 MappingVector_ maps_;
120
121 Header &Header_() {
122 return *reinterpret_cast<Header *>(blocks_[0]);
123 }
124
125 uint32_t &Size_() {
126 return Header_().size_;
127 }
128
129 void Map_(size_t size) {
130 size_t before(blocks_.size() * Block_);
131 size_t extend(size - before);
132
133 void *data(mmap(NULL, extend, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, file_, before));
134 uint8_t *bytes(reinterpret_cast<uint8_t *>(data));
135
136 maps_.push_back(Mapping_(bytes,extend));
137 for (size_t i(0); i != extend >> Shift_; ++i)
138 blocks_.push_back(bytes + Block_ * i);
139 }
140
141 void Truncate_(size_t capacity) {
142 capacity = Round(capacity, Block_);
143 ftruncate(file_, capacity);
144 Map_(capacity);
145 }
146
147 public:
148 File() :
149 file_(-1)
150 {
151 }
152
153 File(const char *path) :
154 file_(-1)
155 {
156 Open(path);
157 }
158
159 ~File() {
160 for (typename MappingVector_::const_iterator map(maps_.begin()); map != maps_.end(); ++map)
161 munmap(map->data_, map->size_);
162 close(file_);
163 }
164
165 void Sync() {
166 for (typename MappingVector_::const_iterator map(maps_.begin()); map != maps_.end(); ++map)
167 msync(map->data_, map->size_, MS_SYNC);
168 }
169
170 size_t Capacity() const {
171 return blocks_.size() * Block_;
172 }
173
174 void Open(const char *path) {
175 _assert(file_ == -1);
176 file_ = open(path, O_RDWR | O_CREAT | O_EXLOCK, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
177
178 struct stat stat;
179 fstat(file_, &stat);
180
181 size_t core(sizeof(Header) + sizeof(Base_));
182
183 size_t size(stat.st_size);
184 if (size == 0) {
185 Truncate_(core);
186 Header_().magic_ = Magic;
187 Size_() = core;
188 } else {
189 _assert(size >= core);
190 Truncate_(size);
191 _assert(Header_().magic_ == Magic);
192 _assert(Header_().version_ == 0);
193 }
194 }
195
196 void Reserve(size_t capacity) {
197 if (capacity <= Capacity())
198 return;
199 blocks_.pop_back();
200 Truncate_(capacity);
201 }
202
203 template <typename Target_>
204 Target_ &Get(uint32_t offset) {
205 return *reinterpret_cast<Target_ *>(blocks_[offset >> Shift_] + (offset & Mask_));
206 }
207
208 template <typename Target_>
209 Target_ &Get(Offset<Target_> &ref) {
210 return Get<Target_>(ref.GetOffset());
211 }
212
213 Base_ *operator ->() {
214 return &Get<Base_>(sizeof(Header));
215 }
216
217 template <typename Target_>
218 Offset<Target_> New(size_t extra = 0) {
219 size_t size(sizeof(Target_) + extra);
220 size = Round(size, sizeof(uintptr_t));
221 Reserve(Size_() + size);
222 uint32_t offset(Size_());
223 Size_() += size;
224 return Offset<Target_>(offset);
225 }
226 };
227
228 }
229
230 #endif//CYTORE_HPP