memory mapped file
Implements a memory mapped file for super fast file access.
Definition at line 31 of file MemoryMappedFile.h.
Public Member Functions | |
CMemoryMappedFile (void) | |
CMemoryMappedFile (const char *fname, char flag='r', int64_t fsize=0) | |
virtual | ~CMemoryMappedFile () |
T * | get_map () |
uint64_t | get_length () |
uint64_t | get_size () |
char * | get_line (uint64_t &len, uint64_t &offs) |
void | write_line (const char *line, uint64_t len, uint64_t &offs) |
void | set_truncate_size (uint64_t *sz=0) |
int32_t | get_num_lines () |
T | operator[] (uint64_t index) const |
T | operator[] (int32_t index) const |
virtual const char * | get_name () const |
Protected Attributes | |
int | fd |
uint64_t | length |
void * | address |
char | rw |
uint64_t | last_written_byte |
CMemoryMappedFile | ( | void | ) |
default constructor
Definition at line 35 of file MemoryMappedFile.h.
CMemoryMappedFile | ( | const char * | fname, | |
char | flag = 'r' , |
|||
int64_t | fsize = 0 | |||
) |
constructor
open a memory mapped file for read or read/write mode
fname | name of file, zero terminated string | |
flag | determines read or read write mode (can be 'r' or 'w') | |
fsize | overestimate of expected file size (in bytes) when opened in write mode; Underestimating the file size will result in an error to occur upon writing. In case the exact file size is known later on, it can be reduced via set_truncate_size() before closing the file. |
Definition at line 60 of file MemoryMappedFile.h.
virtual ~CMemoryMappedFile | ( | ) | [virtual] |
destructor
Definition at line 107 of file MemoryMappedFile.h.
uint64_t get_length | ( | ) |
get the number of objects of type T cointained in the file
Definition at line 137 of file MemoryMappedFile.h.
char* get_line | ( | uint64_t & | len, | |
uint64_t & | offs | |||
) |
get next line from file
The returned line may be modfied in case the file was opened read/write. It is otherwise read-only.
len | length of line (returned via reference) | |
offs | offset to be passed for reading next line, should be 0 initially (returned via reference) |
Definition at line 162 of file MemoryMappedFile.h.
T* get_map | ( | ) |
get the mapping address It can now be accessed via, e.g.
double* x = get_map() x[index]= foo; (for write mode) foo = x[index]; (for read and write mode)
Definition at line 128 of file MemoryMappedFile.h.
virtual const char* get_name | ( | void | ) | const [virtual] |
int32_t get_num_lines | ( | ) |
count the number of lines in a file
Definition at line 225 of file MemoryMappedFile.h.
uint64_t get_size | ( | ) |
get the size of the file in bytes
Definition at line 146 of file MemoryMappedFile.h.
T operator[] | ( | int32_t | index | ) | const |
operator overload for file read only access
DOES NOT DO ANY BOUNDS CHECKING
index | index |
Definition at line 257 of file MemoryMappedFile.h.
T operator[] | ( | uint64_t | index | ) | const |
operator overload for file read only access
DOES NOT DO ANY BOUNDS CHECKING
index | index |
Definition at line 245 of file MemoryMappedFile.h.
void set_truncate_size | ( | uint64_t * | sz = 0 |
) |
set file size
When the file is opened for read/write mode, it will be truncated upon destruction of the CMemoryMappedFile object. This is automagically determined when writing lines, but might have to be set manually for other data types, which is what this function is for.
sz | byte number at which to truncate the file, zero to disable file truncation. Has an effect only when file is opened with in read/write mode 'w' |
Definition at line 216 of file MemoryMappedFile.h.
void write_line | ( | const char * | line, | |
uint64_t | len, | |||
uint64_t & | offs | |||
) |
write line to file
line | string to be written (must not contain ' ' and not required to be zero terminated) | |
len | length of the string to be written | |
offs | offset to be passed for writing next line, should be 0 initially (returned via reference) |
Definition at line 191 of file MemoryMappedFile.h.
void* address [protected] |
mapping address
Definition at line 271 of file MemoryMappedFile.h.
int fd [protected] |
file descriptor
Definition at line 267 of file MemoryMappedFile.h.
uint64_t last_written_byte [protected] |
last_written_byte
Definition at line 276 of file MemoryMappedFile.h.
uint64_t length [protected] |
size of file
Definition at line 269 of file MemoryMappedFile.h.
char rw [protected] |
mode
Definition at line 273 of file MemoryMappedFile.h.