Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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

LUNCHBOX_API MemoryMap ()
 Construct a new memory map. More...
 
LUNCHBOX_API MemoryMap (const std::string &filename)
 Construct and initialize a new, readonly memory map. More...
 
LUNCHBOX_API MemoryMap (const std::string &filename, const size_t size)
 Construct and initialize a new, read-write memory map. More...
 
LUNCHBOX_API ~MemoryMap ()
 Destruct the memory map. More...
 
LUNCHBOX_API const void * map (const std::string &filename)
 Map a file to a memory address. More...
 
LUNCHBOX_API const void * remap (const std::string &filename)
 Remap a different file for this memory map. More...
 
LUNCHBOX_API void * create (const std::string &filename, const size_t size)
 Create a writable file to a memory address. More...
 
LUNCHBOX_API void * recreate (const std::string &filename, const size_t size)
 Recreate a different writable file for this memory map. More...
 
LUNCHBOX_API void unmap ()
 Unmap the file. More...
 
LUNCHBOX_API const void * getAddress () const
 
LUNCHBOX_API void * getAddress ()
 
template<class T >
const T * getAddress () const
 
template<class T >
T * getAddress ()
 
LUNCHBOX_API 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 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.
*/
#include <test.h>
#include <lunchbox/memoryMap.h>
#define MAP_SIZE LB_10MB
#define STRIDE 23721
int main( int, char** )
{
MemoryMap map( "foo.mmap", MAP_SIZE );
TESTINFO( map.getSize() == MAP_SIZE, map.getSize( ));
uint8_t* writePtr = map.getAddress< uint8_t >();
TEST( 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" );
TEST( !noPtr );
TEST( map.getSize() == 0 );
map.map( "foo.mmap" );
const uint8_t* readPtr = map.getAddress< uint8_t >();
TEST( readPtr );
TEST( map.getSize() == MAP_SIZE );
for( size_t i=0; i < MAP_SIZE; i += STRIDE )
TEST( readPtr[i] == uint8_t( i ));
return EXIT_SUCCESS;
}

Definition at line 36 of file memoryMap.h.

Constructor & Destructor Documentation

LUNCHBOX_API lunchbox::MemoryMap::MemoryMap ( )

Construct a new memory map.

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

Construct and initialize a new, readonly memory map.

Version
1.7.1
LUNCHBOX_API lunchbox::MemoryMap::MemoryMap ( const std::string &  filename,
const size_t  size 
)

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

Version
1.9.1
LUNCHBOX_API lunchbox::MemoryMap::~MemoryMap ( )

Destruct the memory map.

Unmaps the file, if it is still mapped.

See Also
unmap()
Version
1.0

Member Function Documentation

LUNCHBOX_API 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
LUNCHBOX_API 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:

LUNCHBOX_API 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 116 of file memoryMap.h.

References getAddress().

+ Here is the call graph for this function:

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

Definition at line 120 of file memoryMap.h.

References getAddress().

+ Here is the call graph for this function:

LUNCHBOX_API size_t lunchbox::MemoryMap::getSize ( ) const
Returns
the size of the memory map.
Version
1.0
LUNCHBOX_API 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
LUNCHBOX_API 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
LUNCHBOX_API 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
LUNCHBOX_API void lunchbox::MemoryMap::unmap ( )

Unmap the file.

Version
1.0

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