memory mapped file
Implements a memory mapped file for super fast file access.
Definition at line 29 of file MemoryMappedFile.h.
Public Member Functions | |
CMemoryMappedFile (const char *fname, char flag='r', uint64_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[] (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< T >::CMemoryMappedFile | ( | const char * | fname, | |
char | flag = 'r' , |
|||
uint64_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 45 of file MemoryMappedFile.h.
virtual CMemoryMappedFile< T >::~CMemoryMappedFile | ( | ) | [virtual] |
destructor
Definition at line 92 of file MemoryMappedFile.h.
uint64_t CMemoryMappedFile< T >::get_length | ( | ) |
get the number of objects of type T cointained in the file
Definition at line 122 of file MemoryMappedFile.h.
char* CMemoryMappedFile< T >::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 147 of file MemoryMappedFile.h.
T* CMemoryMappedFile< 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 113 of file MemoryMappedFile.h.
virtual const char* CMemoryMappedFile< T >::get_name | ( | ) | const [virtual] |
int32_t CMemoryMappedFile< T >::get_num_lines | ( | ) |
count the number of lines in a file
Definition at line 209 of file MemoryMappedFile.h.
uint64_t CMemoryMappedFile< T >::get_size | ( | ) |
get the size of the file in bytes
Definition at line 131 of file MemoryMappedFile.h.
T CMemoryMappedFile< T >::operator[] | ( | int32_t | index | ) | const |
operator overload for file read only access
DOES NOT DO ANY BOUNDS CHECKING
index | index |
Definition at line 229 of file MemoryMappedFile.h.
void CMemoryMappedFile< T >::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 200 of file MemoryMappedFile.h.
void CMemoryMappedFile< T >::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 176 of file MemoryMappedFile.h.
void* CMemoryMappedFile< T >::address [protected] |
mapping address
Definition at line 243 of file MemoryMappedFile.h.
int CMemoryMappedFile< T >::fd [protected] |
file descriptor
Definition at line 239 of file MemoryMappedFile.h.
uint64_t CMemoryMappedFile< T >::last_written_byte [protected] |
Definition at line 248 of file MemoryMappedFile.h.
uint64_t CMemoryMappedFile< T >::length [protected] |
size of file
Definition at line 241 of file MemoryMappedFile.h.
char CMemoryMappedFile< T >::rw [protected] |
mode
Definition at line 245 of file MemoryMappedFile.h.