• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: implementing dataWithContentsOfMemory as NSData category
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: implementing dataWithContentsOfMemory as NSData category


  • Subject: Re: implementing dataWithContentsOfMemory as NSData category
  • From: Mike Bolton <email@hidden>
  • Date: Thu, 16 Sep 2004 17:26:55 -0400 (EDT)

Thanks for the reply Fritz.  One of the reasons I
wanted to use NSData, was because I can easily use the
[NSData dataWithContentsOfMappedFile] when I need to
access the file on disk, and I wanted to have the same
functionality with the same interface for when I need
to access the file in memory.  It would also be nice
to have the memory free'd when the object is released,
rather than have to worry about that myself.

I ended up subclassing NSData and implementing
"initWithContentsOfMemory" which basically just calls
vm_map and fills in two instance variables, unsigned
long length, and void *addr, along with the
appropriate accessor methods.  In dealloc, I just call
vm_deallocate, to take care of the deallocation of the
memory.  I also implemented a category method for
NSData so that I can use "+ [NSData
dataWithContentsOfMemory]".  This seems like it will
do what I wanted, although I haven't had a chance to
test it out yet, so I'm keeping my fingers crossed ;)

Someone also explained to me that another alternative
would be to create a function that calls
CFDataCreateWithBytesNoCopy with a custom deallocator
(which calls vm_deallocate) and returns a CFDataRef.

Thanks again for the reply,

Mike



--- Fritz Anderson <email@hidden> wrote:
> The method you are missing is
>   + (id)dataWithBytesNoCopy:(void *)bytes
> length:(unsigned)length
> freeWhenDone:(BOOL)freeWhenDone;
>
> 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/
>
>

______________________________________________________________________
Post your free ad now! http://personals.yahoo.ca
 _______________________________________________
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

References: 
 >Re: implementing dataWithContentsOfMemory as NSData category (From: Fritz Anderson <email@hidden>)

  • Prev by Date: Super slow task performance...
  • Next by Date: Re: NSAppKitVersionNumber for 10.3.0?
  • Previous by thread: Re: implementing dataWithContentsOfMemory as NSData category
  • Next by thread: Re: modifying self in init
  • Index(es):
    • Date
    • Thread