Borkware Quickies is a highly recommended collection of small, useful code snippets. Mark Dalrymple has been so kind to post one of my own there: “Making naked memory autoreleased”. Here’s a shorter and even more useful, though sometimes slower, version:
static void* tempCopyOf(void* data,UInt32 size) {
void* buffer = calloc(1,size);
if (buffer) {
if (data) bcopy(data,buffer,size);
[NSData dataWithBytesNoCopy:buffer length:size freeWhenDone:YES];
}
return buffer;
}
So, you can call this as:
void* thing = tempCopyOf(&myStructure,sizeof(myStructure));
which will give you a temporary copy of myStructure, or as
void* thing = tempCopyOf(NULL,someSize);
which will return a zero-filled buffer of someSize for you to fill in as you want.
Leave a Comment