memory mapped file stream

D

Divick

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
 
M

Maxim Yegorushkin

Divick said:
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
 
R

Richard Herring

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.
 
D

Divick

If you're not actually concerned with memory mapping, but just want an
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
 
?

=?iso-8859-1?q?Stephan_Br=F6nnimann?=

Divick said:
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
 
D

Darko Miletic

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top