Lunchbox  1.14.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
lunchbox::MemoryMap Class Reference

Helper to map a file to a memory address (mmap). More...

#include <memoryMap.h>

+ Inheritance diagram for lunchbox::MemoryMap:
+ Collaboration diagram for lunchbox::MemoryMap:

Public Member Functions

 MemoryMap ()
 Construct a new memory map. More...
 
 MemoryMap (const std::string &filename)
 Construct and initialize a new, readonly memory map. More...
 
 MemoryMap (const std::string &filename, const size_t size)
 Construct and initialize a new, read-write memory map. More...
 
 ~MemoryMap ()
 Destruct the memory map. More...
 
const void * map (const std::string &filename)
 Map a file to a memory address. More...
 
const void * remap (const std::string &filename)
 Remap a different file for this memory map. More...
 
void * create (const std::string &filename, const size_t size)
 Create a writable file to a memory address. More...
 
void * recreate (const std::string &filename, const size_t size)
 Recreate a different writable file for this memory map. More...
 
void unmap ()
 Unmap the file. More...
 
const void * getAddress () const
 
void * getAddress ()
 
template<class T >
const T * getAddress () const
 
template<class T >
T * getAddress ()
 
size_t getSize () const
 

Detailed Description

Helper to map a file to a memory address (mmap).

Deprecated:
Use boost::iostreams::mapped_file_source

Example:

/* Copyright (c) 2013-2016 Stefan.Eilemann@epfl.ch
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define BOOST_TEST_MODULE MemoryMap
#include <lunchbox/memoryMap.h>
#include <lunchbox/types.h>
#include <boost/test/unit_test.hpp>
#define MAP_SIZE LB_10MB
#define STRIDE 23721
BOOST_AUTO_TEST_CASE( write_read )
{
lunchbox::MemoryMap map( "foo.mmap", MAP_SIZE );
BOOST_CHECK_EQUAL( map.getSize(), MAP_SIZE );
BOOST_CHECK( map.recreate( "foo.mmap", MAP_SIZE ));
uint8_t* writePtr = map.getAddress< uint8_t >();
BOOST_CHECK( writePtr );
for( size_t i=0; i < MAP_SIZE; i += STRIDE )
writePtr[i] = uint8_t( i );
map.unmap();
const void* noPtr = map.map( "foo.map" );
BOOST_CHECK( !noPtr );
BOOST_CHECK_EQUAL( map.getSize(), 0 );
BOOST_CHECK( map.map( "foo.mmap" ));
BOOST_CHECK( !map.map( "foo.mmap" ));
BOOST_CHECK( map.remap( "foo.mmap" ));
const uint8_t* readPtr = map.getAddress< uint8_t >();
BOOST_CHECK( readPtr );
BOOST_CHECK_EQUAL( map.getSize(), MAP_SIZE );
for( size_t i=0; i < MAP_SIZE; i += STRIDE )
BOOST_CHECK_EQUAL( readPtr[i], uint8_t( i ));
}
BOOST_AUTO_TEST_CASE( exceptions )
{
BOOST_CHECK_THROW( lunchbox::MemoryMap( "doesnotexist" ),
std::runtime_error );
BOOST_CHECK_THROW( lunchbox::MemoryMap( "/doesnotexist", 42 ),
std::runtime_error );
}

Definition at line 36 of file memoryMap.h.

Constructor & Destructor Documentation

lunchbox::MemoryMap::MemoryMap ( )

Construct a new memory map.

Version
1.0
lunchbox::MemoryMap::MemoryMap ( const std::string &  filename)
explicit

Construct and initialize a new, readonly memory map.

Exceptions
std::runtime_errorif file can't be mapped.
Version
1.7.1
lunchbox::MemoryMap::MemoryMap ( const std::string &  filename,
const size_t  size 
)

Construct and initialize a new, read-write memory map.

Exceptions
std::runtime_errorif file can't be created.
Version
1.9.1
lunchbox::MemoryMap::~MemoryMap ( )

Destruct the memory map.

Unmaps the file, if it is still mapped.

See also
unmap()
Version
1.0

Member Function Documentation

void* lunchbox::MemoryMap::create ( const std::string &  filename,
const size_t  size 
)

Create a writable file to a memory address.

The file is mapped read-write. An existing file will be overwritten. The file is automatically unmapped when the memory map is deleted.

Parameters
filenameThe filename of the file to map.
sizethis size of the file.
Returns
the pointer to the mapped file, or 0 upon error.
Version
1.9.1
const void* lunchbox::MemoryMap::getAddress ( ) const
Returns
the pointer to the memory map.
Version
1.0

Referenced by getAddress().

+ Here is the caller graph for this function:

void* lunchbox::MemoryMap::getAddress ( )
Returns
the pointer to the memory map.
Version
1.9.1
template<class T >
const T* lunchbox::MemoryMap::getAddress ( ) const
inline
Returns
the pointer to the memory map.
Version
1.9.1

Definition at line 125 of file memoryMap.h.

template<class T >
T* lunchbox::MemoryMap::getAddress ( )
inline
Returns
the pointer to the memory map.
Version
1.9.1

Definition at line 129 of file memoryMap.h.

References getAddress(), getSize(), and lunchbox::operator<<().

+ Here is the call graph for this function:

size_t lunchbox::MemoryMap::getSize ( ) const
Returns
the size of the memory map.
Version
1.0

Referenced by getAddress().

+ Here is the caller graph for this function:

const void* lunchbox::MemoryMap::map ( const std::string &  filename)

Map a file to a memory address.

The file is only mapped read-only. The file is automatically unmapped when the memory map is deleted.

Parameters
filenameThe filename of the file to map.
Returns
the pointer to the mapped file, or 0 upon error.
Version
1.0
void* lunchbox::MemoryMap::recreate ( const std::string &  filename,
const size_t  size 
)

Recreate a different writable file for this memory map.

The file is only mapped read-write. An existing map is unmapped.

Parameters
filenameThe filename of the file to map.
sizethis size of the file.
Returns
the pointer to the mapped file, or 0 upon error.
Version
1.0
const void* lunchbox::MemoryMap::remap ( const std::string &  filename)

Remap a different file for this memory map.

The file is only mapped read-only. An existing map is unmapped.

Parameters
filenameThe filename of the file to map.
Returns
the pointer to the mapped file, or 0 upon error.
Version
1.9.1
void lunchbox::MemoryMap::unmap ( )

Unmap the file.

Version
1.0

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