11 #ifndef __MEMORYMAPPEDFILE_H__
12 #define __MEMORYMAPPEDFILE_H__
21 #include <sys/types.h>
31 template <
class T>
class CMemoryMappedFile :
public CSGObject
37 SG_UNSTABLE(
"CMemoryMappedFile::CMemoryMappedFile()",
65 REQUIRE(flag==
'w' || flag==
'r',
"Only 'r' and 'w' flags are allowed")
70 int open_flags=O_RDONLY;
71 int mmap_prot=PROT_READ;
72 int mmap_flags=MAP_PRIVATE;
76 open_flags=O_RDWR | O_CREAT;
77 mmap_prot=PROT_READ|PROT_WRITE;
78 mmap_flags=MAP_SHARED;
81 fd = open(fname, open_flags, S_IRWXU | S_IRWXG | S_IRWXO);
88 if (lseek(
fd, fsize, SEEK_SET) != fsize || write(
fd, &byte, 1) != 1)
89 SG_ERROR(
"Error creating file of size %ld bytes\n", fsize)
93 if (fstat(
fd, &sb) == -1)
94 SG_ERROR(
"Error determining file size\n")
163 for (uint64_t i=offs; i<
length; i++)
189 void write_line(
const char* line, uint64_t len, uint64_t& offs)
191 char* s = ((
char*)
address) + offs;
193 SG_ERROR(
"Writing beyond size of file\n")
195 for (uint64_t i=0; i<len; i++)
227 for (uint64_t i=0; i<
length; i++)
261 virtual const char*
get_name()
const {
return "MemoryMappedFile"; }
T operator[](uint64_t index) const
virtual const char * get_name() const
char * get_line(uint64_t &len, uint64_t &offs)
Class SGObject is the base class of all shogun objects.
void write_line(const char *line, uint64_t len, uint64_t &offs)
uint64_t last_written_byte
virtual ~CMemoryMappedFile()
T operator[](int32_t index) const
all of classes and functions are contained in the shogun namespace
#define SG_UNSTABLE(func,...)
CMemoryMappedFile(const char *fname, char flag='r', int64_t fsize=0)
void set_truncate_size(uint64_t sz=0)