From 3d864e369c838b77b662a8e8b190f0bb8819f906 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Tue, 26 Jun 2012 20:44:26 +0200 Subject: [PATCH 1/6] Add .gitignore --- .gitignore | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..30b821f --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +*.mode1 +*.mode1v3 +*.mode2v3 +*.perspective +*.perspectivev3 +*.pbxuser +project.xcworkspace +xcuserdata From 2518ac6f0dc008680d11c84c0d3ac75388ab0f1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Tue, 26 Jun 2012 20:49:29 +0200 Subject: [PATCH 2/6] Remove fallback support to Apple's implementation --- Source/blockimp.c | 19 ------------------- Source/blockimp_private.h | 10 ---------- 2 files changed, 29 deletions(-) diff --git a/Source/blockimp.c b/Source/blockimp.c index 87fe23e..02343b3 100644 --- a/Source/blockimp.c +++ b/Source/blockimp.c @@ -47,12 +47,6 @@ static pl_trampoline_table *blockimp_table = NULL; * */ IMP pl_imp_implementationWithBlock (void *block) { -#if SUPPORT_APPLE_FALLBACK - /* Prefer Apple's implementation */ - if (&imp_implementationWithBlock != NULL) - return imp_implementationWithBlock(block); -#endif - /* Allocate the appropriate trampoline type. */ pl_trampoline *tramp; struct Block_layout *bl = block; @@ -75,13 +69,6 @@ IMP pl_imp_implementationWithBlock (void *block) { * */ void *pl_imp_getBlock(IMP anImp) { -#if SUPPORT_APPLE_FALLBACK - /* Prefer Apple's implementation */ - if (&imp_getBlock != NULL) { - return imp_getBlock(anImp); - } -#endif - /* Fetch the config data and return the block reference. */ void **config = pl_trampoline_data_ptr(anImp); return config[0]; @@ -91,12 +78,6 @@ void *pl_imp_getBlock(IMP anImp) { * */ BOOL pl_imp_removeBlock(IMP anImp) { -#if SUPPORT_APPLE_FALLBACK - /* Prefer Apple's implementation */ - if (&imp_removeBlock != NULL) - return imp_removeBlock(anImp); -#endif - /* Fetch the config data */ void **config = pl_trampoline_data_ptr(anImp); struct Block_layout *bl = config[0]; diff --git a/Source/blockimp_private.h b/Source/blockimp_private.h index a6fa23b..0b2fd5c 100644 --- a/Source/blockimp_private.h +++ b/Source/blockimp_private.h @@ -41,16 +41,6 @@ # error Unknown Architecture #endif -#pragma mark Fallback Support - -// if 1, we attempt to use Apple's official implementations -#define SUPPORT_APPLE_FALLBACK 0 -#if SUPPORT_APPLE_FALLBACK -extern IMP imp_implementationWithBlock(void *block) WEAK_IMPORT_ATTRIBUTE; -extern void *imp_getBlock(IMP anImp) WEAK_IMPORT_ATTRIBUTE; -extern BOOL imp_removeBlock(IMP anImp) WEAK_IMPORT_ATTRIBUTE; -#endif - /* * Block Flags */ From 9457feea83950d787334f72e14c5241fcd2b99a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Tue, 26 Jun 2012 20:57:43 +0200 Subject: [PATCH 3/6] Make PLBlockIMP overwrite default block imps and fall through to system if available. Stole this code from PLWeakCompatibility after @landonf encouraged me to. --- Source/blockimp.c | 35 +++++++++++++++++++++++++++++++++++ Source/blockimp.h | 8 +++++--- Source/blockimp_private.h | 6 +++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Source/blockimp.c b/Source/blockimp.c index 02343b3..fb0e5d3 100644 --- a/Source/blockimp.c +++ b/Source/blockimp.c @@ -31,6 +31,41 @@ #include #include +#include +#include + +// Convenience for falling through to the system implementation. +static BOOL fallthroughEnabled = YES; + +#define NEXT(name, ...) do { \ +static dispatch_once_t fptrOnce; \ +static __typeof__(&name) fptr; \ +dispatch_once(&fptrOnce, ^{ fptr = dlsym(RTLD_NEXT, #name); });\ +if (fallthroughEnabled && fptr != NULL) \ +return fptr(__VA_ARGS__); \ +} while(0) + +void PLBlockIMPSetFallthroughEnabled(BOOL enabled) { + fallthroughEnabled = enabled; +} + +IMP imp_implementationWithBlock(void *block) { + NEXT(imp_implementationWithBlock, block); + + return pl_imp_implementationWithBlock(block); +} + +void *imp_getBlock(IMP anImp) { + NEXT(imp_getBlock, anImp); + + return pl_imp_getBlock(anImp); +} + +BOOL imp_removeBlock(IMP anImp) { + NEXT(imp_removeBlock, anImp); + + return imp_removeBlock(anImp); +} #pragma mark Trampolines diff --git a/Source/blockimp.h b/Source/blockimp.h index 07b86dc..fd0551a 100644 --- a/Source/blockimp.h +++ b/Source/blockimp.h @@ -26,6 +26,8 @@ #include -extern IMP pl_imp_implementationWithBlock(void *block); -extern void *pl_imp_getBlock(IMP anImp); -extern BOOL pl_imp_removeBlock(IMP anImp); \ No newline at end of file +OBJC_EXPORT IMP imp_implementationWithBlock(void *block); +OBJC_EXPORT void *imp_getBlock(IMP anImp); +OBJC_EXPORT BOOL imp_removeBlock(IMP anImp); + +OBJC_EXPORT void PLBlockIMPSetFallthroughEnabled(BOOL enabled); \ No newline at end of file diff --git a/Source/blockimp_private.h b/Source/blockimp_private.h index 0b2fd5c..ad39099 100644 --- a/Source/blockimp_private.h +++ b/Source/blockimp_private.h @@ -122,4 +122,8 @@ struct Block_layout { struct Block_descriptor *descriptor; // imported variables -}; \ No newline at end of file +}; + +IMP pl_imp_implementationWithBlock(void *block); +void *pl_imp_getBlock(IMP anImp); +BOOL pl_imp_removeBlock(IMP anImp); \ No newline at end of file From d28d922fe5a9c1d965289c971b7df87b75d042e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Tue, 26 Jun 2012 21:08:59 +0200 Subject: [PATCH 4/6] Make tests build --- Source/blockimp_tests.m | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Source/blockimp_tests.m b/Source/blockimp_tests.m index 7e05ed7..9ffbe78 100644 --- a/Source/blockimp_tests.m +++ b/Source/blockimp_tests.m @@ -27,6 +27,10 @@ #import "GTMSenTestCase.h" #import "blockimp.h" +IMP pl_imp_implementationWithBlock (void *block); +void *pl_imp_getBlock(IMP anImp); +BOOL pl_imp_removeBlock(IMP anImp); + @interface BlockIMPTests : SenTestCase @end /** From 600bbb4bc3ffde28ece45f7434df1f2db8dcf321 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Tue, 26 Jun 2012 21:12:30 +0200 Subject: [PATCH 5/6] Build on Xcode 4.3 and 4.5 --- Source/blockimp.c | 4 ++-- Source/blockimp.h | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Source/blockimp.c b/Source/blockimp.c index fb0e5d3..48bb199 100644 --- a/Source/blockimp.c +++ b/Source/blockimp.c @@ -49,13 +49,13 @@ void PLBlockIMPSetFallthroughEnabled(BOOL enabled) { fallthroughEnabled = enabled; } -IMP imp_implementationWithBlock(void *block) { +IMP imp_implementationWithBlock(PLObjectPtr block) { NEXT(imp_implementationWithBlock, block); return pl_imp_implementationWithBlock(block); } -void *imp_getBlock(IMP anImp) { +PLObjectPtr imp_getBlock(IMP anImp) { NEXT(imp_getBlock, anImp); return pl_imp_getBlock(anImp); diff --git a/Source/blockimp.h b/Source/blockimp.h index fd0551a..cccc2b2 100644 --- a/Source/blockimp.h +++ b/Source/blockimp.h @@ -26,8 +26,22 @@ #include -OBJC_EXPORT IMP imp_implementationWithBlock(void *block); -OBJC_EXPORT void *imp_getBlock(IMP anImp); +#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED + #ifdef __MAC_10_8 + typedef id PLObjectPtr; + #else + typedef void *PLObjectPtr; + #endif +#else + #ifdef __IPHONE_6_0 + typedef id PLObjectPtr; + #else + typedef void *PLObjectPtr; + #endif +#endif + +OBJC_EXPORT IMP imp_implementationWithBlock(PLObjectPtr block); +OBJC_EXPORT PLObjectPtr imp_getBlock(IMP anImp); OBJC_EXPORT BOOL imp_removeBlock(IMP anImp); OBJC_EXPORT void PLBlockIMPSetFallthroughEnabled(BOOL enabled); \ No newline at end of file From 2a7b671030cc24baa41b4779f3a3804cd17ec18d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Schu=CC=88rrer?= Date: Wed, 27 Jun 2012 00:35:11 +0200 Subject: [PATCH 6/6] DRY up availability based typedefs --- Source/blockimp.h | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/Source/blockimp.h b/Source/blockimp.h index cccc2b2..0578031 100644 --- a/Source/blockimp.h +++ b/Source/blockimp.h @@ -25,19 +25,12 @@ */ #include +#include -#ifdef __MAC_OS_X_VERSION_MIN_REQUIRED - #ifdef __MAC_10_8 - typedef id PLObjectPtr; - #else - typedef void *PLObjectPtr; - #endif +#if defined (__MAC_10_8) || defined (__IPHONE_6_0) + typedef id PLObjectPtr; #else - #ifdef __IPHONE_6_0 - typedef id PLObjectPtr; - #else - typedef void *PLObjectPtr; - #endif + typedef void *PLObjectPtr; #endif OBJC_EXPORT IMP imp_implementationWithBlock(PLObjectPtr block);