11 #ifndef __MEMORYMAPPEDFILE_H__ 12 #define __MEMORYMAPPEDFILE_H__ 14 #include <shogun/lib/config.h> 26 #include <sys/types.h> 35 template <
class T>
class CMemoryMappedFile :
public CSGObject
41 SG_UNSTABLE(
"CMemoryMappedFile::CMemoryMappedFile()",
69 REQUIRE(flag==
'w' || flag==
'r',
"Only 'r' and 'w' flags are allowed")
75 DWORD open_flags = GENERIC_READ;
76 DWORD share_mode = FILE_SHARE_READ;
77 DWORD create_disp = OPEN_EXISTING;
78 DWORD mmap_prot = PAGE_READONLY;
79 DWORD mmap_flags = FILE_MAP_READ;
82 open_flags |= GENERIC_WRITE;
83 share_mode |= FILE_SHARE_WRITE;
84 create_disp = OPEN_ALWAYS;
85 mmap_prot = PAGE_READWRITE;
86 mmap_flags = FILE_MAP_ALL_ACCESS;
89 fd = CreateFile(fname, open_flags, share_mode, 0, create_disp, FILE_ATTRIBUTE_NORMAL, NULL);
92 LARGE_INTEGER desired_len;
93 desired_len.QuadPart = fsize;
96 if ((SetFilePointerEx(
fd, desired_len, NULL, FILE_BEGIN) == 0) || (WriteFile(
fd, &byte, 1, &bytes_written, NULL) == 0))
97 SG_ERROR(
"Error creating file of size %ld bytes\n", fsize)
100 DWORD
length = GetFileSize(
fd, NULL);
101 if (length == INVALID_FILE_SIZE)
102 SG_ERROR(
"Error determining file size\n")
104 mapping = CreateFileMapping(
fd, 0, mmap_prot, 0, 0, 0);
106 address = MapViewOfFile(mapping, mmap_flags, 0, 0, length);
110 int open_flags=O_RDONLY;
111 int mmap_prot=PROT_READ;
112 int mmap_flags=MAP_PRIVATE;
116 open_flags=O_RDWR | O_CREAT;
117 mmap_prot=PROT_READ|PROT_WRITE;
118 mmap_flags=MAP_SHARED;
121 fd = open(fname, open_flags, S_IRWXU | S_IRWXG | S_IRWXO);
125 if (
rw==
'w' && fsize)
128 if (lseek(
fd, fsize, SEEK_SET) != fsize || write(
fd, &byte, 1) != 1)
129 SG_ERROR(
"Error creating file of size %ld bytes\n", fsize)
133 if (fstat(
fd, &sb) == -1)
134 SG_ERROR(
"Error determining file size\n")
137 address = mmap(NULL, length, mmap_prot, mmap_flags,
fd, 0);
149 CloseHandle(mapping);
152 LARGE_INTEGER desired_len;
154 if ((SetFilePointerEx(
fd, desired_len, NULL, FILE_BEGIN) == 0) || (SetEndOfFile(
fd) == 0)) {
218 for (uint64_t i=offs; i<
length; i++)
244 void write_line(
const char* line, uint64_t len, uint64_t& offs)
246 char* s = ((
char*)
address) + offs;
248 SG_ERROR(
"Writing beyond size of file\n")
250 for (uint64_t i=0; i<len; i++)
282 for (uint64_t i=0; i<
length; i++)
316 virtual const char*
get_name()
const {
return "MemoryMappedFile"; }
virtual ~CMemoryMappedFile()
void write_line(const char *line, uint64_t len, uint64_t &offs)
T operator[](uint64_t index) const
char * get_line(uint64_t &len, uint64_t &offs)
void set_truncate_size(uint64_t sz=0)
Class SGObject is the base class of all shogun objects.
CMemoryMappedFile(const char *fname, char flag='r', int64_t fsize=0)
all of classes and functions are contained in the shogun namespace
uint64_t last_written_byte
virtual const char * get_name() const
#define SG_UNSTABLE(func,...)
T operator[](int32_t index) const