Joedb 10.0.1
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
File_Slice.h
Go to the documentation of this file.
1#ifndef joedb_File_Slice_declared
2#define joedb_File_Slice_declared
3
6
7#include <sys/mman.h>
8
9namespace joedb
10{
11 namespace detail
12 {
13 class Memory_Mapping
14 {
15 private:
16 void * const data;
17 const size_t size;
18
19 public:
20 Memory_Mapping(int fd, size_t size):
21 data(mmap(nullptr, size, PROT_READ, MAP_PRIVATE, fd, 0)),
22 size(size)
23 {
24 if (data == MAP_FAILED)
25 Posix_File::throw_last_error("mapping", "file");
26 }
27
28 Memory_Mapping(const Memory_Mapping &) = delete;
29 Memory_Mapping &operator=(const Memory_Mapping &) = delete;
30
31 const char *get() const {return (const char *)data;}
32
33 ~Memory_Mapping()
34 {
35 munmap(data, size);
36 }
37 };
38 }
39
40 /// @ingroup journal
41 class File_Slice: public detail::Memory_Mapping, public Readonly_Memory_File
42 {
43 public:
44 File_Slice(int fd, size_t offset, size_t size):
45 detail::Memory_Mapping(fd, offset + size),
46 Readonly_Memory_File(detail::Memory_Mapping::get() + offset, size)
47 {
48 }
49 };
50}
51
52#endif
File_Slice(int fd, size_t offset, size_t size)
Definition File_Slice.h:44
static void throw_last_error(const char *action, const char *file_name)
Definition Blob.h:7