How does mmap'ed data work with object distribution?

I'm a little confused about how it mmap()

works with frameworks on iOS or OSX.

If a file is mapped to virtual memory using mmap()

, and data is requested from it, the kernel pages are in data in RAM. How does this affect the creation of objects?

If you normally create an object with alloc

/ init

, a block of memory is allocated and the object is initialized. But what if the data is in virtual memory in the mmap

'ed file ? Do I need to alloc

call the object? Does the allocated memory for an object span data from virtual memory? Or skip the call alloc

and just pass the pointer to the data in virtual memory?

eg. image or sound file, if I know where the file is in virtual memory, how would I set up the object?

If one allocates data, is it possible to duplicate it if the data is already swapped out to RAM?

I thought that using memory from virtual addresses would eliminate the need for heap allocation.

+3


source to share


1 answer


If you only have one object that you store in mmaped space, then you just skip the selection and use the location directly. Typically, however, you will have more than one object, and now you manage it yourself. Typically, at least part of it will be lined up in a fixed way so that both processes know where to look for things. Instead of pointers, you enjoy using offsets from the start of the arena, as it works in the address space of both processes.

Basically, you are given a chunk of memory as if you had done one big malloc / alloc and you can play with it.

If you have, tell

void *p = mmap( <appropriate arguments> );

      



and you want to place an object like foo at offset 200, you would say

foo *f = (foo *)p+200;

      

and now you can manipulate f in all normal ways, provided that f does not contain any pointers in mmapped space. It is generally good discipline to replace offsets for such pointers, and then when you need to follow one, you can convert it to a pointer (by adding p).

+1


source







All Articles