Skip to content

Commit

Permalink
Added calloc/realloc overrides to AERealtimeWatchdog; addressed a cou…
Browse files Browse the repository at this point in the history
…ple of warnings
  • Loading branch information
michaeltyson committed Jun 13, 2016
1 parent 3b84698 commit 4e7105f
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions TheAmazingAudioEngine/Utilities/AERealtimeWatchdog.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@



void AERealtimeWatchdogUnsafeActivityWarning(const char * activity) {
static void AERealtimeWatchdogUnsafeActivityWarning(const char * activity) {
#ifndef REPORT_EVERY_INFRACTION
static BOOL once = NO;
if ( !once ) {
Expand All @@ -56,7 +56,8 @@ void AERealtimeWatchdogUnsafeActivityWarning(const char * activity) {
#endif
}

BOOL AERealtimeWatchdogIsOnRealtimeThread() {
BOOL AERealtimeWatchdogIsOnRealtimeThread(void);
BOOL AERealtimeWatchdogIsOnRealtimeThread(void) {
pthread_t thread = pthread_self();

static pthread_t __audioThread = NULL;
Expand All @@ -82,6 +83,8 @@ BOOL AERealtimeWatchdogIsOnRealtimeThread() {

// Signatures for the functions we'll override
typedef void * (*malloc_t)(size_t);
typedef void * (*calloc_t)(size_t, size_t);
typedef void * (*realloc_t)(void *, size_t);
typedef void (*free_t)(void*);
typedef int (*pthread_mutex_lock_t)(pthread_mutex_t *);
typedef int (*objc_sync_enter_t)(id obj);
Expand Down Expand Up @@ -112,6 +115,20 @@ typedef ssize_t (*recvfrom_t)(int socket, void *restrict buffer, size_t length,
return funcptr(sz);
}

void * calloc(size_t count, size_t size) {
static calloc_t funcptr = NULL;
if ( !funcptr ) funcptr = (calloc_t) dlsym(RTLD_NEXT, "calloc");
if ( AERealtimeWatchdogIsOnRealtimeThread() ) AERealtimeWatchdogUnsafeActivityWarning("calloc");
return funcptr(count, size);
}

void * realloc(void * ptr, size_t size) {
static realloc_t funcptr = NULL;
if ( !funcptr ) funcptr = (realloc_t) dlsym(RTLD_NEXT, "realloc");
if ( AERealtimeWatchdogIsOnRealtimeThread() ) AERealtimeWatchdogUnsafeActivityWarning("realloc");
return funcptr(ptr, size);
}

void free(void *p) {
static free_t funcptr = NULL;
if ( !funcptr ) funcptr = (free_t) dlsym(RTLD_NEXT, "free");
Expand All @@ -133,14 +150,16 @@ int objc_sync_enter(id obj) {
return funcptr(obj);
}

id objc_storeStrong(id * object, id value);
id objc_storeStrong(id * object, id value) {
static objc_storeStrong_t funcptr = NULL;
if ( !funcptr ) funcptr = (objc_storeStrong_t) dlsym(RTLD_NEXT, "objc_storeStrong");
if ( AERealtimeWatchdogIsOnRealtimeThread() ) AERealtimeWatchdogUnsafeActivityWarning("object retain");
return funcptr(object,value);
}

objc_msgSend_t AERealtimeWatchdogLookupMsgSendAndWarn() {
objc_msgSend_t AERealtimeWatchdogLookupMsgSendAndWarn(void);
objc_msgSend_t AERealtimeWatchdogLookupMsgSendAndWarn(void) {
// This method is called by our objc_msgSend implementation
static objc_msgSend_t funcptr = NULL;
if ( !funcptr ) funcptr = (objc_msgSend_t) dlsym(RTLD_NEXT, "objc_msgSend");
Expand Down

0 comments on commit 4e7105f

Please sign in to comment.