Re: Deleting an file that's been NSData memory mapped - safe?
Re: Deleting an file that's been NSData memory mapped - safe?
- Subject: Re: Deleting an file that's been NSData memory mapped - safe?
- From: Dan Lau <email@hidden>
- Date: Thu, 11 Feb 2016 12:07:45 -0800
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 <email@hidden> wrote:
>
> On Feb 11, 2016, at 11:44 AM, Dan Lau <email@hidden> 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 (email@hidden)
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:
This email sent to email@hidden