Skip to content

Commit

Permalink
Offer an objc_enumerationMutation hook for platforms without weak imp…
Browse files Browse the repository at this point in the history
…orts.
  • Loading branch information
DHowett committed Jan 22, 2018
1 parent 8c8534e commit 6e4fa86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
14 changes: 11 additions & 3 deletions mutation.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@
#include <stdlib.h>
#include "objc/runtime.h"

// This function is exported as a weak symbol to enable GNUstep or some other
// framework to replace it trivially
void __attribute__((weak)) objc_enumerationMutation(id obj)
static void objc_enumeration_mutation_fallback(id object)
{
fprintf(stderr, "Mutation occured during enumeration.");
abort();
}

void (*_objc_enumeration_mutation)(id object) = &objc_enumeration_mutation_fallback;

// This function is exported as a weak symbol to enable GNUstep or some other
// framework to replace it trivially. On platforms with linkers that cannot handle weak exports,
// the objc_enumeration_mutation hook is the preferred override point.
void __attribute__((weak)) objc_enumerationMutation(id obj)
{
_objc_enumeration_mutation(obj);
}

5 changes: 5 additions & 0 deletions objc/hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ typedef IMP (*objc_tracing_hook)(id, SEL, IMP, int, void*);
*/
int objc_registerTracingHook(SEL, objc_tracing_hook);

/**
* Hook called when a collection is mutated while it is being enumerated.
*/
OBJC_HOOK void(*_objc_enumeration_mutation)(id object);

#ifdef __cplusplus
}
#endif

0 comments on commit 6e4fa86

Please sign in to comment.