memory mapped file stream

Discussion in 'C++' started by Divick, Jan 19, 2006.

  1. Divick

    Divick Guest

    Is there a way in C++ to have a file stream which is directly mapped to
    memory(virtual memory) without actually having to create a file and
    then memory mapping that file using system dependent calls?

    I need this because I don't want to create a regular file on disk
    (reasons for which are many , say directory permissions ), but being a
    file pointer, someone can use that pointer as a regular file descriptor
    for reading and writing.

    Thanks,
    Divick
     
    Divick, Jan 19, 2006
    #1
    1. Advertising

  2. Divick wrote:
    > Is there a way in C++ to have a file stream which is directly mapped to
    > memory(virtual memory) without actually having to create a file and
    > then memory mapping that file using system dependent calls?


    C++ does not provide such facilities. Platforms do.

    > I need this because I don't want to create a regular file on disk
    > (reasons for which are many , say directory permissions ), but being a
    > file pointer, someone can use that pointer as a regular file descriptor
    > for reading and writing.


    >From words "file descriptor" I gather you are using some Unix-like OS.

    Use mmap() to map memory not backed by a file.

    http://www.opengroup.org/onlinepubs/009695399/functions/mmap.html
     
    Maxim Yegorushkin, Jan 19, 2006
    #2
    1. Advertising

  3. In message <>,
    Divick <> writes
    >Is there a way in C++ to have a file stream which is directly mapped to
    >memory(virtual memory) without actually having to create a file and
    >then memory mapping that file using system dependent calls?
    >
    >I need this because I don't want to create a regular file on disk
    >(reasons for which are many , say directory permissions ), but being a
    >file pointer, someone can use that pointer as a regular file descriptor
    >for reading and writing.
    >

    If you're not actually concerned with memory mapping, but just want an
    object that can be read and written via operators >> and << without
    being tied to a physical file, you might consider using a stringstream.

    --
    Richard Herring
     
    Richard Herring, Jan 19, 2006
    #3
  4. Divick

    Divick Guest

    >>If you're not actually concerned with memory mapping, but just want an
    >>object that can be read and written via operators >> and << without
    >>being tied to a physical file, you might consider using a stringstream.


    My concern is not operators but a library which requires a file stream
    for reading and writing. Since it is a C library, I am pretty sure that
    it is not using << and >> operators for reading and writing. I assume
    that it uses fread and fwrite calls for reading and writing.

    Also although I am writing code on Linux but I also want the same code
    to run on windows with minimal changes on windows as well, hence mmap
    is not suitable for me as it can map a file to stream but I don't want
    at all to create a regular file, apart from mmap being system dependent
    call.

    I can across common cpp library on gnu, which is cross platform (*nix
    and windows only) which provides a portable MappedFile class using
    which one can map, unmap and sync mapped files. But again it creates a
    files which is what I don't want. Also just to have a mapped file, I
    don't want to link with a totally new library.

    Divick
     
    Divick, Jan 19, 2006
    #4
  5. Divick wrote:
    > >>If you're not actually concerned with memory mapping, but just want an
    > >>object that can be read and written via operators >> and << without
    > >>being tied to a physical file, you might consider using a stringstream.

    >
    > My concern is not operators but a library which requires a file stream
    > for reading and writing. Since it is a C library, I am pretty sure that
    > it is not using << and >> operators for reading and writing. I assume
    > that it uses fread and fwrite calls for reading and writing.
    >
    > Also although I am writing code on Linux but I also want the same code
    > to run on windows with minimal changes on windows as well, hence mmap
    > is not suitable for me as it can map a file to stream but I don't want
    > at all to create a regular file, apart from mmap being system dependent
    > call.
    >
    > I can across common cpp library on gnu, which is cross platform (*nix
    > and windows only) which provides a portable MappedFile class using
    > which one can map, unmap and sync mapped files. But again it creates a
    > files which is what I don't want. Also just to have a mapped file, I
    > don't want to link with a totally new library.
    >
    > Divick


    Why not use TCP/IP (sockets) then?

    Stephan
     
    =?iso-8859-1?q?Stephan_Br=F6nnimann?=, Jan 19, 2006
    #5
  6. Divick

    Divick Guest

    That would be like killing a fly by a sword.

    Divick
     
    Divick, Jan 21, 2006
    #6
  7. On 19 Jan 2006 09:39:38 -0800, "Divick" <>
    wrote:

    >I can across common cpp library on gnu, which is cross platform (*nix
    >and windows only) which provides a portable MappedFile class using
    >which one can map, unmap and sync mapped files. But again it creates a
    >files which is what I don't want. Also just to have a mapped file, I
    >don't want to link with a totally new library.


    Then write your ovn MemoryMappedFile class.
    You will have one header and at least two cpp files. In one you will
    implement class with unix functions and in another with windows API
    functions for that.

    That's what I did. It was couple of days worth spending.


    Darko
     
    Darko Miletic, Jan 31, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    2
    Views:
    439
  2. Srijit Kumar Bhadra

    Memory mapped File (Python win32 extensions)

    Srijit Kumar Bhadra, Mar 17, 2005, in forum: Python
    Replies:
    1
    Views:
    432
    Thomas Heller
    Mar 17, 2005
  3. Carl Mackey

    mmap -- memory mapped file

    Carl Mackey, Jun 29, 2006, in forum: Python
    Replies:
    1
    Views:
    2,593
    Alex Martelli
    Jun 29, 2006
  4. Replies:
    3
    Views:
    315
    Walter Roberson
    Jan 10, 2006
  5. Replies:
    0
    Views:
    283
Loading...

Share This Page