Re: implementing dataWithContentsOfMemory as NSData category
Re: implementing dataWithContentsOfMemory as NSData category
- Subject: Re: implementing dataWithContentsOfMemory as NSData category
- From: Fritz Anderson <email@hidden>
- Date: Thu, 16 Sep 2004 14:41:33 -0500
The method you are missing is
<x-tad-bigger>+ (id)dataWithBytesNoCopy:(void *)bytes length:(unsigned)length freeWhenDone:(BOOL)freeWhenDone;</x-tad-bigger>
It does require 10.2 or later.
My impulse is that anything that needs the exquisite control of the silicon provided by the Mach vm_map facility would be ruined by wrapping it in NSData. Conversely, if your needs are high-level and abstract enough to be modeled by NSData, vm_map can only do mischief. The Mac OS X virtual-memory system does not do a perfect job of optimizing among RAM, disk, and the user's needs across many applications, but it apt to do better than you or I.
-- F
On 15 Sep 2004, at 9:25 PM, Mike Bolton wrote:
I now have a question regarding NSData. At the
moment, I use an NSData object and
dataWIthContentsOfMappedFile to map a file from disk
to memory so that I can parse the contents. The
problem is that I also need to be able to parse these
files while they're in memory (in another task). I
was thinking of extending NSData by adding my own
category to implement a method such as:
+ (NSData *) dataWithContentsOfMemory:(const void
*)addr size:(unsigned long)size
task:(task_t)target_task;
and then in the actual method itself, I'd use
something like vm_map to map the memory to addr. The
question is, how do I then create an NSData object to
wrap around this address. I was thinking of using: "+
[NSData dataWithBytesNoCopy:(void *)bytes
length:(unsigned)length]", but it states in the
documentation that "bytes must point to a memory block
allocated with malloc.", so I'm not sure if this will
work. And using "+ [NSData dataWithBytes:(const void
*)bytes length:(unsigned)length]", will cause the
entire chunk of memory to be copied, which is not what
I want either. So what I'm wondering is if there's a
way to create an NSData object that represents
vm_map'd memory, given that none of the methods
available in the NSData class provide this
functionality (unless I'm missing something).
--
Fritz Anderson
Consulting Programmer Chicago, Illinois
http://resume.manoverboard.org/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden