3

I am trying to understand how Boost memory mapped files work. The following code works, it does what it is supposed to do, but the problem is that the file it generates is stored on disk (in the same directory of the executable) instead of memory. Maybe there is a flag to set somewhere, but I could not find it...
Thanks in advance for any info!

    #include <iostream>
    #include <string>
    #include <cstring>
    #include <boost/iostreams/device/mapped_file.hpp>
    using std::cout;
    using std::endl;

    int main(int argc, char** argv) {
     const int blockSize = 64;
     bool writer = false;

     if(argc > 1) {
      if(!strcmp(argv[1], "w"))
       writer = true;
     }

     boost::iostreams::mapped_file_params  params;
     params.path = "map.dat";
    // params.length = 1024;     // default: all the file
     params.new_file_size = blockSize;

     if(writer) {
      cout << "Writer" << endl;
      params.mode = std::ios_base::out;
     }
     else {
      cout << "Reader" << endl;
      params.mode = std::ios_base::in;
     }

        boost::iostreams::mapped_file  mf;
        mf.open(params);

     if(writer)
     {
      char *block = mf.data();
      strcpy(block, "Test data block...\0");
      cout << "Written: " << block << endl;
     }
     else
     {
      cout << "Reading: " << mf.const_data() << endl;
     }

     mf.close();

        return 0;
    }
/*
    Compiler options: -Wall -I$(PATH_BOOST_INCLUDE) -ggdb
    Linker options: -L$(PATH_BOOST_LIBS) -lboost_iostreams-mt -lboost_system-mt -lboost_filesystem-mt -DBOOST_FILESYSTEM_NO_DEPRECATED
*/

Compiler used: gcc 4.2.1
Boost 1.41.0
OS: MacOS X 10.6.2

Pietro
  • 12,086
  • 26
  • 100
  • 193

2 Answers2

12

Memory mapping maps disk files into memory. There has to be a file on disk for this to happen!

Edit: From your comments, it sounds like you want to use shared memory - see http://www.boost.org/doc/libs/1_41_0/doc/html/interprocess/quick_guide.html

  • sorry, but mmap with the MAP_ANONYMOUS flag doesn't require or create a file system object. – Pete Kirkham Dec 20 '09 at 16:41
  • The MAP_ANONYMOUS flag provides a mapping identical to that obtained by mapping /dev/zero. This can be used in some very special circumstances, but is not what the question is about, I think. –  Dec 20 '09 at 16:44
  • I am not using mmap, but boost::iostreams::mapped_file. That is what my problem is related to. – Pietro Dec 20 '09 at 17:29
  • I would also like the file to be created directly in memory, without a copy on disk. Let's say it is just temporary data. – Pietro Dec 20 '09 at 17:31
  • 1
    Then why not simply use a std::vector or similar? –  Dec 20 '09 at 17:37
  • 1
    Because I want this data to be shared between different applications. – Pietro Dec 20 '09 at 17:40
  • Thank you Neil, what I need is shared memory. I am using an SSD, and I do not want to stress it if not strictly necessary. – Pietro Dec 20 '09 at 18:12
2

Memory mapped files are specifically about causing the contents of a file to show up as a region of memory in your program. They are not about creating a special 'in memory' file. Boost's concept is taken directly from the availability of the mmap system call in Unix and similar facilities in most other operating systems. It is designed to be a generic wrapper around this capability.

If what you are trying to do is create a region of memory shared by two processes, memory mapped files can do that for you, but at the cost of having an on-disk file that reflects the contents of that memory. In Linux this will still be reasonably efficient since the file contents will simply be cached in memory and your program's memory pages corresponding to the file contents will be the same exact physical pages as are being used by the cache.

If what you want is just a ramdisk...

Most modern operating systems use memory for disk cache that isn't needed by processes. There is no real need for a ramdisk. Under Linux there is a filesystem type called tmpfs that you can use for filesystems that do not have to persist between boots. Its files will generally be in memory, but they can be swapped out just like any other sort of memory can be.

Yes, system V shared memory exists, and it has an absolutely abysmal design. I wouldn't touch the sys V interprocess communications primitives with a 10-foot pole.

Omnifarious
  • 54,333
  • 19
  • 131
  • 194
  • So tmpfs does not touch the hard disk at all (unless it gets full, or an explicit "flush" is required)? – Pietro Dec 20 '09 at 18:17
  • @Pietro, sort of. tmpfs pages will be treated like any other memory pages. So they will get swapped out when the system is under memory pressure. – Omnifarious Dec 21 '09 at 13:55