Thanks Jens, that makes sense.

This brings up a related question that I haven't seen a definitive answer
to - on iOS, does NSDataReadingMappedIfSafe always map the file? After all,
the iOS apps can only reference local files on the device.

On Thu, Feb 11, 2016 at 12:03 PM, Jens Alfke <j...@mooseyard.com> wrote:

>
> On Feb 11, 2016, at 11:44 AM, Dan Lau <d...@twitter.com> wrote:
>
> If a file has its contents mapped using NSData's
> initWithContentsOfFile + NSDataReadingMappedIfSafe,
> deleting it doesn't appear to affect reading it's mapped contents. Does
> anyone know if NSData uses mmap(2) under the hood to ensure that this sort
> of operation is valid?
>
>
> Yes. (mmap() is the only way to memory-map a file, that I know of.)
>
> You can delete a memory-mapped file without problems, for the same reason
> you can delete an open file and keep reading/writing it. Files are
> reference-counted, and the filesystem doesn’t delete a file until its
> number of links goes to zero. An open file handle counts as a link.
>
> Also note that NSDataReadingMappedIfSafe won’t always map the file. I
> think the rule is that files on external filesystems won’t be mapped, just
> copied into memory. The reason is that if the filesystem is unexpectedly
> disconnected, accessing the mapped memory will crash with a segfault.
>
> —Jens
>
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to