static RSStringRef __RSQueueClassDescription(RSTypeRef rs) { RSQueueRef queue = (RSQueueRef)rs; RSMutableStringRef result; RSAllocatorRef allocator; RSIndex idx, cnt; cnt = RSQueueGetCount(queue); allocator = RSGetAllocator(queue); result = RSStringCreateMutable(allocator, 0); RSStringAppendStringWithFormat(result, RSSTR("<RSQueue %p [%p]>{type = mutable-small, count = %u, values = (%s"), rs, allocator, cnt, cnt ? "\n" : ""); for (idx = 0; idx < cnt; idx++) { RSStringRef desc = nil; RSTypeRef val = nil; desc = (RSStringRef)INVOKE_CALLBACK1(RSDescription, val = RSArrayObjectAtIndex(queue->_queueCore, idx)); if (nil != desc) { RSStringAppendStringWithFormat(result, RSSTR("\t%u : %R\n"), idx, desc); RSRelease(desc); } else { RSStringAppendStringWithFormat(result, RSSTR("\t%u : <%p>\n"), idx, val); } } RSStringAppendString(result, RSSTR(")}")); return result; }
static CFStringRef __CFArrayCopyDescription(CFTypeRef cf) { CFArrayRef array = (CFArrayRef)cf; CFMutableStringRef result; const CFArrayCallBacks *cb; CFAllocatorRef allocator; CFIndex idx, cnt; cnt = __CFArrayGetCount(array); allocator = CFGetAllocator(array); result = CFStringCreateMutable(allocator, 0); switch (__CFArrayGetType(array)) { case __kCFArrayImmutable: CFStringAppendFormat(result, NULL, CFSTR("<CFArray %p [%p]>{type = immutable, count = %lu, values = (%s"), cf, allocator, (unsigned long)cnt, cnt ? "\n" : ""); break; case __kCFArrayDeque: CFStringAppendFormat(result, NULL, CFSTR("<CFArray %p [%p]>{type = mutable-small, count = %lu, values = (%s"), cf, allocator, (unsigned long)cnt, cnt ? "\n" : ""); break; } cb = __CFArrayGetCallBacks(array); for (idx = 0; idx < cnt; idx++) { CFStringRef desc = NULL; const void *val = __CFArrayGetBucketAtIndex(array, idx)->_item; if (NULL != cb->copyDescription) { desc = (CFStringRef)INVOKE_CALLBACK1(cb->copyDescription, val); } if (NULL != desc) { CFStringAppendFormat(result, NULL, CFSTR("\t%lu : %@\n"), (unsigned long)idx, desc); CFRelease(desc); } else { CFStringAppendFormat(result, NULL, CFSTR("\t%lu : <%p>\n"), (unsigned long)idx, val); } } CFStringAppend(result, CFSTR(")}")); return result; }
static void __CFPlugInInstanceDeallocate(CFTypeRef cf) { CFPlugInInstanceRef instance = (CFPlugInInstanceRef)cf; __CFGenericValidateType(cf, __kCFPlugInInstanceTypeID); if (instance->deallocateInstanceDataFunction) { FAULT_CALLBACK((void **)&(instance->deallocateInstanceDataFunction)); (void)INVOKE_CALLBACK1(instance->deallocateInstanceDataFunction, (void *)(&instance->_instanceData[0])); } if (instance->factory) _CFPFactoryRemoveInstance(instance->factory); }
CF_PRIVATE void __CFAllocatorDeallocate(CFTypeRef cf) { CFAllocatorRef self = (CFAllocatorRef)cf; CFAllocatorRef allocator = self->_allocator; CFAllocatorReleaseCallBack releaseFunc = __CFAllocatorGetReleaseFunction(&self->_context); if (kCFAllocatorUseContext == allocator) { /* Rather a chicken and egg problem here, so we do things in the reverse order from what was done at create time. */ CFAllocatorDeallocateCallBack deallocateFunc = __CFAllocatorGetDeallocateFunction(&self->_context); void *info = self->_context.info; if (NULL != deallocateFunc) { INVOKE_CALLBACK2(deallocateFunc, (void *)self, info); } if (NULL != releaseFunc) { INVOKE_CALLBACK1(releaseFunc, info); } } else { if (NULL != releaseFunc) { INVOKE_CALLBACK1(releaseFunc, self->_context.info); } CFAllocatorDeallocate(allocator, (void *)self); } }
void CFTreeSetContext(CFTreeRef tree, const CFTreeContext *context) { uint32_t newtype, oldtype = __CFTreeGetCallBacksType(tree); struct __CFTreeCallBacks *oldcb = (struct __CFTreeCallBacks *)__CFTreeGetCallBacks(tree); struct __CFTreeCallBacks *newcb; void *oldinfo = tree->_info; CFAllocatorRef allocator = CFGetAllocator(tree); if (__CFTreeCallBacksMatchNull(context)) { newtype = __kCFTreeHasNullCallBacks; } else if (__CFTreeCallBacksMatchCFType(context)) { newtype = __kCFTreeHasCFTypeCallBacks; } else { newtype = __kCFTreeHasCustomCallBacks; __CFAssignWithWriteBarrier((void **)&tree->_callbacks, _CFAllocatorAllocateGC(allocator, sizeof(struct __CFTreeCallBacks), 0)); if (__CFOASafe) __CFSetLastAllocationEventName(tree->_callbacks, "CFTree (callbacks)"); tree->_callbacks->retain = context->retain; tree->_callbacks->release = context->release; tree->_callbacks->copyDescription = context->copyDescription; FAULT_CALLBACK((void **)&(tree->_callbacks->retain)); FAULT_CALLBACK((void **)&(tree->_callbacks->release)); FAULT_CALLBACK((void **)&(tree->_callbacks->copyDescription)); } __CFBitfieldSetValue(tree->_base._cfinfo[CF_INFO_BITS], 1, 0, newtype); newcb = (struct __CFTreeCallBacks *)__CFTreeGetCallBacks(tree); if (NULL != newcb->retain) { tree->_info = (void *)INVOKE_CALLBACK1(newcb->retain, context->info); } else { __CFAssignWithWriteBarrier((void **)&tree->_info, context->info); } if (NULL != oldcb->release) { INVOKE_CALLBACK1(oldcb->release, oldinfo); } if (oldtype == __kCFTreeHasCustomCallBacks) { _CFAllocatorDeallocateGC(allocator, oldcb); } }
static void __CFTreeDeallocate(CFTypeRef cf) { CFTreeRef tree = (CFTreeRef)cf; const struct __CFTreeCallBacks *cb; CFAllocatorRef allocator = __CFGetAllocator(tree); if (!CF_IS_COLLECTABLE_ALLOCATOR(allocator)) { // GC: keep the tree intact during finalization. CFTreeRemoveAllChildren(tree); } cb = __CFTreeGetCallBacks(tree); if (NULL != cb->release) { INVOKE_CALLBACK1(cb->release, tree->_info); } if (__kCFTreeHasCustomCallBacks == __CFTreeGetCallBacksType(tree)) { _CFAllocatorDeallocateGC(CFGetAllocator(tree), tree->_callbacks); } }
static CFStringRef __CFTreeCopyDescription(CFTypeRef cf) { CFTreeRef tree = (CFTreeRef)cf; CFMutableStringRef result; CFStringRef contextDesc = NULL; const struct __CFTreeCallBacks *cb; CFAllocatorRef allocator; allocator = CFGetAllocator(tree); result = CFStringCreateMutable(allocator, 0); cb = __CFTreeGetCallBacks(tree); if (NULL != cb->copyDescription) { contextDesc = (CFStringRef)INVOKE_CALLBACK1(cb->copyDescription, tree->_info); } if (NULL == contextDesc) { contextDesc = CFStringCreateWithFormat(allocator, NULL, CFSTR("<CFTree context %p>"), tree->_info); } CFStringAppendFormat(result, NULL, CFSTR("<CFTree %p [%p]>{children = %u, context = %@}"), cf, allocator, CFTreeGetChildCount(tree), contextDesc); if (contextDesc) CFRelease(contextDesc); return result; }
static CFAllocatorRef __CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContext *context) { struct __CFAllocator *memory = NULL; CFAllocatorRetainCallBack retainFunc; CFAllocatorAllocateCallBack allocateFunc; void *retainedInfo; #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator && kCFAllocatorUseContext != allocator && allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return NULL; // require allocator to this function to be an allocator } #endif retainFunc = context->retain; FAULT_CALLBACK((void **)&retainFunc); allocateFunc = context->allocate; FAULT_CALLBACK((void **)&allocateFunc); if (NULL != retainFunc) { retainedInfo = (void *)INVOKE_CALLBACK1(retainFunc, context->info); } else { retainedInfo = context->info; } // We don't use _CFRuntimeCreateInstance() if (kCFAllocatorUseContext == allocator) { memory = NULL; if (allocateFunc) { memory = (struct __CFAllocator *)INVOKE_CALLBACK3(allocateFunc, sizeof(struct __CFAllocator), 0, retainedInfo); } if (NULL == memory) { return NULL; } } else { allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; memory = (struct __CFAllocator *)CFAllocatorAllocate(allocator, sizeof(struct __CFAllocator), __kCFAllocatorGCObjectMemory); if (NULL == memory) { return NULL; } if (__CFOASafe) __CFSetLastAllocationEventName(memory, "CFAllocator"); } memset(memory, 0, sizeof(CFRuntimeBase)); memory->_base._cfisa = 0; #if __LP64__ memory->_base._rc = 1; #else memory->_base._cfinfo[CF_RC_BITS] = 1; #endif memory->_base._cfinfo[CF_INFO_BITS] = 0; _CFRuntimeSetInstanceTypeID(memory, __kCFAllocatorTypeID); memory->_base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI memory->size = __CFAllocatorCustomSize; memory->malloc = __CFAllocatorCustomMalloc; memory->calloc = __CFAllocatorCustomCalloc; memory->valloc = __CFAllocatorCustomValloc; memory->free = __CFAllocatorCustomFree; memory->realloc = __CFAllocatorCustomRealloc; memory->destroy = __CFAllocatorCustomDestroy; memory->zone_name = "Custom CFAllocator"; memory->batch_malloc = NULL; memory->batch_free = NULL; memory->introspect = &__CFAllocatorZoneIntrospect; memory->version = 6; memory->memalign = NULL; memory->free_definite_size = NULL; #endif memory->_allocator = allocator; memory->_context.version = context->version; memory->_context.info = retainedInfo; memory->_context.retain = retainFunc; memory->_context.release = context->release; FAULT_CALLBACK((void **)&(memory->_context.release)); memory->_context.copyDescription = context->copyDescription; FAULT_CALLBACK((void **)&(memory->_context.copyDescription)); memory->_context.allocate = allocateFunc; memory->_context.reallocate = context->reallocate; FAULT_CALLBACK((void **)&(memory->_context.reallocate)); memory->_context.deallocate = context->deallocate; FAULT_CALLBACK((void **)&(memory->_context.deallocate)); memory->_context.preferredSize = context->preferredSize; FAULT_CALLBACK((void **)&(memory->_context.preferredSize)); return memory; }