Debian Codegraph @

mmap_file_pool.cpp

raw symbols SCIP
1 #cxx . . $ `<file>/mmap_file_pool.cpp`/include "mmap_file_pool.h"cxx . . $ `<file>/mmap_file_pool.h`/
2 #include "mmap_exception.h"cxx . . $ `<file>/mmap_exception.h`/
3 #include <stdio.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <sys/mman.h>
8 #include <unistd.h>
9 #include <assert.h>
10
11 namespace mmap_allocator_namespacecxx . . $ mmap_allocator_namespace/ {
12
13 #ifndef MMAP_ALLOCATOR_DEBUG
14 #define MMAP_ALLOCATOR_DEBUGcxx . . $ `mmap_file_pool.cpp:14:9`! 0
15 #endif
16
17 int verbositycxx . . $ mmap_allocator_namespace/verbosity. = MMAP_ALLOCATOR_DEBUGcxx . . $ `mmap_file_pool.cpp:14:9`!;
18
19 void set_verbositycxx . . $ mmap_allocator_namespace/set_verbosity(d4f767463ce0a6b3).(int v)
20 {
21 verbositycxx . . $ mmap_allocator_namespace/verbosity. = v;
22 }
23
24 int get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).(void)
25 {
26 return verbositycxx . . $ mmap_allocator_namespace/verbosity.;
27 }
28
29 off_t filesizecxx . . $ mmap_allocator_namespace/filesize(f62e798c4ad241f1).(int fd, stdcxx . . $ std/::stringcxx . . $ std/string# fname)
30 {
31 struct stat buf;
32 if (fstat(fd, &buf) < 0) {
33 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
34 perror("stat");
35 }
36
37 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Cannot stat file" +cxx . . $ std/operator+(42c652dfebe55ad7). fname);
38 }
39
40 return buf.st_size;
41 }
42
43 mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#::mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#mmap_file_identifier(65544bbd9e999b1a).(stdcxx . . $ std/::stringcxx . . $ std/string# fname, enum access_modecxx . . $ mmap_allocator_namespace/access_mode# access_mode_param)
44 {
45 struct stat buf;
46 if (stat(fname.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), &buf) < 0) {
47 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
48 perror("stat");
49 }
50 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Cannot stat file "+cxx . . $ std/operator+(42c652dfebe55ad7).fname);
51 }
52
53 devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device. = buf.st_dev;
54 inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. = buf.st_ino;
55 access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. = access_mode_param;
56 }
57
58 mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#::mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#mmap_file_identifier(50c231092f0944b3).(const mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier# &other)
59 {
60 devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device. = other.devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device.;
61 inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. = other.inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode.;
62 access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. = other.access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode.;
63 }
64
65 bool mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#::operatorcxx . . $ mmap_allocator_namespace/mmap_file_identifier#`operator==`(1416fdf5df534f58).==(const mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier# &other) const
66 {
67 return devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device. == other.devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device. &&
68 inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. == other.inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. &&
69 access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. == other.access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode.;
70 }
71
72 bool mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#::operatorcxx . . $ mmap_allocator_namespace/mmap_file_identifier#`operator<`(1416fdf5df534f58).<(const mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier# &other) const
73 {
74 return inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. < other.inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. ||
75 (inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. == other.inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. && access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. < other.access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode.) ||
76 (inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. == other.inodecxx . . $ mmap_allocator_namespace/mmap_file_identifier#inode. && access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. == other.access_modecxx . . $ mmap_allocator_namespace/mmap_file_identifier#access_mode. && devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device. < other.devicecxx . . $ mmap_allocator_namespace/mmap_file_identifier#device.);
77
78 }
79
80 void *mmapped_filecxx . . $ mmap_allocator_namespace/mmapped_file#::open_and_mmap_filecxx . . $ mmap_allocator_namespace/mmapped_file#open_and_mmap_file(defbca87225225a7).(stdcxx . . $ std/::stringcxx . . $ std/string# filename, enum access_modecxx . . $ mmap_allocator_namespace/access_mode# access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap)
81 {
82 if (filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).()[0] == '\0') {
83 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("mmap_allocator not correctly initialized: filename is empty.");
84 }
85 int mode;
86 int prot;
87 int mmap_mode = 0;
88 off_t offset_to_map;
89 size_t length_to_map;
90 void *address_to_map = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!;
91
92 if (memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
93 address_to_map = memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area.;
94
95 /* do not use MAP_FIXED, since that may invalidate other memory
96 areas in the process, such as shared libraries, which would
97 lead to a mystic Segfault. */
98
99 }
100 switch (access_mode) {
101 case READ_ONLYcxx . . $ mmap_allocator_namespace/READ_ONLY.: mode = O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!; prot = PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`!; mmap_mode |= MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!; break;
102 case READ_WRITE_SHAREDcxx . . $ mmap_allocator_namespace/READ_WRITE_SHARED.: mode = O_RDWRcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:45:9`!; prot = PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!; mmap_mode |= MAP_SHAREDcxx . . $ `<external>/17226767460805440860/mman-linux.h:42:9`!; break;
103 case READ_WRITE_PRIVATEcxx . . $ mmap_allocator_namespace/READ_WRITE_PRIVATE.: mode = O_RDONLYcxx . . $ `<external>/14578502075116107718/fcntl-linux.h:43:9`!; prot = PROT_READcxx . . $ `<external>/17226767460805440860/mman-linux.h:32:9`! | PROT_WRITEcxx . . $ `<external>/17226767460805440860/mman-linux.h:33:9`!; mmap_mode |= MAP_PRIVATEcxx . . $ `<external>/17226767460805440860/mman-linux.h:43:9`!; break;
104 default: throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Internal error"); break;
105 }
106
107 if (fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd. == -1) {
108 fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd. = open(filename.c_strcxx . . $ std/__cxx11/basic_string#c_str(8e614c7de0b1573e).(), mode);
109 if (fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd. < 0) {
110 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
111 perror("open");
112 }
113
114 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error opening file " +cxx . . $ std/operator+(42c652dfebe55ad7). filename);
115 }
116 }
117 if (map_whole_file) {
118 offset_to_map = 0;
119 length_to_map = filesizecxx . . $ mmap_allocator_namespace/filesize(f62e798c4ad241f1).(fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd., filenamecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5).);
120 } else {
121 offset_to_map = ALIGN_TO_PAGEcxx . . $ `mmap_access_mode.h:6:9`!(offset);
122 length_to_map = UPPER_ALIGN_TO_PAGEcxx . . $ `mmap_access_mode.h:7:9`!(length);
123 }
124
125 if (offset_to_map == offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped. && length_to_map == size_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#size_mapped.) {
126 reference_countcxx . . $ mmap_allocator_namespace/mmapped_file#reference_count.++;
127 return ((char*)memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area.)+offset-offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped.;
128 }
129 if (offset_to_map >= offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped. && length_to_map + offset_to_map - offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped. <= size_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#size_mapped.)
130 {
131 reference_countcxx . . $ mmap_allocator_namespace/mmapped_file#reference_count.++;
132 return ((char*)memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area.)+offset-offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped.;
133 }
134
135 if (memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`!) {
136 if (munmap(memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area., size_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#size_mapped.) < 0) {
137 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
138 perror("munmap");
139 }
140 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error in munmap file "+cxx . . $ std/operator+(42c652dfebe55ad7).filename);
141 }
142 }
143
144 memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. = mmap(address_to_map, length_to_map, prot, mmap_mode, fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd., offset_to_map);
145 if (address_to_map != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:416:9`! && !allow_remap && memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. != MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`! && memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. != address_to_map) {
146 if (munmap(memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area., length_to_map) < 0) {
147 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
148 perror("munmap");
149 }
150 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error in munmap" +cxx . . $ std/operator+(42c652dfebe55ad7). filename);
151 }
152 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Request to remap area but allow_remap is not given (remapping "+cxx . . $ std/operator+(42c652dfebe55ad7).filename+cxx . . $ std/operator+(900ec9581c410ad9).")");
153 }
154
155 if (memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area. == MAP_FAILEDcxx . . $ `<external>/6724834311684550606/mman.h:44:9`!) {
156 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
157 perror("mmap");
158 }
159 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error in mmap "+cxx . . $ std/operator+(42c652dfebe55ad7).filename);
160 }
161 offset_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#offset_mapped. = offset_to_map;
162 size_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#size_mapped. = length_to_map;
163 reference_countcxx . . $ mmap_allocator_namespace/mmapped_file#reference_count.++;
164
165 void *ret = ((char*)memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area.)+offset-offset_to_map;
166 // assert(ret >= memory_area && ret < (char*)memory_area+size_mapped);
167
168 return ret;
169 }
170
171 bool mmapped_filecxx . . $ mmap_allocator_namespace/mmapped_file#::munmap_and_close_filecxx . . $ mmap_allocator_namespace/mmapped_file#munmap_and_close_file(c9857a73d68e59ba).(void)
172 {
173 reference_countcxx . . $ mmap_allocator_namespace/mmapped_file#reference_count.--;
174 if (reference_countcxx . . $ mmap_allocator_namespace/mmapped_file#reference_count. > 0) {
175 return false;
176 }
177 if (munmap(memory_areacxx . . $ mmap_allocator_namespace/mmapped_file#memory_area., size_mappedcxx . . $ mmap_allocator_namespace/mmapped_file#size_mapped.) < 0) {
178 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
179 perror("munmap");
180 }
181 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error in munmap");
182 }
183 if (close(fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd.)) {
184 if (get_verbositycxx . . $ mmap_allocator_namespace/get_verbosity(b126dc7c1de90089).() > 0) {
185 perror("close");
186 }
187 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("Error in close");
188 }
189 fdcxx . . $ mmap_allocator_namespace/mmapped_file#fd. = -1;
190 return true;
191 }
192
193 void *mmap_file_poolcxx . . $ mmap_allocator_namespace/mmap_file_pool#::mmap_filecxx . . $ mmap_allocator_namespace/mmap_file_pool#mmap_file(defbca87225225a7).(stdcxx . . $ std/::stringcxx . . $ std/string# fname, enum access_modecxx . . $ mmap_allocator_namespace/access_mode# access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap)
194 {
195 mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier# the_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#mmap_file_identifier(65544bbd9e999b1a).(fnamecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5)., access_mode);
196 mmapped_file_map_tcxx . . $ mmap_allocator_namespace/mmapped_file_map_t#::iteratorcxx . . $ std/map#iterator# itcxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(ced63f7c635d850d).;
197
198 it =cxx . . $ std/_Rb_tree_iterator#`operator=`(214f3216cb6a16af). the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..findcxx . . $ std/map#find(36f8d56706583706).(the_identifier);
199 if (it !=cxx . . $ std/`operator!=`(be6a71a859368080). the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..endcxx . . $ std/map#end(7e4de33083b6e738).()) {
200 return it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second..open_and_mmap_filecxx . . $ mmap_allocator_namespace/mmapped_file#open_and_mmap_file(defbca87225225a7).(fnamecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5)., access_mode, offset, length, map_whole_file, allow_remap);
201 } else {
202 mmapped_filecxx . . $ mmap_allocator_namespace/mmapped_file# the_filecxx . . $ mmap_allocator_namespace/mmapped_file#mmapped_file(49f6e7a06ebc5aa8).;
203 void *ret;
204
205 ret = the_file.open_and_mmap_filecxx . . $ mmap_allocator_namespace/mmapped_file#open_and_mmap_file(defbca87225225a7).(fnamecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5)., access_mode, offset, length, map_whole_file, allow_remap);
206 the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..insertcxx . . $ std/map#insert(92111883a3599ede).(mmapped_file_pair_tcxx . . $ std/pair#pair(14d64bafa7379ebc).(the_identifier, the_file));
207 return ret;
208 }
209 }
210
211 void mmap_file_poolcxx . . $ mmap_allocator_namespace/mmap_file_pool#::munmap_filecxx . . $ mmap_allocator_namespace/mmap_file_pool#munmap_file(50701b6de2e55329).(stdcxx . . $ std/::stringcxx . . $ std/string# fname, enum access_modecxx . . $ mmap_allocator_namespace/access_mode# access_mode, off_t offset, size_t length)
212 {
213 mmap_file_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier# the_identifiercxx . . $ mmap_allocator_namespace/mmap_file_identifier#mmap_file_identifier(65544bbd9e999b1a).(fnamecxx . . $ std/__cxx11/basic_string#basic_string(a47f53d4115f6ce5)., access_mode);
214 mmapped_file_map_tcxx . . $ mmap_allocator_namespace/mmapped_file_map_t#::iteratorcxx . . $ std/map#iterator# itcxx . . $ std/_Rb_tree_iterator#_Rb_tree_iterator(ced63f7c635d850d).;
215
216 it =cxx . . $ std/_Rb_tree_iterator#`operator=`(214f3216cb6a16af). the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..findcxx . . $ std/map#find(36f8d56706583706).(the_identifier);
217 if (it !=cxx . . $ std/`operator!=`(be6a71a859368080). the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..endcxx . . $ std/map#end(7e4de33083b6e738).()) {
218 if (it->cxx . . $ std/_Rb_tree_iterator#`operator->`(34d6acfd341ba7ab).secondcxx . . $ std/pair#second..munmap_and_close_filecxx . . $ mmap_allocator_namespace/mmapped_file#munmap_and_close_file(c9857a73d68e59ba).()) {
219 the_mapcxx . . $ mmap_allocator_namespace/mmap_file_pool#the_map..erasecxx . . $ std/map#erase(9238c2f1608b9ba6).(it);
220 }
221 } else {
222 throw mmap_allocator_exceptioncxx . . $ mmap_allocator_namespace/mmap_allocator_exception#("File "+cxx . . $ std/operator+(42c652dfebe55ad7).fname+cxx . . $ std/operator+(900ec9581c410ad9)." not found in pool");
223 }
224 }
225
226 mmap_file_poolcxx . . $ mmap_allocator_namespace/mmap_file_pool# the_poolcxx . . $ mmap_allocator_namespace/the_pool.; /* TODO: move to app object */
227 }