Ticket #3571: mmap1.cpp

File mmap1.cpp, 1.2 KB (added by anonymous, 13 years ago)
Line 
1
2#include <iostream>
3#include <exception>
4
5#include <boost/iostreams/device/mapped_file.hpp>
6namespace bio = boost::iostreams;
7
8using namespace std;
9
10int main( void ) {
11 try {
12 std::string fileName( "test.dat" );
13 cerr << fileName <<endl;
14
15 int osAlignment = bio::mapped_file_source::alignment();
16 cerr << osAlignment <<endl; //65536 (WinXP)
17
18 bio::mapped_file_source fileMap( fileName, 2 * osAlignment, 0); //filename, sz, offset
19 cerr << fileMap.is_open() <<endl; //1, ok
20 cerr << "open ok!" <<endl;
21
22 // Load data to application memory using fileMap.data();
23
24 fileMap.close();
25 cerr << "close ok!" <<endl;
26 cerr << fileMap.is_open() <<endl; //0, ok
27
28 // The following line throws because fileMap.is_open() returns true
29 fileMap.open( fileName, 1024 * osAlignment, 1024 * osAlignment );
30 cerr << "reopen ok!" <<endl;
31
32 // Load more data to application memory using fileMap.data();
33 }
34 catch (std::exception& e) {
35 cout << "Exception: \"" << e.what() << "\" caught." << endl;
36 }
37
38 return 0;
39}
40
41// 2nd open fails with:
42// Exception: "failed mapping view: access denied" caught.
43// mingw gcc 4.2.1 / Windows XP / boost 1.36.0