Do not call handler blocks if they are nil

This commit is contained in:
Levin Li 2021-11-17 21:35:49 +08:00 committed by Levin Li
parent e68b97d58b
commit 5b151c5fa0
12 changed files with 32 additions and 33 deletions

View file

@ -48,12 +48,12 @@ typedef retTy(^name)()
/**
* Calls a block. Works irrespective of whether the compiler supports blocks.
*/
#define CALL_BLOCK(block, args, ...) block(args, ## __VA_ARGS__)
#define CALL_NON_NULL_BLOCK(block, args, ...) block(args, ## __VA_ARGS__)
/**
* Calls a block without arguments.
*/
#define CALL_BLOCK_NO_ARGS(block) block()
#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block()
#else
/* Fall-back versions for when the compiler doesn't have native blocks support.
@ -76,9 +76,9 @@ typedef retTy(^name)()
retTy (*invoke)(void*);\
} *name
#define CALL_BLOCK(block, args, ...) block->invoke(block, args, ## __VA_ARGS__)
#define CALL_NON_NULL_BLOCK(block, args, ...) block->invoke(block, args, ## __VA_ARGS__)
#define CALL_BLOCK_NO_ARGS(block) block->invoke(block)
#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block->invoke(block)
#define BLOCK_SCOPE
#else /* GCC_VERSION >= 3000 */
@ -100,13 +100,16 @@ typedef retTy(^name)()
} *name
#define CALL_BLOCK(block, args...) block->invoke(block, args)
#define CALL_BLOCK_NO_ARGS(block) block->invoke(block)
#define CALL_NON_NULL_BLOCK(block, args...) block->invoke(block, args)
#define CALL_NON_NULL_BLOCK_NO_ARGS(block) block->invoke(block)
#define BLOCK_SCOPE
#endif /* GCC_VERSION >= 3000 */
#endif /* __has_feature(blocks) */
#define CALL_BLOCK(block, args...) ((NULL != block) ? CALL_NON_NULL_BLOCK(block, args) : nil)
#define CALL_BLOCK_NO_ARGS(block) ((NULL != block) ? CALL_NON_NULL_BLOCK_NO_ARGS(block) : nil)
#if __has_include(<objc/blocks_runtime.h>)
# include <objc/blocks_runtime.h>
#else

View file

@ -148,7 +148,7 @@ GSCompareUsingDescriptorOrComparator(id first, id second, id descOrComp,
return [(NSSortDescriptor*)descOrComp compareObject: first
toObject: second];
case GSComparisonTypeComparatorBlock:
return CALL_BLOCK(((NSComparator)descOrComp), first, second);
return CALL_NON_NULL_BLOCK(((NSComparator)descOrComp), first, second);
case GSComparisonTypeFunction:
return ((NSInteger (*)(id, id, void *))descOrComp)(first,

View file

@ -1146,7 +1146,7 @@ compare(id elem1, id elem2, void* context)
}
if (range.length == 1)
{
switch (CALL_BLOCK(comparator, key, [self objectAtIndex: range.location]))
switch (CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: range.location]))
{
case NSOrderedSame:
return range.location;
@ -1212,7 +1212,7 @@ compare(id elem1, id elem2, void* context)
* For a search from the left, we'd have the correct index anyways. Check
* whether it's equal to the key and return NSNotFound otherwise
*/
return (NSOrderedSame == CALL_BLOCK(comparator,
return (NSOrderedSame == CALL_NON_NULL_BLOCK(comparator,
key, [self objectAtIndex: index]) ? index : NSNotFound);
}
// Never reached
@ -1858,10 +1858,10 @@ compare(id elem1, id elem2, void* context)
}
else // call block directly
# endif
if (CALL_BLOCK(predicate, obj, count, &shouldStop))
if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop))
{
/* TODO: It would be more efficient to collect an NSRange and only
* pass it to the index set when CALL_BLOCK returned NO. */
* pass it to the index set when CALL_NON_NULL_BLOCK returned NO. */
[set addIndex: count];
}
if (shouldStop)
@ -1959,7 +1959,7 @@ compare(id elem1, id elem2, void* context)
}
else // call block directly
# endif
if (CALL_BLOCK(predicate, obj, count, &shouldStop))
if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop))
{
index = count;
shouldStop = YES;

View file

@ -3579,7 +3579,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
{
if (deallocator != NULL)
{
CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, length);
CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, length);
DESTROY(deallocator);
}
// Clear out the ivars so that super doesn't double free.
@ -4442,7 +4442,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
{
if (deallocator != NULL)
{
CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
// Clear out the ivars so that super doesn't double free.
bytes = NULL;
length = 0;
@ -4472,7 +4472,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
memcpy(tmp, bytes, capacity < size ? capacity : size);
if (deallocator != NULL)
{
CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
DESTROY(deallocator);
zone = NSDefaultMallocZone();
}
@ -4483,7 +4483,7 @@ getBytes(void* dst, void* src, unsigned len, unsigned limit, unsigned *pos)
}
else if (deallocator != NULL)
{
CALL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
CALL_NON_NULL_BLOCK(((GSDataDeallocatorBlock)deallocator), bytes, capacity);
DESTROY(deallocator);
zone = NSDefaultMallocZone();
}

View file

@ -1071,7 +1071,7 @@ compareIt(id o1, id o2, void* context)
}
else // call block directly
#endif
if (CALL_BLOCK(aPredicate, key, obj, &shouldStop))
if (CALL_NON_NULL_BLOCK(aPredicate, key, obj, &shouldStop))
{
addObject(buildSet, addObjectSelector, key);
}

