| 1 | #include <sys/types.h> |
| 2 | #include <sys/stat.h> |
| 3 | #include <unistd.h> |
| 4 | #include <string> |
| 5 | #include <map> |
| 6 | #include "mmap_access_mode.h" |
| 7 | |
| 8 | namespace mmap_allocator_namespace { |
| 9 | class mmap_file_identifier { |
| 10 | public: |
| 11 | mmap_file_identifier(); |
| 12 | mmap_file_identifier(const mmap_file_identifier &other); |
| 13 | mmap_file_identifier(std::string fname, enum access_mode access_mode); |
| 14 | bool operator==(const mmap_file_identifier &other) const; |
| 15 | bool operator<(const mmap_file_identifier &other) const; |
| 16 | |
| 17 | private: |
| 18 | enum access_mode access_mode; |
| 19 | dev_t device; |
| 20 | ino_t inode; |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | class mmapped_file { |
| 25 | public: |
| 26 | mmapped_file(): |
| 27 | fd(-1), |
| 28 | memory_area(NULL), |
| 29 | size_mapped(0), |
| 30 | offset_mapped(0), |
| 31 | reference_count(0) |
| 32 | { } |
| 33 | |
| 34 | void *get_memory_area(void) |
| 35 | { |
| 36 | return memory_area; |
| 37 | } |
| 38 | |
| 39 | void* open_and_mmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap); |
| 40 | bool munmap_and_close_file(void); |
| 41 | |
| 42 | private: |
| 43 | friend class mmap_file_pool; |
| 44 | |
| 45 | int fd; |
| 46 | void *memory_area; |
| 47 | size_t size_mapped; |
| 48 | off_t offset_mapped; |
| 49 | int reference_count; |
| 50 | }; |
| 51 | |
| 52 | typedef std::map<mmap_file_identifier, mmapped_file> mmapped_file_map_t; |
| 53 | typedef std::pair<mmap_file_identifier, mmapped_file> mmapped_file_pair_t; |
| 54 | |
| 55 | /* Singleton */ |
| 56 | class mmap_file_pool { |
| 57 | public: |
| 58 | mmap_file_pool(): |
| 59 | the_map() |
| 60 | { } |
| 61 | |
| 62 | void *mmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap); |
| 63 | void munmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length); |
| 64 | |
| 65 | private: |
| 66 | mmapped_file_map_t the_map; |
| 67 | }; |
| 68 | |
| 69 | extern mmap_file_pool the_pool; |
| 70 | } |