Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

Mem0ry Mapped files?

This topic is 2779 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm still a little confused on how a memory mapped file works. If anyone could provide a easy to understand explanation id appreciate it!

Share this post


Link to post
Share on other sites
Advertisement
And if the explanations available from google still leave you confused, consider referencing one of them here and ask about the specific parts of it you don't understand. After all, if we just "explain memory mapping", we'll only end up repeating what's already been said — a waste of everyone's time, yours included.

Share this post


Link to post
Share on other sites
mmaps go like this:

$ man mmap
*snip*
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define handle_error(msg) do { perror(msg); exit(EXIT_FAILURE); } while (0)

int
main(int argc, char *argv[])
{
char *addr;
int fd;
struct stat sb;
off_t offset, pa_offset;
size_t length;
ssize_t s;

if (argc < 3 || argc > 4) {
fprintf(stderr, "%s file offset [length]\n", argv[0]);
exit(EXIT_FAILURE);
}

fd = open(argv[1], O_RDONLY);
if (fd == -1)
handle_error("open");

if (fstat(fd, &sb) == -1) /* To obtain file size */
handle_error("fstat");

offset = atoi(argv[2]);
pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1);
/* offset for mmap() must be page aligned */

if (offset >= sb.st_size) {
fprintf(stderr, "offset is past end of file\n");
exit(EXIT_FAILURE);
}

if (argc == 4) {
length = atoi(argv[3]);
if (offset + length > sb.st_size)
length = sb.st_size - offset;
/* Can't display bytes past end of file */

} else { /* No length arg ==> display to end of file */
length = sb.st_size - offset;
}

addr = mmap(NULL, length + offset - pa_offset, PROT_READ,
MAP_PRIVATE, fd, pa_offset);
if (addr == MAP_FAILED)
handle_error("mmap");

s = write(STDOUT_FILENO, addr + offset - pa_offset, length);
if (s != length) {
if (s == -1)
handle_error("write");

fprintf(stderr, "partial write");
exit(EXIT_FAILURE);
}

exit(EXIT_SUCCESS);
} /* main */
*snip*



Was this helpful? If not, please be more specific.

Share this post


Link to post
Share on other sites
if i understand correctly it u can use memorymapping to pretty much edit a file while it's still on the hard drive. Memory mapping creates virtual addresses for the contents of the file in which u can edit/read while it's still on the hard drive? is this correct?

im confused on how memory mapping can be used to load a file to be run like this....im not even sure if i completely understand it -thx

Share this post


Link to post
Share on other sites
Quote:
Original post by nuclear123
if i understand correctly it u can use memorymapping to pretty much edit a file while it's still on the hard drive. Memory mapping creates virtual addresses for the contents of the file in which u can edit/read while it's still on the hard drive? is this correct?

im confused on how memory mapping can be used to load a file to be run like this....im not even sure if i completely understand it -thx


Your assumption is correct. However I don't understand what you mean by the second statement. Are you refering to executing code or do you want to know how memory mapping is actually implemented?

Share this post


Link to post
Share on other sites
"like virtual memory,memory mapped files allow u to reserve a ragion of address space and commit physical storage to the region. the difference is that the physical storage comes from a file that is already on disk instead of the systems paging file. Once the file has been mapped, you can access it as if the while file were loaded in memory" <-- windows via c/c++ book. So in other words there are 2 ways to loading a file to be run??? how does this not use the systems paging file? if the code that your requesting from the file is not in memory, it must be PAGED in from the HD right???

Share this post


Link to post
Share on other sites
Quote:
Original post by nuclear123
So in other words there are 2 ways to loading a file to be run???

They're comparing opening it with traditional, stream based methods, and reading the entire thing into memory. In C++ you'd handle a "FILE" or fstream, C# has various Stream objects, etc.

Quote:
how does this not use the systems paging file?

The paging file is used by the OS to back pages of memory that:
1) Needs to get 'paged out' because we don't have enough RAM to keep everything in real memory
2) The OS can't tell is already backed by something else. If we manually load things into memory, the OS knows little about it -- just that it's important data. If we use memory mapping, the OS can go, oh hey, this memory just represents this file. I can back this memory with that file instead of using the page file.

Note that you can disable your paging file in many/most OSes - it's not a fundamental part of your memory system. It's there so you can pretend you have more memory than you do, basically, which is surprisingly handy.

If you've got enough physical memory, the OS doesn't need to use the page file -- it can just store all the memory pages in actual memory. If you've memory mapped a file, the OS can use that file directly, and again doesn't need to use the page file.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!