File: mmap_file_pool.h

package info (click to toggle)
libmmap-allocator 0.4.0%2Bgit20200122.adbfbe1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 152 kB
  • sloc: cpp: 946; ansic: 61; makefile: 44; sh: 11
file content (70 lines) | stat: -rw-r--r-- 1,709 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string>
#include <map>
#include "mmap_access_mode.h"

namespace mmap_allocator_namespace {
	class mmap_file_identifier {
public:
		mmap_file_identifier();
		mmap_file_identifier(const mmap_file_identifier &other);
		mmap_file_identifier(std::string fname, enum access_mode access_mode);
		bool operator==(const mmap_file_identifier &other) const;
		bool operator<(const mmap_file_identifier &other) const;

private:
		enum access_mode access_mode;
		dev_t device;
		ino_t inode;	
	};


	class mmapped_file {
public:
		mmapped_file():
			fd(-1),
			memory_area(NULL),
			size_mapped(0),
			offset_mapped(0),
			reference_count(0)
		{ }

		void *get_memory_area(void)
		{
			return memory_area;
		}

		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);
		bool munmap_and_close_file(void);

private:
		friend class mmap_file_pool;

                int fd;
                void *memory_area;
                size_t size_mapped;
                off_t offset_mapped;
		int reference_count;
	};

	typedef std::map<mmap_file_identifier, mmapped_file> mmapped_file_map_t;
	typedef std::pair<mmap_file_identifier, mmapped_file> mmapped_file_pair_t;

/* Singleton */
	class mmap_file_pool {
public:
		mmap_file_pool():
			the_map()
		{ }

		void *mmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length, bool map_whole_file, bool allow_remap);
		void munmap_file(std::string fname, enum access_mode access_mode, off_t offset, size_t length);

private:
		mmapped_file_map_t the_map;
	};

	extern mmap_file_pool the_pool;
}