---
name: "ost_MappedFile"
section: "3"
platform: "linux"
package: "libcommoncpp2-doc"
version: "1.8.1-10"
release: "Debian 11 (Bullseye)"
platforms: ["linux"]
url: "https://new.carta.tech/ost_MappedFile"
---

# ost\_MappedFile(3)

Create and map a disk file into memory.

## NAME

ost::MappedFile - Create and map a disk file into memory.

## SYNOPSIS

`#include <file.h>`

Inherits **ost::RandomFile**.

### Public Member Functions

**MappedFile** (const char \*fname, **Access** mode)\
 Open a file for mapping.  **MappedFile** (const char \*fname, **Access** mode, size\_t size)\
 Create if not exists, and map a file of specified size into memory.  **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.  virtual **~MappedFile** ()\
 Release a mapped section of memory associated with a file.  void **sync** (void)\
 Synchronize the contents of the mapped portion of memory with the disk file and wait for completion.  void **sync** (caddr\_t address, size\_t len)\
 Synchronize a segment of memory mapped from a segment fetch.  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.  void **update** (caddr\_t address, size\_t len)\
 Update a mapped region back to disk as specified by address and length.  void **release** (caddr\_t address, size\_t len)\
 Release (unmap) a memory segment.  caddr\_t **fetch** (size\_t offset=0)\
 Fetch a pointer to an offset within the memory mapped portion of the disk file.  caddr\_t **fetch** (off\_t pos, size\_t len)\
 Fetch and map a portion of a disk file to a logical memory block.  bool **lock** (void)\
 Lock the currently mapped portion of a file.  void **unlock** (void)\
 Unlock a locked mapped portion of a file.  size\_t **pageAligned** (size\_t size)\
 Compute map size to aligned page boundry.

### Additional Inherited Members

## 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@ostel.com

Map a named disk file into memory.

## Constructor & Destructor Documentation

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

Open a file for mapping. More than one segment of a file may be mapped into separate regions of memory.

**Parameters**

*fname* file name to access for mapping. \
 *mode* access mode to map file.

### 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**

*fname* file name to access for mapping. \
 *mode* access mode to map file. \
 *size* of file to map.

### 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**

*fname* pathname of file to map into memory. \
 *offset* from start of file to begin mapping in bytes. \
 *size* of mapped area in bytes. \
 *mode* to map file.

### 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

### caddr\_t 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**

*pos* offset of file segment to map. \
 *len* size of memory segment to map.

### caddr\_t 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**

*offset* from start of mapped memory.

### bool ost::MappedFile::lock (void)

Lock the currently mapped portion of a file.

**Returns**

true if pages are locked.

### size\_t ost::MappedFile::pageAligned (size\_t size)

Compute map size to aligned page boundry.

**Parameters**

*size* request.

**Returns**

page aligned size.

### void ost::MappedFile::release (caddr\_t address, size\_t len)

Release (unmap) a memory segment.

**Parameters**

*address* address of memory segment to release. \
 *len* length of memory segment to release.

### void ost::MappedFile::sync (caddr\_t address, size\_t len)

Synchronize a segment of memory mapped from a segment fetch.

**Parameters**

*address* memory address to update. \
 *len* size of segment.

### 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.

### void ost::MappedFile::unlock (void)

Unlock a locked mapped portion of a file.

### void ost::MappedFile::update (caddr\_t address, size\_t len)

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

**Parameters**

*address* address of segment. \
 *len* length of segment.

### 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**

*offset* offset into the mapped region of memory. \
 *len* length of partial region (example, record length).

## Author

Generated automatically by Doxygen for GNU CommonC++ from the source code.
