---
name: "mmapbuf"
section: "3bobcat"
platform: "linux"
package: "libbobcat-dev"
version: "6.07.01-2"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mmapbuf"
---

# mmapbuf(3bobcat)

a std::streambuf using mmap(2) for file I/O

## NAME

FBB::MmapBuf - a std::streambuf using [mmap(2)](/mmap/2.md) for file I/O

## SYNOPSIS

**#include \<bobcat/mmapbuf\>**\

Linking option: *-lbobcat*

## DESCRIPTION

The [**mmap**(2)](/mmap/2.md) functions map files in the virtual address space of the calling process. Using *mmap* in **C++** is doable, but complex. The **MmapBuf** *std::streambuf* class embeds the features of [**mmap**(2)](/mmap/2.md), allowing the use of **C++**’s streams instead of using the raw *mmap* calls. the **FBB::Mmapbase** class defines a *std::streambuf* which can be used by *istream, ostream,* and *iostream* objects.

An *std::exception* is thrown, and an error message is written to *cerr* if the details about the specified filename (cf. section **CONSTRUCORS** cannot be determined, or when *mmap* or *munmap* return errors.

## NAMESPACE

**FBB**\
 All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace **FBB**.

## INHERITS FROM

**std::streambuf**

## CONSTRUCTORS

- o

  **MmapBuf()**:\
   The default constructor merely constructs an empty **MmapBuf** object. To change it to a usable object use move assignment;

-

  - o

  **MmapBuf(std::string const &fname,**  **std::ios::openmode openMode,** **char const \*bufSize = 0, mode\_t mode = 0644)**:\
   The constructor initializes the *MmapBuf* object for a file named *fname*. The *openmode* can be any combination of the *std::ios::openmode* values. The required values are usually provided when constructing *ImapStream, OmapStream,* or *IOmapStream* objects (cf. section **SEE ALSO** below).

-

  The *opnemMode* parameter specifies how *fname* is used. The standard *ios::in, ios::out, ios::trunc, ios::app,* and *ios::ate* modes are supported.

-

  By default bfMmapBuf uses a mapping buffer size of 10 times the standard page size, (cf. [**sysconf**(3)](/sysconf/3.md), and the member *pageSize* below). The size of the mapping buffer can also be specified using the *bufSize* parameter. To specify it use a value followed by *K, M,* or *G*, representing, resp. 1024, 1024 \* 1024, and 1024 \* 1024 \* 1024 bytes. The final buffer size is at least equal to the standard page size. When a larger value is specified the used buffer size is set to *specified / pageSize \* pageSize*.

The *mode* parameter specifies the user/group/other access mode which is used when the file is created by *MmapBuf*. Its default value specifies read/write access by the user, and  read access by others. )

The move constructor and move assignment operator are available.\
 The copy constructor and copy assignment operator are not available.

## MEMBER FUNCTIONS

All members of **std::stringstream** are available, as **FBB::MmapBuf** inherits from that class.

- o

  **size\_t bufSize() const**:\
   returns the used t(mmap) buffer size;

-

  - o

  **size\_t fileSize() const**:\
   returns the size of the used file. The size is updated to a larger size when writing beyond the current file size. Once **MmapBuf** objects cease to exist the used file’s size is modified to the current *fileSize* value. The size of a file that can only be read is never altered.

-

  - o

  **size\_t pageSize() const**:\
   returns the smallest page size used by t(mmap).

## EXAMPLE

An example is provided in bobcat’s source archive and gitlab repository at *bobcat/mmapbuf/demo*.

## FILES

*bobcat/mmapbuf* - defines the class interface

## SEE ALSO

[**bobcat**(7)](/bobcat/7.md), [**chmod**(2)](/chmod/2.md), [**immapstream**(3bobcat)](/immapstream/3bobcat.md), [**iommapstream**(3bobcat)](/iommapstream/3bobcat.md), [**ommapstream**(3bobcat)](/ommapstream/3bobcat.md), [**mmap**(2)](/mmap/2.md), [**sysconf**(3)](/sysconf/3.md)

## BUGS

None Reported.

## BOBCAT PROJECT FILES

- o

  *https://fbb-git.gitlab.io/bobcat/*: gitlab project page;

Debian Bobcat project files:

- o

  *libbobcat6*: debian package containing the shared library, changelog and copyright note;

- o

  *libbobcat-dev*: debian package containing the static library, headers, manual pages, and developer info;

## BOBCAT

Bobcat is an acronym of \`Brokken’s Own Base Classes And Templates’.

## COPYRIGHT

This is free software, distributed under the terms of the GNU General Public License (GPL).

## AUTHOR

Frank B. Brokken (**f.b.brokken@rug.nl**).
