UCommon
Public Member Functions
ost::MappedFile Class Reference

Create and map a disk file into memory. More...

#include <file.h>

Inheritance diagram for ost::MappedFile:
Inheritance graph
[legend]
Collaboration diagram for ost::MappedFile:
Collaboration graph
[legend]

Public Member Functions

char * fetch (size_t offset=0)
 Fetch a pointer to an offset within the memory mapped portion of the disk file. More...
 
char * fetch (off_t pos, size_t len)
 Fetch and map a portion of a disk file to a logical memory block. More...
 
bool lock (void)
 Lock the currently mapped portion of a file. More...
 
 MappedFile (const char *fname, Access mode)
 Open a file for mapping. More...
 
 MappedFile (const char *fname, Access mode, size_t size)
 Create if not exists, and map a file of specified size into memory. More...
 
 MappedFile (const char *fname, pos_t offset, size_t size, Access mode)
 Map a portion or all of a specified file in the specified shared memory access mode. More...
 
size_t pageAligned (size_t size)
 Compute map size to aligned page boundry. More...
 
void release (char *address, size_t len)
 Release (unmap) a memory segment. More...
 
void sync (void)
 Synchronize the contents of the mapped portion of memory with the disk file and wait for completion. More...
 
void sync (char *address, size_t len)
 Synchronize a segment of memory mapped from a segment fetch. More...
 
void unlock (void)
 Unlock a locked mapped portion of a file.
 
void update (size_t offset=0, size_t len=0)
 Map a portion of the memory mapped from the file back to the file and do not wait for completion. More...
 
void update (char *address, size_t len)
 Update a mapped region back to disk as specified by address and length. More...
 
virtual ~MappedFile ()
 Release a mapped section of memory associated with a file. More...
 
- Public Member Functions inherited from ost::RandomFile
off_t getCapacity (void)
 Get current file capacity. More...
 
Error getErrorNumber (void) const
 Return current error id. More...
 
char * getErrorString (void) const
 Return current error string. More...
 
bool initial (void)
 This method should be called right after a RandomFile derived object has been created. More...
 
 operator bool () const
 
bool operator! (void) const
 
virtual Error restart (void)
 This method is commonly used to close and re-open an existing database. More...
 
virtual ~RandomFile ()
 Destroy a random access file or it's derived class.
 

Additional Inherited Members

- Protected Member Functions inherited from ost::RandomFile
Error error (Error errid, char *errstr=NULL)
 Post an error event. More...
 
Error error (char *err)
 Post an extended string error message. More...
 
void final (void)
 Close the file.
 
virtual Attr initialize (void)
 This method is used to initialize a newly created file as indicated by the "initial" flag. More...
 
 RandomFile (const char *name=NULL)
 Create an unopened random access file.
 
 RandomFile (const RandomFile &rf)
 Default copy constructor.
 
Error setCompletion (Complete mode)
 Used to set file completion modes. More...
 
void setError (bool enable)
 Used to enable or disable throwing of exceptions on errors. More...
 
void setTemporary (bool enable)
 Used to set the temporary attribute for the file. More...
 
- Protected Attributes inherited from ost::RandomFile
Access access
 
int fd
 
struct {
   unsigned   count: 16
 
   bool   immediate: 1
 
   bool   initial: 1
 
   bool   temp: 1
 
   bool   thrown: 1
 
flags
 
char * pathname
 

Detailed Description

Create and map a disk file into memory.

This portable class works under both Posix via mmap and under the win32 API. A mapped file can be referenced directly by it's memory segment. One can map and unmap portions of a file on demand, and update changed memory pages mapped from files immediately through sync().

Author
David Sugar dyfet.nosp@m.@ost.nosp@m.el.co.nosp@m.m Map a named disk file into memory.

Definition at line 663 of file file.h.

Constructor & Destructor Documentation

◆ MappedFile() [1/3]

ost::MappedFile::MappedFile ( const char *  fname,
Access  mode 
)

Open a file for mapping.

More than one segment of a file may be mapped into seperate regions of memory.

Parameters
fnamefile name to access for mapping.
modeaccess mode to map file.

◆ MappedFile() [2/3]

ost::MappedFile::MappedFile ( const char *  fname,
Access  mode,
size_t  size 
)

Create if not exists, and map a file of specified size into memory.

Parameters
fnamefile name to access for mapping.
modeaccess mode to map file.
sizeof file to map.

◆ MappedFile() [3/3]

ost::MappedFile::MappedFile ( const char *  fname,
pos_t  offset,
size_t  size,
Access  mode 
)

Map a portion or all of a specified file in the specified shared memory access mode.

Valid mapping modes include mappedRead, mappedWrite, and mappedReadWrite.

Parameters
fnamepathname of file to map into memory.
offsetfrom start of file to begin mapping in bytes.
sizeof mapped area in bytes.
modeto map file.

◆ ~MappedFile()

virtual ost::MappedFile::~MappedFile ( )
virtual

Release a mapped section of memory associated with a file.

The mapped area is updated back to disk.

Member Function Documentation

◆ fetch() [1/2]

char* ost::MappedFile::fetch ( size_t  offset = 0)
inline

Fetch a pointer to an offset within the memory mapped portion of the disk file.

This really is used for convience of matching operations between Update and Fetch, as one could simply have accessed the base pointer where the file was mapped directly.

Parameters
offsetfrom start of mapped memory.

Definition at line 764 of file file.h.

◆ fetch() [2/2]

char* ost::MappedFile::fetch ( off_t  pos,
size_t  len 
)

Fetch and map a portion of a disk file to a logical memory block.

Returns
pointer to memory segment.
Parameters
posoffset of file segment to map.
lensize of memory segment to map.

◆ lock()

bool ost::MappedFile::lock ( void  )

Lock the currently mapped portion of a file.

Returns
true if pages are locked.

◆ pageAligned()

size_t ost::MappedFile::pageAligned ( size_t  size)

Compute map size to aligned page boundry.

Parameters
sizerequest.
Returns
page aligned size.

◆ release()

void ost::MappedFile::release ( char *  address,
size_t  len 
)

Release (unmap) a memory segment.

Parameters
addressaddress of memory segment to release.
lenlength of memory segment to release.

◆ sync() [1/2]

void ost::MappedFile::sync ( void  )

Synchronize the contents of the mapped portion of memory with the disk file and wait for completion.

This assures the memory mapped from the file is written back.

◆ sync() [2/2]

void ost::MappedFile::sync ( char *  address,
size_t  len 
)

Synchronize a segment of memory mapped from a segment fetch.

Parameters
addressmemory address to update.
lensize of segment.

◆ update() [1/2]

void ost::MappedFile::update ( size_t  offset = 0,
size_t  len = 0 
)

Map a portion of the memory mapped from the file back to the file and do not wait for completion.

This is useful when mapping a database file and updating a single record.

Parameters
offsetoffset into the mapped region of memory.
lenlength of partial region (example, record length).

◆ update() [2/2]

void ost::MappedFile::update ( char *  address,
size_t  len 
)

Update a mapped region back to disk as specified by address and length.

Parameters
addressaddress of segment.
lenlength of segment.

The documentation for this class was generated from the following file: