From 1cf93569af696f6c3d8e7b9dcfa60e7d6b50cef9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C5=93ur?= Date: Mon, 25 Apr 2022 19:16:38 +0800 Subject: [PATCH] arc and non-arc compatibility --- VDKQueue.h | 1 - VDKQueue.m | 68 +++++++++++++++++++++++++++++++++++------------------- 2 files changed, 44 insertions(+), 25 deletions(-) diff --git a/VDKQueue.h b/VDKQueue.h index 9856a34..a448d88 100644 --- a/VDKQueue.h +++ b/VDKQueue.h @@ -117,7 +117,6 @@ extern NSString * VDKQueueAccessRevocationNotification; @interface VDKQueue : NSObject { - id _delegate; BOOL _alwaysPostNotifications; // By default, notifications are posted only if there is no delegate set. Set this value to YES to have notes posted even when there is a delegate. @private diff --git a/VDKQueue.m b/VDKQueue.m index f1bc83b..b16a98a 100644 --- a/VDKQueue.m +++ b/VDKQueue.m @@ -20,10 +20,30 @@ // distribution. #import "VDKQueue.h" +#import #import #import #include +// ARC Helpers + +#if ! __has_feature(objc_arc) +#define ARCCompatAutorelease(__obj__) [__obj__ autorelease]; +#define ARCCompatRetain(__obj__) [__obj__ retain]; +#define ARCCompatRelease(__obj__) [__obj__ release]; +#define ARCCompatDealloc(__obj__) [__obj__ dealloc]; +#define ARCCompatAutoreleaseInline(__obj__) [__obj__ autorelease] +#define ARCCompatRetainInline(__obj__) [__obj__ retain] +#define ARCCompatBridge(__type__, __obj__) __obj__ +#else +#define ARCCompatAutorelease(__obj__) +#define ARCCompatRetain(__obj__) +#define ARCCompatRelease(__obj__) +#define ARCCompatDealloc(__obj__) +#define ARCCompatAutoreleaseInline(__obj__) __obj__ +#define ARCCompatRetainInline(__obj__) __obj__ +#define ARCCompatBridge(__type__, __obj__) (__bridge __type__)__obj__ +#endif NSString * VDKQueueRenameNotification = @"VDKQueueFileRenamedNotification"; @@ -70,7 +90,7 @@ - (id) initWithPath:(NSString*)inPath andSubscriptionFlags:(u_int)flags; _watchedFD = open([_path fileSystemRepresentation], O_EVTONLY, 0); if (_watchedFD < 0) { - [self autorelease]; + ARCCompatAutorelease(self) return nil; } _subscriptionFlags = flags; @@ -80,13 +100,13 @@ - (id) initWithPath:(NSString*)inPath andSubscriptionFlags:(u_int)flags; -(void) dealloc { - [_path release]; + ARCCompatRelease(_path) _path = nil; if (_watchedFD >= 0) close(_watchedFD); _watchedFD = -1; - [super dealloc]; + ARCCompatDealloc(super) } @end @@ -128,7 +148,7 @@ - (id) init _coreQueueFD = kqueue(); if (_coreQueueFD == -1) { - [self autorelease]; + ARCCompatAutorelease(self) return nil; } @@ -147,10 +167,10 @@ - (void) dealloc // Do this to close all the open file descriptors for files we're watching [self removeAllPaths]; - [_watchedPathEntries release]; + ARCCompatRelease(_watchedPathEntries) _watchedPathEntries = nil; - [super dealloc]; + ARCCompatDealloc(super) } @@ -172,7 +192,7 @@ - (VDKQueuePathEntry *) addPathToQueue:(NSString *)path notifyingAbout:(u_int)fl // All flags already set? if(([pathEntry subscriptionFlags] & flags) == flags) { - return [[pathEntry retain] autorelease]; + return ARCCompatAutoreleaseInline(ARCCompatRetainInline(pathEntry)); } flags |= [pathEntry subscriptionFlags]; @@ -183,12 +203,12 @@ - (VDKQueuePathEntry *) addPathToQueue:(NSString *)path notifyingAbout:(u_int)fl if (!pathEntry) { - pathEntry = [[[VDKQueuePathEntry alloc] initWithPath:path andSubscriptionFlags:flags] autorelease]; + pathEntry = ARCCompatAutoreleaseInline([[VDKQueuePathEntry alloc] initWithPath:path andSubscriptionFlags:flags]); } if (pathEntry) { - EV_SET(&ev, [pathEntry watchedFD], EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, flags, 0, pathEntry); + EV_SET(&ev, [pathEntry watchedFD], EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, flags, 0, ARCCompatBridge(void*, pathEntry)); [pathEntry setSubscriptionFlags:flags]; @@ -203,7 +223,7 @@ - (VDKQueuePathEntry *) addPathToQueue:(NSString *)path notifyingAbout:(u_int)fl } } - return [[pathEntry retain] autorelease]; + return ARCCompatAutoreleaseInline(ARCCompatRetainInline(pathEntry)); } return nil; @@ -250,10 +270,10 @@ - (void) watcherThread:(id)sender // check here to try to eliminate this (infrequent) problem. In theory, a KEVENT that does not have a VDKQueuePathEntry // object attached as the udata parameter is not an event we registered for, so we should not be "missing" any events. In theory. // - id pe = ev.udata; + id pe = ARCCompatBridge(id, ev.udata); if (pe && [pe respondsToSelector:@selector(path)]) { - NSString *fpath = [((VDKQueuePathEntry *)pe).path retain]; // Need to retain so it does not disappear while the block at the bottom is waiting to run on the main thread. Released in that block. + NSString *fpath = ARCCompatRetainInline(((VDKQueuePathEntry *)pe).path); // Need to retain so it does not disappear while the block at the bottom is waiting to run on the main thread. Released in that block. if (!fpath) continue; [[NSWorkspace sharedWorkspace] noteFileSystemChanged:fpath]; @@ -300,18 +320,18 @@ - (void) watcherThread:(id)sender ^{ for (NSString *note in notes) { - [_delegate VDKQueue:self receivedNotification:note forPath:fpath]; + [self->_delegate VDKQueue:self receivedNotification:note forPath:fpath]; - if (!_delegate || _alwaysPostNotifications) + if (!self->_delegate || self->_alwaysPostNotifications) { NSDictionary *userInfoDict = [[NSDictionary alloc] initWithObjectsAndKeys:fpath, @"path", nil]; [[[NSWorkspace sharedWorkspace] notificationCenter] postNotificationName:note object:self userInfo:userInfoDict]; - [userInfoDict release]; + ARCCompatRelease(userInfoDict) } } - [fpath release]; - [notes release]; + ARCCompatRelease(fpath) + ARCCompatRelease(notes) }); } } @@ -330,7 +350,7 @@ - (void) watcherThread:(id)sender NSLog(@"VDKQueue watcherThread: Couldn't close main kqueue (%d)", errno); } - [notesToPost release]; + ARCCompatRelease(notesToPost) #if DEBUG_LOG_THREAD_LIFETIME NSLog(@"watcherThread finished."); @@ -351,7 +371,7 @@ - (void) watcherThread:(id)sender - (void) addPath:(NSString *)aPath { if (!aPath) return; - [aPath retain]; + ARCCompatRetain(aPath) @synchronized(self) { @@ -367,14 +387,14 @@ - (void) addPath:(NSString *)aPath } } - [aPath release]; + ARCCompatRelease(aPath) } - (void) addPath:(NSString *)aPath notifyingAbout:(u_int)flags { if (!aPath) return; - [aPath retain]; + ARCCompatRetain(aPath) @synchronized(self) { @@ -390,14 +410,14 @@ - (void) addPath:(NSString *)aPath notifyingAbout:(u_int)flags } } - [aPath release]; + ARCCompatRelease(aPath) } - (void) removePath:(NSString *)aPath { if (!aPath) return; - [aPath retain]; + ARCCompatRetain(aPath) @synchronized(self) { @@ -409,7 +429,7 @@ - (void) removePath:(NSString *)aPath } } - [aPath release]; + ARCCompatRelease(aPath) }