Я хочу иметь общий буфер данных с изменяемой длиной и вот как я могу его создать:
void *buffer = malloc(length);
// initialize buffer content
NSData *sharedData = [[NSData alloc] initWithBytesNoCopy:buffer length:length freeWhenDone:YES]
Что произойдет, если я изменю buffer
после того, как создаю из него NSData? Отражает ли NSData изменение, которое я внес в buffer
?
Я могу гарантировать, что sharedData
не получит dealloc, когда я хочу изменить buffer
.
Вот как я на самом деле хочу это использовать:
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);