__private_extern__ void __CFAllocatorInitialize(void) { __kCFAllocatorTypeID = _CFRuntimeRegisterClass(&__CFAllocatorClass); _CFRuntimeSetInstanceTypeID(&__kCFAllocatorSystemDefault, __kCFAllocatorTypeID); __kCFAllocatorSystemDefault._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI __kCFAllocatorSystemDefault._context.info = (kCFUseCollectableAllocator ? objc_collectableZone() : malloc_default_zone()); #endif __kCFAllocatorSystemDefault._allocator = kCFAllocatorSystemDefault; _CFRuntimeSetInstanceTypeID(&__kCFAllocatorMalloc, __kCFAllocatorTypeID); __kCFAllocatorMalloc._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorMalloc._allocator = kCFAllocatorSystemDefault; #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI _CFRuntimeSetInstanceTypeID(&__kCFAllocatorMallocZone, __kCFAllocatorTypeID); __kCFAllocatorMallocZone._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorMallocZone._allocator = kCFAllocatorSystemDefault; __kCFAllocatorMallocZone._context.info = malloc_default_zone(); #endif _CFRuntimeSetInstanceTypeID(&__kCFAllocatorNull, __kCFAllocatorTypeID); __kCFAllocatorNull._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorNull._allocator = kCFAllocatorSystemDefault; }
CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) { CFAllocatorPreferredSizeCallBack prefFunc; CFIndex newsize = 0; if (kCFAllocatorSystemDefaultGCRefZero == allocator) { allocator = kCFAllocatorSystemDefault; } else if (kCFAllocatorDefaultGCRefZero == allocator) { // Under GC, we can't use just any old allocator when the GCRefZero allocator was requested allocator = kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : __CFGetDefaultAllocator(); } else if (NULL == allocator) { allocator = __CFGetDefaultAllocator(); } #if defined(DEBUG) && (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_good_size(size); } #endif prefFunc = __CFAllocatorGetPreferredSizeFunction(&allocator->_context); if (0 < size && NULL != prefFunc) { newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info)); } if (newsize < size) newsize = size; return newsize; }
void CFAllocatorGetContext(CFAllocatorRef allocator, CFAllocatorContext *context) { allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; #if (DEPLOYMENT_TARGET_MACOSX) && defined(DEBUG) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif CFAssert1(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__); #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return; } #endif context->version = 0; context->info = allocator->_context.info; context->retain = __CFAllocatorGetRetainFunction(&allocator->_context); context->release = __CFAllocatorGetReleaseFunction(&allocator->_context); context->copyDescription = __CFAllocatorGetCopyDescriptionFunction(&allocator->_context); context->allocate = __CFAllocatorGetAllocateFunction(&allocator->_context); context->reallocate = __CFAllocatorGetReallocateFunction(&allocator->_context); context->deallocate = __CFAllocatorGetDeallocateFunction(&allocator->_context); context->preferredSize = __CFAllocatorGetPreferredSizeFunction(&allocator->_context); #if (DEPLOYMENT_TARGET_MACOSX) && defined(__ppc__) context->retain = (void *)((uintptr_t)context->retain & ~0x3); context->release = (void *)((uintptr_t)context->release & ~0x3); context->copyDescription = (void *)((uintptr_t)context->copyDescription & ~0x3); context->allocate = (void *)((uintptr_t)context->allocate & ~0x3); context->reallocate = (void *)((uintptr_t)context->reallocate & ~0x3); context->deallocate = (void *)((uintptr_t)context->deallocate & ~0x3); context->preferredSize = (void *)((uintptr_t)context->preferredSize & ~0x3); #endif }
void CFAllocatorGetContext(CFAllocatorRef allocator, CFAllocatorContext *context) { if (kCFAllocatorSystemDefaultGCRefZero == allocator) { allocator = kCFAllocatorSystemDefault; } else if (kCFAllocatorDefaultGCRefZero == allocator) { // Under GC, we can't use just any old allocator when the GCRefZero allocator was requested allocator = kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : __CFGetDefaultAllocator(); } else if (NULL == allocator) { allocator = __CFGetDefaultAllocator(); } #if defined(DEBUG) && (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif CFAssert1(0 == context->version, __kCFLogAssertion, "%s(): context version not initialized to 0", __PRETTY_FUNCTION__); #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return; } #endif context->version = 0; context->info = allocator->_context.info; context->retain = __CFAllocatorGetRetainFunction(&allocator->_context); context->release = __CFAllocatorGetReleaseFunction(&allocator->_context); context->copyDescription = __CFAllocatorGetCopyDescriptionFunction(&allocator->_context); context->allocate = __CFAllocatorGetAllocateFunction(&allocator->_context); context->reallocate = __CFAllocatorGetReallocateFunction(&allocator->_context); context->deallocate = __CFAllocatorGetDeallocateFunction(&allocator->_context); context->preferredSize = __CFAllocatorGetPreferredSizeFunction(&allocator->_context); }
void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) { CFAllocatorAllocateCallBack allocateFunc; void *newptr = NULL; allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; #if (DEPLOYMENT_TARGET_MACOSX) && defined(DEBUG) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif if (0 == size) return NULL; #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_malloc((malloc_zone_t *)allocator, size); } #endif if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) { newptr = auto_zone_allocate_object((auto_zone_t*)allocator->_context.info, size, CF_GET_GC_MEMORY_TYPE(hint), true, false); } else { newptr = NULL; allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context); if (allocateFunc) { newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info); } } return newptr; }
void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) { CFAllocatorDeallocateCallBack deallocateFunc; if (kCFAllocatorSystemDefaultGCRefZero == allocator) { if (_CFAllocatorIsGCRefZero(allocator)) return; allocator = kCFAllocatorSystemDefault; } else if (kCFAllocatorDefaultGCRefZero == allocator) { // Under GC, we can't use just any old allocator when the GCRefZero allocator was requested allocator = kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : __CFGetDefaultAllocator(); if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) return; } else if (NULL == allocator) { allocator = __CFGetDefaultAllocator(); } #if defined(DEBUG) && (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * #if defined(DEBUG) size_t size = malloc_size(ptr); if (size) memset(ptr, 0xCC, size); #endif return malloc_zone_free((malloc_zone_t *)allocator, ptr); } #endif deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context); if (NULL != ptr && NULL != deallocateFunc) { INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info); } }
__private_extern__ void __CFAllocatorInitialize(void) { __kCFAllocatorTypeID = _CFRuntimeRegisterClass(&__CFAllocatorClass); _CFRuntimeSetInstanceTypeID(&__kCFAllocatorSystemDefault, __kCFAllocatorTypeID); __kCFAllocatorSystemDefault._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); #if DEPLOYMENT_TARGET_MACOSX __kCFAllocatorSystemDefault._context.info = (CF_USING_COLLECTABLE_MEMORY ? __CFCollectableZone : malloc_default_zone()); memset(malloc_default_zone(), 0, 2 * sizeof(void *)); #endif __kCFAllocatorSystemDefault._allocator = kCFAllocatorSystemDefault; #ifdef DEPLOYMENT_TARGET_WINDOWS __kCFAllocatorSystemDefault._context.allocate = __CFAllocatorSystemAllocate; __kCFAllocatorSystemDefault._context.reallocate = __CFAllocatorSystemReallocate; __kCFAllocatorSystemDefault._context.deallocate = __CFAllocatorSystemDeallocate; #endif // DEPLOYMENT_TARGET_WINDOWS _CFRuntimeSetInstanceTypeID(&__kCFAllocatorMalloc, __kCFAllocatorTypeID); __kCFAllocatorMalloc._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorMalloc._allocator = kCFAllocatorSystemDefault; #if DEPLOYMENT_TARGET_MACOSX _CFRuntimeSetInstanceTypeID(&__kCFAllocatorMallocZone, __kCFAllocatorTypeID); __kCFAllocatorMallocZone._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorMallocZone._allocator = kCFAllocatorSystemDefault; __kCFAllocatorMallocZone._context.info = malloc_default_zone(); #endif //__MACH__ _CFRuntimeSetInstanceTypeID(&__kCFAllocatorNull, __kCFAllocatorTypeID); __kCFAllocatorNull._base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); __kCFAllocatorNull._allocator = kCFAllocatorSystemDefault; }
void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize, CFOptionFlags hint) { CFAllocatorAllocateCallBack allocateFunc; CFAllocatorReallocateCallBack reallocateFunc; CFAllocatorDeallocateCallBack deallocateFunc; void *newptr; allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; #if (DEPLOYMENT_TARGET_MACOSX) && defined(DEBUG) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif if (NULL == ptr && 0 < newsize) { #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_malloc((malloc_zone_t *)allocator, newsize); } #endif newptr = NULL; allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context); if (allocateFunc) { newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info); } return newptr; } if (NULL != ptr && 0 == newsize) { #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * #if defined(DEBUG) size_t size = malloc_size(ptr); if (size) memset(ptr, 0xCC, size); #endif malloc_zone_free((malloc_zone_t *)allocator, ptr); return NULL; } #endif deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context); if (NULL != deallocateFunc) { INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info); } return NULL; } if (NULL == ptr && 0 == newsize) return NULL; #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_realloc((malloc_zone_t *)allocator, ptr, newsize); } #endif reallocateFunc = __CFAllocatorGetReallocateFunction(&allocator->_context); if (NULL == reallocateFunc) return NULL; newptr = (void *)INVOKE_CALLBACK4(reallocateFunc, ptr, newsize, hint, allocator->_context.info); return newptr; }
CFTypeRef _CFRuntimeCreateInstance(CFAllocatorRef allocator, CFTypeID typeID, CFIndex extraBytes, unsigned char *category) { CFRuntimeBase *memory; Boolean usesSystemDefaultAllocator; CFIndex size; CFAssert1(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__); if (NULL == __CFRuntimeClassTable[typeID]) { return NULL; } allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; usesSystemDefaultAllocator = (allocator == kCFAllocatorSystemDefault); size = sizeof(CFRuntimeBase) + extraBytes + (usesSystemDefaultAllocator ? 0 : sizeof(CFAllocatorRef)); size = (size + 0xF) & ~0xF; // CF objects are multiples of 16 in size // CFType version 0 objects are unscanned by default since they don't have write-barriers and hard retain their innards // CFType version 1 objects are scanned and use hand coded write-barriers to store collectable storage within memory = (CFRuntimeBase *)CFAllocatorAllocate(allocator, size, CF_GET_COLLECTABLE_MEMORY_TYPE(__CFRuntimeClassTable[typeID])); if (NULL == memory) { return NULL; } #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX // malloc_size won't work if the memory address has been moved // (such as a custom allocator that adds its own metadata // (e.g. under/overflow guard data), so don't attempt to call it // on the allocation return. size_t msize = (usesSystemDefaultAllocator) ? malloc_size(memory) : size; #else size_t msize = malloc_size(memory); #endif memset(memory, 0, msize); if (__CFOASafe && category) { __CFSetLastAllocationEventName(memory, (char *)category); } else if (__CFOASafe) { __CFSetLastAllocationEventName(memory, (char *)__CFRuntimeClassTable[typeID]->className); } if (!usesSystemDefaultAllocator) { // add space to hold allocator ref for non-standard allocators. // (this screws up 8 byte alignment but seems to work) *(CFAllocatorRef *)((char *)memory) = (CFAllocatorRef)CFRetain(allocator); memory = (CFRuntimeBase *)((char *)memory + sizeof(CFAllocatorRef)); } memory->_cfisa = __CFISAForTypeID(typeID); #if __LP64__ *(uint32_t *)(memory->_cfinfo) = (uint32_t)((0 << 24) + ((typeID & 0xFFFF) << 8) + (usesSystemDefaultAllocator ? 0x80 : 0x00)); memory->_rc = 1; #else *(uint32_t *)(memory->_cfinfo) = (uint32_t)((1 << 24) + ((typeID & 0xFFFF) << 8) + (usesSystemDefaultAllocator ? 0x80 : 0x00)); #endif if (NULL != __CFRuntimeClassTable[typeID]->init) { (__CFRuntimeClassTable[typeID]->init)(memory); } return memory; }
void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) { CFAllocatorAllocateCallBack allocateFunc; void *newptr = NULL; Boolean initialRefcountOne = true; if (kCFAllocatorSystemDefaultGCRefZero == allocator) { allocator = kCFAllocatorSystemDefault; initialRefcountOne = false; } else if (kCFAllocatorDefaultGCRefZero == allocator) { // Under GC, we can't use just any old allocator when the GCRefZero allocator was requested allocator = kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : __CFGetDefaultAllocator(); if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) initialRefcountOne = false; } else if (NULL == allocator) { allocator = __CFGetDefaultAllocator(); } #if defined(DEBUG) && (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif if (0 == size) return NULL; #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_malloc((malloc_zone_t *)allocator, size); } #endif if (CF_IS_COLLECTABLE_ALLOCATOR(allocator)) { newptr = auto_zone_allocate_object((auto_zone_t*)allocator->_context.info, size, CF_GET_GC_MEMORY_TYPE(hint), initialRefcountOne, false); } else { newptr = NULL; allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context); if (allocateFunc) { newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info); } } return newptr; }
CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) { CFAllocatorPreferredSizeCallBack prefFunc; CFIndex newsize = 0; allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; #if (DEPLOYMENT_TARGET_MACOSX) && defined(DEBUG) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_good_size(size); } #endif prefFunc = __CFAllocatorGetPreferredSizeFunction(&allocator->_context); if (0 < size && NULL != prefFunc) { newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info)); } if (newsize < size) newsize = size; return newsize; }
void CFAllocatorDeallocate(CFAllocatorRef allocator, void *ptr) { CFAllocatorDeallocateCallBack deallocateFunc; allocator = (NULL == allocator) ? __CFGetDefaultAllocator() : allocator; #if (DEPLOYMENT_TARGET_MACOSX) && defined(DEBUG) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif #if DEPLOYMENT_TARGET_MACOSX if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * #if defined(DEBUG) size_t size = malloc_size(ptr); if (size) memset(ptr, 0xCC, size); #endif return malloc_zone_free((malloc_zone_t *)allocator, ptr); } #endif deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context); if (NULL != ptr && NULL != deallocateFunc) { INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info); } }
void _CFRuntimeInitStaticInstance(void *ptr, CFTypeID typeID) { CFRuntimeBase *memory = (CFRuntimeBase *)ptr; CFAssert1(typeID != _kCFRuntimeNotATypeID, __kCFLogAssertion, "%s(): Uninitialized type id", __PRETTY_FUNCTION__); if (NULL == __CFRuntimeClassTable[typeID]) { return; } memory->_cfisa = __CFISAForTypeID(typeID); *(uint32_t *)(memory->_cfinfo) = (uint32_t)((0 << 24) + ((typeID & 0xFFFF) << 8) + 0x80); #if __LP64__ memory->_rc = 0; #endif if (NULL != __CFRuntimeClassTable[typeID]->init) { (__CFRuntimeClassTable[typeID]->init)(memory); } }
void CFAllocatorSetDefault(CFAllocatorRef allocator) { CFAllocatorRef current = (CFAllocatorRef)__CFGetThreadSpecificData_inline()->_allocator; #if defined(DEBUG) if (NULL != allocator) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #endif #if DEPLOYMENT_TARGET_MACOSX if (allocator && allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return; // require allocator to this function to be an allocator } #endif if (NULL != allocator && allocator != current) { if (current) CFRelease(current); CFRetain(allocator); // We retain an extra time so that anything set as the default // allocator never goes away. CFRetain(allocator); __CFGetThreadSpecificData_inline()->_allocator = (void *)allocator; } }
void CFAllocatorSetDefault(CFAllocatorRef allocator) { if (kCFAllocatorSystemDefaultGCRefZero == allocator || kCFAllocatorDefaultGCRefZero == allocator) { HALT; } CFAllocatorRef current = __CFGetDefaultAllocator(); #if defined(DEBUG) if (NULL != allocator) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #endif #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator && allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return; // require allocator to this function to be an allocator } #endif if (NULL != allocator && allocator != current) { if (current) CFRelease(current); CFRetain(allocator); // We retain an extra time so that anything set as the default // allocator never goes away. CFRetain(allocator); _CFSetTSD(__CFTSDKeyAllocator, (void *)allocator, NULL); } }
__private_extern__ void __CFNullInitialize(void) { __kCFNullTypeID = _CFRuntimeRegisterClass(&__CFNullClass); _CFRuntimeSetInstanceTypeID(&__kCFNull, __kCFNullTypeID); __kCFNull._base._cfisa = __CFISAForTypeID(__kCFNullTypeID); }
void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize, CFOptionFlags hint) { CFAllocatorAllocateCallBack allocateFunc; CFAllocatorReallocateCallBack reallocateFunc; CFAllocatorDeallocateCallBack deallocateFunc; void *newptr; if (kCFAllocatorSystemDefaultGCRefZero == allocator) { allocator = kCFAllocatorSystemDefault; } else if (kCFAllocatorDefaultGCRefZero == allocator) { // Under GC, we can't use just any old allocator when the GCRefZero allocator was requested allocator = kCFUseCollectableAllocator ? kCFAllocatorSystemDefault : __CFGetDefaultAllocator(); } else if (NULL == allocator) { allocator = __CFGetDefaultAllocator(); } #if defined(DEBUG) && (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI) if (allocator->_base._cfisa == __CFISAForTypeID(__kCFAllocatorTypeID)) { __CFGenericValidateType(allocator, __kCFAllocatorTypeID); } #else __CFGenericValidateType(allocator, __kCFAllocatorTypeID); #endif if (NULL == ptr && 0 < newsize) { #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_malloc((malloc_zone_t *)allocator, newsize); } #endif newptr = NULL; allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context); if (allocateFunc) { newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info); } return newptr; } if (NULL != ptr && 0 == newsize) { #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * #if defined(DEBUG) size_t size = malloc_size(ptr); if (size) memset(ptr, 0xCC, size); #endif malloc_zone_free((malloc_zone_t *)allocator, ptr); return NULL; } #endif deallocateFunc = __CFAllocatorGetDeallocateFunction(&allocator->_context); if (NULL != deallocateFunc) { INVOKE_CALLBACK2(deallocateFunc, ptr, allocator->_context.info); } return NULL; } if (NULL == ptr && 0 == newsize) return NULL; #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI if (allocator->_base._cfisa != __CFISAForTypeID(__kCFAllocatorTypeID)) { // malloc_zone_t * return malloc_zone_realloc((malloc_zone_t *)allocator, ptr, newsize); } #endif reallocateFunc = __CFAllocatorGetReallocateFunction(&allocator->_context); if (NULL == reallocateFunc) return NULL; newptr = (void *)INVOKE_CALLBACK4(reallocateFunc, ptr, newsize, hint, allocator->_context.info); return newptr; }
static void _CFAllocatorSetInstanceTypeIDAndIsa(struct __CFAllocator *memory) { _CFRuntimeSetInstanceTypeID(memory, __kCFAllocatorTypeID); memory->_base._cfisa = __CFISAForTypeID(__kCFAllocatorTypeID); }
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; }