Skip to content

Commit b91826a

Browse files
committed
Move the size check to be the first thing checked in CFAllocatorCreate
This saves us a lot of work is size is 0
1 parent 2d23cf3 commit b91826a

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

CoreFoundation/Base.subproj/CFBase.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -581,8 +581,9 @@ CFAllocatorRef CFAllocatorCreate(CFAllocatorRef allocator, CFAllocatorContext *c
581581

582582
void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint) {
583583
CFAllocatorAllocateCallBack allocateFunc;
584-
void *newptr = NULL;
584+
void *newptr;
585585

586+
if (0 < size) {
586587
if (NULL == allocator) {
587588
allocator = __CFGetDefaultAllocator();
588589
}
@@ -594,16 +595,14 @@ void *CFAllocatorAllocate(CFAllocatorRef allocator, CFIndex size, CFOptionFlags
594595
#else
595596
__CFGenericValidateType(allocator, _kCFRuntimeIDCFAllocator);
596597
#endif
597-
if (0 == size) return NULL;
598598
#if TARGET_OS_MAC
599599
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
600600
return malloc_zone_malloc((malloc_zone_t *)allocator, size);
601601
}
602602
#endif
603-
newptr = NULL;
604603
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
605-
if (allocateFunc) {
606-
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
604+
if (NULL == allocateFunc) return NULL;
605+
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, size, hint, allocator->_context.info);
607606
}
608607
return newptr;
609608
}
@@ -631,14 +630,12 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
631630
return malloc_zone_malloc((malloc_zone_t *)allocator, newsize);
632631
}
633632
#endif
634-
newptr = NULL;
635633
allocateFunc = __CFAllocatorGetAllocateFunction(&allocator->_context);
636-
if (allocateFunc) {
637-
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
638-
}
634+
if (NULL == allocateFunc) return NULL;
635+
newptr = (void *)INVOKE_CALLBACK3(allocateFunc, newsize, hint, allocator->_context.info);
639636
return newptr;
640637
}
641-
if (NULL != ptr && 0 == newsize) {
638+
if (NULL != ptr && 0 >= newsize) {
642639
#if TARGET_OS_MAC
643640
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
644641
#if defined(DEBUG)
@@ -655,7 +652,7 @@ void *CFAllocatorReallocate(CFAllocatorRef allocator, void *ptr, CFIndex newsize
655652
}
656653
return NULL;
657654
}
658-
if (NULL == ptr && 0 == newsize) return NULL;
655+
if (NULL == ptr && 0 >= newsize) return NULL;
659656
#if TARGET_OS_MAC
660657
if (_CFTypeGetClass(allocator) != __CFISAForCFAllocator()) { // malloc_zone_t *
661658
return malloc_zone_realloc((malloc_zone_t *)allocator, ptr, newsize);
@@ -700,6 +697,7 @@ CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex siz
700697
CFAllocatorPreferredSizeCallBack prefFunc;
701698
CFIndex newsize = 0;
702699

700+
if (0 < size) {
703701
if (NULL == allocator) {
704702
allocator = __CFGetDefaultAllocator();
705703
}
@@ -717,10 +715,11 @@ CFIndex CFAllocatorGetPreferredSizeForSize(CFAllocatorRef allocator, CFIndex siz
717715
}
718716
#endif
719717
prefFunc = __CFAllocatorGetPreferredSizeFunction(&allocator->_context);
720-
if (0 < size && NULL != prefFunc) {
718+
if (NULL != prefFunc) {
721719
newsize = (CFIndex)(INVOKE_CALLBACK3(prefFunc, size, hint, allocator->_context.info));
722720
}
723721
if (newsize < size) newsize = size;
722+
}
724723
return newsize;
725724
}
726725

0 commit comments

Comments
 (0)