View file

@ -2900,7 +2900,7 @@ static inline void gsedRelease(GSEnumeratedDirectory X)
_currentFilePath, [NSError _last]);
if (_errorHandler != NULL)
{
flag = CALL_BLOCK(_errorHandler,
flag = CALL_NON_NULL_BLOCK(_errorHandler,
[NSURL fileURLWithPath: _currentFilePath],
[NSError _last]);
}

View file

@ -526,11 +526,7 @@ static NSArray *empty = nil;
internal->finished = YES;
[self didChangeValueForKey: @"isFinished"];
}
if (NULL != internal->completionBlock)
{
CALL_BLOCK_NO_ARGS(
((GSOperationCompletionBlock)internal->completionBlock));
}
CALL_BLOCK_NO_ARGS(((GSOperationCompletionBlock)internal->completionBlock));
}
[internal->lock unlock];
}
@ -584,7 +580,7 @@ static NSArray *empty = nil;
while ((theBlock = (GSBlockOperationBlock)[en nextObject]) != NULL)
{
CALL_BLOCK_NO_ARGS(theBlock);
CALL_NON_NULL_BLOCK_NO_ARGS(theBlock);
}
}
@end

View file

@ -751,7 +751,7 @@ static SEL remSel;
}
if (range.length == 1)
{
switch (CALL_BLOCK(comparator, key, [self objectAtIndex: range.location]))
switch (CALL_NON_NULL_BLOCK(comparator, key, [self objectAtIndex: range.location]))
{
case NSOrderedSame:
return range.location;
@ -818,7 +818,7 @@ static SEL remSel;
* For a search from the left, we'd have the correct index anyways. Check
* whether it's equal to the key and return NSNotFound otherwise
*/
return (NSOrderedSame == CALL_BLOCK(comparator,
return (NSOrderedSame == CALL_NON_NULL_BLOCK(comparator,
key, [self objectAtIndex: index]) ? index : NSNotFound);
}
// Never reached
@ -886,7 +886,7 @@ static SEL remSel;
}
else // call block directly
# endif
if (CALL_BLOCK(predicate, obj, count, &shouldStop))
if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop))
{
index = count;
shouldStop = YES;
@ -959,10 +959,10 @@ static SEL remSel;
}
else // call block directly
# endif
if (CALL_BLOCK(predicate, obj, count, &shouldStop))
if (CALL_NON_NULL_BLOCK(predicate, obj, count, &shouldStop))
{
/* TODO: It would be more efficient to collect an NSRange and only
* pass it to the index set when CALL_BLOCK returned NO. */
* pass it to the index set when CALL_NON_NULL_BLOCK returned NO. */
[set addIndex: count];
}
if (shouldStop)

View file

@ -2718,7 +2718,7 @@ GSICUStringMatchesRegex(NSString *string, NSString *regex, NSStringCompareOption
substitutionVariables: (GS_GENERIC_CLASS(NSDictionary,
NSString*,id)*)variables
{
return CALL_BLOCK(_block, object, variables);
return CALL_NON_NULL_BLOCK(_block, object, variables);
}
- (BOOL) evaluateWithObject: (id)object

View file

@ -933,7 +933,7 @@ static Class NSMutableSet_concrete_class;
FOR_IN (id, obj, enumerator)
{
BOOL include = CALL_BLOCK(aBlock, obj, &shouldStop);
BOOL include = CALL_NON_NULL_BLOCK(aBlock, obj, &shouldStop);
if (include)
{

View file

@ -120,7 +120,7 @@ static BOOL initialized = NO;
}
else
{
result = CALL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2);
result = CALL_NON_NULL_BLOCK(((NSComparator)_comparator), comparedKey1, comparedKey2);
}
if (_ascending == NO)

View file

@ -290,7 +290,7 @@ static Class NSDate_class;
{
if ((id)_block != nil)
{
CALL_BLOCK(_block, self);
CALL_NON_NULL_BLOCK(_block, self);
}
else
{