static void __CFMessagePortReplyCallBack(CFMachPortRef port, void *msg, CFIndex size, void *info) { CFMessagePortRef ms = info; struct __CFMessagePortMachMessage *msgp = msg; struct __CFMessagePortMachMessage *replymsg; __CFMessagePortLock(ms); if (!__CFMessagePortIsValid(ms)) { __CFMessagePortUnlock(ms); return; } // assert: (int32_t)msgp->head.msgh_id < 0 if (CFDictionaryContainsKey(ms->_replies, (void *)msgp->head.msgh_id)) { CFDataRef reply = NULL; replymsg = (struct __CFMessagePortMachMessage *)msg; if (0 == replymsg->body.msgh_descriptor_count) { int32_t byteslen = CFSwapInt32LittleToHost(replymsg->contents.msg0.byteslen); if (0 <= byteslen) { reply = CFDataCreate(kCFAllocatorSystemDefault, replymsg->contents.msg0.bytes, byteslen); } else { reply = (void *)0xffffffff; // means NULL data } } else { //#warning CF: should create a no-copy data here that has a custom VM-freeing allocator, and not vm_dealloc here reply = CFDataCreate(kCFAllocatorSystemDefault, replymsg->contents.msg1.desc.out_of_line.address, replymsg->contents.msg1.desc.out_of_line.size); vm_deallocate(mach_task_self(), (vm_address_t)replymsg->contents.msg1.desc.out_of_line.address, replymsg->contents.msg1.desc.out_of_line.size); } CFDictionarySetValue(ms->_replies, (void *)msgp->head.msgh_id, (void *)reply); } else { /* discard message */ if (1 == msgp->body.msgh_descriptor_count) { vm_deallocate(mach_task_self(), (vm_address_t)msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size); } } __CFMessagePortUnlock(ms); }
//Swap int32 to system endianness int32_t swapBytes_i32(int32_t val, bql_file *blendfile) { if(blendfile->endianness == CFByteOrderLittleEndian) return CFSwapInt32LittleToHost(val); else if(blendfile->endianness == CFByteOrderBigEndian) return CFSwapInt32BigToHost(val); else return val; }
static void show(const AudioBufferList &abl, int framesToPrint, int wordSize, const char *label, const char *fmtstr=NULL) { printf("%s %p (%d fr%s):\n", label ? label : "AudioBufferList", &abl, framesToPrint, fmtstr ? fmtstr : ""); const AudioBuffer *buf = abl.mBuffers; for (UInt32 i = 0; i < abl.mNumberBuffers; ++i, ++buf) { printf(" [%2d] %5dbytes %dch @ %p", (int)i, (int)buf->mDataByteSize, (int)buf->mNumberChannels, buf->mData); if (framesToPrint && buf->mData != NULL) { printf(":"); Byte *p = (Byte *)buf->mData; for (int j = framesToPrint * buf->mNumberChannels; --j >= 0; ) switch (wordSize) { case 0: // native float printf(" %6.3f", *(Float32 *)p); p += sizeof(Float32); break; // positive: big endian case 1: case -1: printf(" %02X", *p); p += 1; break; case 2: printf(" %04X", CFSwapInt16BigToHost(*(UInt16 *)p)); p += 2; break; case 3: printf(" %06X", (p[0] << 16) | (p[1] << 8) | p[2]); p += 3; break; case 4: printf(" %08X", (unsigned int)CFSwapInt32BigToHost(*(UInt32 *)p)); p += 4; break; case 10: printf(" %6.3f", CASwapFloat32BigToHost(*(Float32 *)p)); p += sizeof(Float32); break; case -2: printf(" %04X", CFSwapInt16LittleToHost(*(UInt16 *)p)); p += 2; break; case -3: printf(" %06X", (p[2] << 16) | (p[1] << 8) | p[0]); p += 3; break; case -4: printf(" %08X", (unsigned int)CFSwapInt32LittleToHost(*(UInt32 *)p)); p += 4; break; case -10: printf(" %6.3f", CASwapFloat32LittleToHost(*(Float32 *)p)); p += sizeof(Float32); break; } } printf("\n"); } }
static short file_read_int16_le(char* buffer, FILE* file) { fread(buffer, 1, 2, file); printf("Buffer contents: "); for (int i=0; i<sizeof(buffer)/sizeof(char); i++) { printf("%c", (char)buffer[i]); } printf("\n"); unsigned int val = *(unsigned int*)buffer; return CFSwapInt32LittleToHost(val); }
static void *__CFMessagePortPerform(void *msg, CFIndex size, CFAllocatorRef allocator, void *info) { CFMessagePortRef ms = info; struct __CFMessagePortMachMessage *msgp = msg; struct __CFMessagePortMachMessage *replymsg; void *context_info; void (*context_release)(const void *); CFDataRef returnData, data = NULL; void *return_bytes = NULL; CFIndex return_len = 0; int32_t msgid; __CFMessagePortLock(ms); if (!__CFMessagePortIsValid(ms)) { __CFMessagePortUnlock(ms); return NULL; } // assert: 0 < (int32_t)msgp->head.msgh_id if (NULL != ms->_context.retain) { context_info = (void *)ms->_context.retain(ms->_context.info); context_release = ms->_context.release; } else { context_info = ms->_context.info; context_release = NULL; } __CFMessagePortUnlock(ms); /* Create no-copy, no-free-bytes wrapper CFData */ if (0 == msgp->body.msgh_descriptor_count) { int32_t byteslen = CFSwapInt32LittleToHost(msgp->contents.msg0.byteslen); msgid = CFSwapInt32LittleToHost(msgp->contents.msg0.msgid); if (0 <= byteslen) { data = CFDataCreateWithBytesNoCopy(kCFAllocatorSystemDefault, msgp->contents.msg0.bytes, byteslen, kCFAllocatorNull); } } else { msgid = CFSwapInt32LittleToHost(msgp->contents.msg1.msgid); data = CFDataCreateWithBytesNoCopy(kCFAllocatorSystemDefault, msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size, kCFAllocatorNull); } returnData = ms->_callout(ms, msgid, data, context_info); /* Now, returnData could be (1) NULL, (2) an ordinary data < MAX_INLINE, (3) ordinary data >= MAX_INLINE, (4) a no-copy data < MAX_INLINE, (5) a no-copy data >= MAX_INLINE. In cases (2) and (4), we send the return bytes inline in the Mach message, so can release the returnData object here. In cases (3) and (5), we'll send the data out-of-line, we need to create a copy of the memory, which we'll have the kernel autodeallocate for us on send. In case (4) also, the bytes in the return data may be part of the bytes in "data" that we sent into the callout, so if the incoming data was received out of line, we wouldn't be able to clean up the out-of-line wad until the message was sent either, if we didn't make the copy. */ if (NULL != returnData) { return_len = CFDataGetLength(returnData); if (return_len < __CFMessagePortMaxInlineBytes) { return_bytes = (void *)CFDataGetBytePtr(returnData); } else { return_bytes = NULL; vm_allocate(mach_task_self(), (vm_address_t *)&return_bytes, return_len, VM_FLAGS_ANYWHERE | VM_MAKE_TAG(VM_MEMORY_MACH_MSG)); /* vm_copy would only be a win here if the source address is page aligned; it is a lose in all other cases, since the kernel will just do the memmove for us (but not in as simple a way). */ memmove(return_bytes, CFDataGetBytePtr(returnData), return_len); } } replymsg = __CFMessagePortCreateMessage(allocator, true, msgp->head.msgh_remote_port, MACH_PORT_NULL, -1 * (int32_t)msgp->head.msgh_id, msgid, return_bytes, return_len); if (1 == replymsg->body.msgh_descriptor_count) { replymsg->contents.msg1.desc.out_of_line.deallocate = true; } if (data) CFRelease(data); if (1 == msgp->body.msgh_descriptor_count) { vm_deallocate(mach_task_self(), (vm_address_t)msgp->contents.msg1.desc.out_of_line.address, msgp->contents.msg1.desc.out_of_line.size); } if (returnData) CFRelease(returnData); if (context_release) { context_release(context_info); } return replymsg; }
UInt32 littleEndianUInt32(FILE *stream) { UInt32 n; fread(&n, 4, 1, stream); return CFSwapInt32LittleToHost(n); }