I want to have a fix length mutable content shared data buffer and that is how do I create it:
void *buffer = malloc(length);
// initialize buffer content
NSData *sharedData = [[NSData alloc] initWithBytesNoCopy:buffer length:length freeWhenDone:YES]
What happen if I modify buffer after I created a NSData from it? Will NSData reflect the change I did to buffer?
I can guaranty that sharedData will not get dealloc when I want to modify buffer.
This is how I actually want to use it:
void *my_alloc(CFIndex allocSize, CFOptionFlags hint, void *info) {return NULL;}
void my_dealloc(void *ptr, void *info) {
mach_vm_deallocate(mach_task_self(), (mach_vm_address_t)ptr, (size_t)info);
}
size_t length = //some number
mach_vm_address_t buffer;
mach_vm_allocate(mach_task_self(), &buffer, length, VM_FLAGS_ANYWHERE);
// do something to buffer, for example pass to other process using mach RPC and expect other process will modify the content
CFAllocatorContext context = {0, (void *)length, NULL, NULL, NULL, my_alloc, NULL, my_dealloc, NULL};
CFAllocatorRef allocator = CFAllocatorCreate(NULL, &context);
CFDataCreateWithBytesNoCopy(NULL, (const UInt8 *)buffer, length, allocator);
The
initWithBytesNoCopy:will effectively create anNSDatawrapper around the existing buffer; so yes, things accessing through[sharedData bytes]will see any updates you make.Of course, it does nothing to link other objects which are created from the
NSDatainstance, so for instance an[NSImage initWithData:sharedData]may make a copy for theNSImageinstance, which won’t reflect any changes.Also, with
freeWhenDone:YEStheNSDatawill destroy the buffer when the last reference is removed, so look out for that =)So, given that the
NSDatais effectively a thin wrapper around amalloc()allocation, yes it will reflect changes made to that memory (by any process); but as it will callfree()on it, it’s a bad idea to use it to wrap a buffer created another way (mach_vm_allocate) withfreeWhenDone:YES.If you don’t really-really need to use a custom allocator (why?), I think you’d be better off with: