11 #ifndef __MEMORYMAPPEDFILE_H__
12 #define __MEMORYMAPPEDFILE_H__
23 #include <sys/types.h>
33 template <
class T>
class CMemoryMappedFile :
public CSGObject
39 SG_UNSTABLE(
"CMemoryMappedFile::CMemoryMappedFile()",
67 REQUIRE(flag==
'w' || flag==
'r',
"Only 'r' and 'w' flags are allowed")
72 int open_flags=O_RDONLY;
73 int mmap_prot=PROT_READ;
74 int mmap_flags=MAP_PRIVATE;
78 open_flags=O_RDWR | O_CREAT;
79 mmap_prot=PROT_READ|PROT_WRITE;
80 mmap_flags=MAP_SHARED;
83 fd = open(fname, open_flags, S_IRWXU | S_IRWXG | S_IRWXO);
90 if (lseek(
fd, fsize, SEEK_SET) != fsize || write(
fd, &byte, 1) != 1)
91 SG_ERROR(
"Error creating file of size %ld bytes\n", fsize)
95 if (fstat(
fd, &sb) == -1)
96 SG_ERROR(
"Error determining file size\n")
165 for (uint64_t i=offs; i<
length; i++)
191 void write_line(
const char* line, uint64_t len, uint64_t& offs)
193 char* s = ((
char*)
address) + offs;
195 SG_ERROR(
"Writing beyond size of file\n")
197 for (uint64_t i=0; i<len; i++)
229 for (uint64_t i=0; i<
length; i++)
263 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)