| 1 | #include "mmap_file_pool.h" |
| 2 | #include "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_namespace { |
| 12 | |
| 13 | #ifndef MMAP_ALLOCATOR_DEBUG |
| 14 | #define MMAP_ALLOCATOR_DEBUG 0 |
| 15 | #endif |
| 16 | |
| 17 | int verbosity = MMAP_ALLOCATOR_DEBUG; |
| 18 | |
| 19 | void set_verbosity(int v) |
| 20 | { |
| 21 | verbosity = v; |
| 22 | } |
| 23 | |
| 24 | int get_verbosity(void) |
| 25 | { |
| 26 | return verbosity; |
| 27 | } |
| 28 | |
| 29 | off_t filesize(int fd, std::string fname) |
| 30 | { |
| 31 | struct stat buf; |
| 32 | if (fstat(fd, &buf) < 0) { |
| 33 | if (get_verbosity() > 0) { |
| 34 | perror("stat"); |
| 35 | } |
| 36 | |
| 37 | throw mmap_allocator_exception("Cannot stat file" + fname); |
| 38 | } |
| 39 | |
| 40 | return buf.st_size; |
| 41 | } |
| 42 | |
| 43 | mmap_file_identifier::mmap_file_identifier(std::string fname, enum access_mode access_mode_param) |
| 44 | { |
| 45 | struct stat buf; |
| 46 | if (stat(fname.c_str(), &buf) < 0) { |
| 47 | if (get_verbosity() > 0) { |
| 48 | perror("stat"); |
| 49 | } |
| 50 | throw mmap_allocator_exception("Cannot stat file "+fname); |
| 51 | } |
| 52 | |
| 53 | device = buf.st_dev; |
| 54 | inode = buf.st_ino; |
| 55 | access_mode = access_mode_param; |
| 56 | } |
| 57 | |
| 58 | mmap_file_identifier::mmap_file_identifier(const mmap_file_identifier &other) |
| 59 | { |
| 60 | device = other.device; |
| 61 | inode = other.inode; |
| 62 | access_mode = other.access_mode; |
| 63 | } |
| 64 | |
| 65 | bool mmap_file_identifier::operator==(const mmap_file_identifier &other) const |
| 66 | { |
| 67 | return device == other.device && |
| 68 | inode == other.inode && |
| 69 | access_mode == other.access_mode; |
| 70 | } |
| 71 | |
| 72 | bool mmap_file_identifier::operator<(const mmap_file_identifier &other) const |
| 73 | { |
| 74 | return inode < other.inode || |
| 75 | (inode == other.inode && access_mode < other.access_mode) || |
| 76 | (inode == other.inode && access_mode == other.access_mode && device < other.device); |
| 77 | |
| 78 | } |
| 79 | |
| 80 | void *mmapped_file::open_and_mmap_file(std::string filename, enum access_mode access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap) |
| 81 | { |
| 82 | if (filename.c_str()[0] == '\0') { |
| 83 | throw 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 = NULL; |
| 91 | |
| 92 | if (memory_area != NULL) { |
| 93 | address_to_map = 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_ONLY: mode = O_RDONLY; prot = PROT_READ; mmap_mode |= MAP_SHARED; break; |
| 102 | case READ_WRITE_SHARED: mode = O_RDWR; prot = PROT_READ | PROT_WRITE; mmap_mode |= MAP_SHARED; break; |
| 103 | case READ_WRITE_PRIVATE: mode = O_RDONLY; prot = PROT_READ | PROT_WRITE; mmap_mode |= MAP_PRIVATE; break; |
| 104 | default: throw mmap_allocator_exception("Internal error"); break; |
| 105 | } |
| 106 | |
| 107 | if (fd == -1) { |
| 108 | fd = open(filename.c_str(), mode); |
| 109 | if (fd < 0) { |
| 110 | if (get_verbosity() > 0) { |
| 111 | perror("open"); |
| 112 | } |
| 113 | |
| 114 | throw mmap_allocator_exception("Error opening file " + filename); |
| 115 | } |
| 116 | } |
| 117 | if (map_whole_file) { |
| 118 | offset_to_map = 0; |
| 119 | length_to_map = filesize(fd, filename); |
| 120 | } else { |
| 121 | offset_to_map = ALIGN_TO_PAGE(offset); |
| 122 | length_to_map = UPPER_ALIGN_TO_PAGE(length); |
| 123 | } |
| 124 | |
| 125 | if (offset_to_map == offset_mapped && length_to_map == size_mapped) { |
| 126 | reference_count++; |
| 127 | return ((char*)memory_area)+offset-offset_mapped; |
| 128 | } |
| 129 | if (offset_to_map >= offset_mapped && length_to_map + offset_to_map - offset_mapped <= size_mapped) |
| 130 | { |
| 131 | reference_count++; |
| 132 | return ((char*)memory_area)+offset-offset_mapped; |
| 133 | } |
| 134 | |
| 135 | if (memory_area != NULL) { |
| 136 | if (munmap(memory_area, size_mapped) < 0) { |
| 137 | if (get_verbosity() > 0) { |
| 138 | perror("munmap"); |
| 139 | } |
| 140 | throw mmap_allocator_exception("Error in munmap file "+filename); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | memory_area = mmap(address_to_map, length_to_map, prot, mmap_mode, fd, offset_to_map); |
| 145 | if (address_to_map != NULL && !allow_remap && memory_area != MAP_FAILED && memory_area != address_to_map) { |
| 146 | if (munmap(memory_area, length_to_map) < 0) { |
| 147 | if (get_verbosity() > 0) { |
| 148 | perror("munmap"); |
| 149 | } |
| 150 | throw mmap_allocator_exception("Error in munmap" + filename); |
| 151 | } |
| 152 | throw mmap_allocator_exception("Request to remap area but allow_remap is not given (remapping "+filename+")"); |
| 153 | } |
| 154 | |
| 155 | if (memory_area == MAP_FAILED) { |
| 156 | if (get_verbosity() > 0) { |
| 157 | perror("mmap"); |
| 158 | } |
| 159 | throw mmap_allocator_exception("Error in mmap "+filename); |
| 160 | } |
| 161 | offset_mapped = offset_to_map; |
| 162 | size_mapped = length_to_map; |
| 163 | reference_count++; |
| 164 | |
| 165 | void *ret = ((char*)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_file::munmap_and_close_file(void) |
| 172 | { |
| 173 | reference_count--; |
| 174 | if (reference_count > 0) { |
| 175 | return false; |
| 176 | } |
| 177 | if (munmap(memory_area, size_mapped) < 0) { |
| 178 | if (get_verbosity() > 0) { |
| 179 | perror("munmap"); |
| 180 | } |
| 181 | throw mmap_allocator_exception("Error in munmap"); |
| 182 | } |
| 183 | if (close(fd)) { |
| 184 | if (get_verbosity() > 0) { |
| 185 | perror("close"); |
| 186 | } |
| 187 | throw mmap_allocator_exception("Error in close"); |
| 188 | } |
| 189 | fd = -1; |
| 190 | return true; |
| 191 | } |
| 192 | |
| 193 | void *mmap_file_pool::mmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap) |
| 194 | { |
| 195 | mmap_file_identifier the_identifier(fname, access_mode); |
| 196 | mmapped_file_map_t::iterator it; |
| 197 | |
| 198 | it = the_map.find(the_identifier); |
| 199 | if (it != the_map.end()) { |
| 200 | return it->second.open_and_mmap_file(fname, access_mode, offset, length, map_whole_file, allow_remap); |
| 201 | } else { |
| 202 | mmapped_file the_file; |
| 203 | void *ret; |
| 204 | |
| 205 | ret = the_file.open_and_mmap_file(fname, access_mode, offset, length, map_whole_file, allow_remap); |
| 206 | the_map.insert(mmapped_file_pair_t(the_identifier, the_file)); |
| 207 | return ret; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | void mmap_file_pool::munmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length) |
| 212 | { |
| 213 | mmap_file_identifier the_identifier(fname, access_mode); |
| 214 | mmapped_file_map_t::iterator it; |
| 215 | |
| 216 | it = the_map.find(the_identifier); |
| 217 | if (it != the_map.end()) { |
| 218 | if (it->second.munmap_and_close_file()) { |
| 219 | the_map.erase(it); |
| 220 | } |
| 221 | } else { |
| 222 | throw mmap_allocator_exception("File "+fname+" not found in pool"); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | mmap_file_pool the_pool; /* TODO: move to app object */ |
| 227 | } |