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: Jens Alfke <email@hidden>
- Date: Thu, 11 Feb 2016 12:03:18 -0800
> 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