Permalink
Browse files

Use dispatch-aware ARC when targeting 6.0+/10.8+

Dispatch objects are refcounted automatically. Flatten PTChannel's 'channel'
and 'source' objects (can't be in a union under ARC) — adds a word. Replace
calls to deprecated dispatch_get_current_queue (might make sense to modify a
few APIs to take an queue as a parameter and eliminate the funkiness, or
maintain a private serial queue?). Clean up a few warnings.
  • Loading branch information...
Loren Brichter
Loren Brichter committed May 13, 2013
1 parent a454694 commit a248c6a158343ea1e3b7107e38579f0edab8065b
Showing with 129 additions and 47 deletions.
  1. +4 −0 peertalk.xcodeproj/project.pbxproj
  2. +44 −35 peertalk/PTChannel.m
  3. +12 −0 peertalk/PTPrivate.h
  4. +53 −6 peertalk/PTProtocol.m
  5. +16 −6 peertalk/PTUSBHub.m
@@ -7,6 +7,7 @@
objects = {
/* Begin PBXBuildFile section */
+ 5E2C5024171F46A6008A9752 /* PTPrivate.h in Headers */ = {isa = PBXBuildFile; fileRef = 5E2C5023171F46A6008A9752 /* PTPrivate.h */; };
6A2E8E351527411E001B90E4 /* Default@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E8E341527411E001B90E4 /* Default@2x.png */; };
6A2E8E371527451D001B90E4 /* Icon-144@2x.png in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E8E361527451D001B90E4 /* Icon-144@2x.png */; };
6A2E8E39152745EF001B90E4 /* Icon-72.png in Resources */ = {isa = PBXBuildFile; fileRef = 6A2E8E38152745EF001B90E4 /* Icon-72.png */; };
@@ -63,6 +64,7 @@
/* End PBXContainerItemProxy section */
/* Begin PBXFileReference section */
+ 5E2C5023171F46A6008A9752 /* PTPrivate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PTPrivate.h; sourceTree = "<group>"; };
6A0A056E150DB1C10014D424 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
6A2E8E341527411E001B90E4 /* Default@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Default@2x.png"; sourceTree = "<group>"; };
6A2E8E361527451D001B90E4 /* Icon-144@2x.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "Icon-144@2x.png"; sourceTree = "<group>"; };
@@ -260,6 +262,7 @@
isa = PBXGroup;
children = (
6A4010D515275E3800EF0E92 /* prefix.pch */,
+ 5E2C5023171F46A6008A9752 /* PTPrivate.h */,
6ACFD2D4151D36220081ACF5 /* PTChannel.h */,
6ACFD2D5151D36220081ACF5 /* PTChannel.m */,
6A88FA5C150D61DE00FC3647 /* PTProtocol.h */,
@@ -300,6 +303,7 @@
6A88FA60150D61DE00FC3647 /* PTProtocol.h in Headers */,
6A88FA62150D61DE00FC3647 /* PTUSBHub.h in Headers */,
6ACFD2D6151D36220081ACF5 /* PTChannel.h in Headers */,
+ 5E2C5024171F46A6008A9752 /* PTPrivate.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
};
View
@@ -1,4 +1,5 @@
#import "PTChannel.h"
+#import "PTPrivate.h"
#include <sys/ioctl.h>
#include <sys/un.h>
@@ -31,10 +32,8 @@
// Note: We are careful about the size of this struct as each connected peer
// implies one allocation of this struct.
@interface PTChannel () {
- union dispatchObj {
- dispatch_io_t channel;
- dispatch_source_t source;
- } dispatchObj_; // 64 bit
+ dispatch_io_t dispatchObj_channel_;
+ dispatch_source_t dispatchObj_source_;
NSError *endError_; // 64 bit
@public // here be hacks
id<PTChannelDelegate> delegate_; // 64 bit
@@ -67,7 +66,7 @@ @implementation PTChannel
+ (PTChannel*)channelWithDelegate:(id<PTChannelDelegate>)delegate {
- return [[PTChannel alloc] initWithProtocol:[PTProtocol sharedProtocolForQueue:dispatch_get_current_queue()] delegate:delegate];
+ return [[PTChannel alloc] initWithProtocol:[PTProtocol sharedProtocolForQueue:dispatch_get_main_queue()] delegate:delegate];
}
@@ -87,13 +86,15 @@ - (id)initWithProtocol:(PTProtocol*)protocol {
- (id)init {
- return [self initWithProtocol:[PTProtocol sharedProtocolForQueue:dispatch_get_current_queue()]];
+ return [self initWithProtocol:[PTProtocol sharedProtocolForQueue:dispatch_get_main_queue()]];
}
- (void)dealloc {
- if (dispatchObj_.channel) dispatch_release(dispatchObj_.channel);
- else if (dispatchObj_.source) dispatch_release(dispatchObj_.source);
+#if PT_DISPATCH_RETAIN_RELEASE
+ if (dispatchObj_channel_) dispatch_release(dispatchObj_channel_);
+ else if (dispatchObj_source_) dispatch_release(dispatchObj_source_);
+#endif
}
@@ -123,12 +124,14 @@ - (void)setConnState:(char)connState {
- (void)setDispatchChannel:(dispatch_io_t)channel {
assert(connState_ == kConnStateConnecting || connState_ == kConnStateConnected || connState_ == kConnStateNone);
- dispatch_io_t prevChannel = dispatchObj_.channel;
+ dispatch_io_t prevChannel = dispatchObj_channel_;
if (prevChannel != channel) {
- dispatchObj_.channel = channel;
- if (dispatchObj_.channel) dispatch_retain(dispatchObj_.channel);
+ dispatchObj_channel_ = channel;
+#if PT_DISPATCH_RETAIN_RELEASE
+ if (dispatchObj_channel_) dispatch_retain(dispatchObj_channel_);
if (prevChannel) dispatch_release(prevChannel);
- if (!dispatchObj_.channel && !dispatchObj_.source) {
+#endif
+ if (!dispatchObj_channel_ && !dispatchObj_source_) {
connState_ = kConnStateNone;
}
}
@@ -137,12 +140,14 @@ - (void)setDispatchChannel:(dispatch_io_t)channel {
- (void)setDispatchSource:(dispatch_source_t)source {
assert(connState_ == kConnStateListening || connState_ == kConnStateNone);
- dispatch_source_t prevSource = dispatchObj_.source;
+ dispatch_source_t prevSource = dispatchObj_source_;
if (prevSource != source) {
- dispatchObj_.source = source;
- if (dispatchObj_.source) dispatch_retain(dispatchObj_.source);
+ dispatchObj_source_ = source;
+#if PT_DISPATCH_RETAIN_RELEASE
+ if (dispatchObj_source_) dispatch_retain(dispatchObj_source_);
if (prevSource) dispatch_release(prevSource);
- if (!dispatchObj_.channel && !dispatchObj_.source) {
+#endif
+ if (!dispatchObj_channel_ && !dispatchObj_source_) {
connState_ = kConnStateNone;
}
}
@@ -292,7 +297,7 @@ - (void)listenOnPort:(in_port_t)port IPv4Address:(in_addr_t)address callback:(vo
return;
}
- assert(dispatchObj_.source == nil);
+ assert(dispatchObj_source_ == nil);
// Create socket
dispatch_fd_t fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -341,22 +346,22 @@ - (void)listenOnPort:(in_port_t)port IPv4Address:(in_addr_t)address callback:(vo
[self setDispatchSource:dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, fd, 0, protocol_.queue)];
- dispatch_source_set_event_handler(dispatchObj_.source, ^{
- unsigned long nconns = dispatch_source_get_data(dispatchObj_.source);
+ dispatch_source_set_event_handler(dispatchObj_source_, ^{
+ unsigned long nconns = dispatch_source_get_data(dispatchObj_source_);
while ([self acceptIncomingConnection:fd] && --nconns);
});
- dispatch_source_set_cancel_handler(dispatchObj_.source, ^{
+ dispatch_source_set_cancel_handler(dispatchObj_source_, ^{
// Captures *self*, effectively holding a reference to *self* until cancelled.
- dispatchObj_.source = nil;
+ dispatchObj_source_ = nil;
close(fd);
if (delegateFlags_ & kDelegateFlagImplements_ioFrameChannel_didEndWithError) {
[delegate_ ioFrameChannel:self didEndWithError:endError_];
endError_ = nil;
}
});
- dispatch_resume(dispatchObj_.source);
+ dispatch_resume(dispatchObj_source_);
//NSLog(@"%@ opened on fd #%d", self, fd);
connState_ = kConnStateListening;
@@ -420,21 +425,21 @@ - (BOOL)acceptIncomingConnection:(dispatch_fd_t)serverSocketFD {
- (void)close {
- if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_.channel) {
- dispatch_io_close(dispatchObj_.channel, DISPATCH_IO_STOP);
+ if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_channel_) {
+ dispatch_io_close(dispatchObj_channel_, DISPATCH_IO_STOP);
[self setDispatchChannel:NULL];
- } else if (connState_ == kConnStateListening && dispatchObj_.source) {
- dispatch_source_cancel(dispatchObj_.source);
+ } else if (connState_ == kConnStateListening && dispatchObj_source_) {
+ dispatch_source_cancel(dispatchObj_source_);
}
}
- (void)cancel {
- if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_.channel) {
- dispatch_io_close(dispatchObj_.channel, 0);
+ if ((connState_ == kConnStateConnecting || connState_ == kConnStateConnected) && dispatchObj_channel_) {
+ dispatch_io_close(dispatchObj_channel_, 0);
[self setDispatchChannel:NULL];
- } else if (connState_ == kConnStateListening && dispatchObj_.source) {
- dispatch_source_cancel(dispatchObj_.source);
+ } else if (connState_ == kConnStateListening && dispatchObj_source_) {
+ dispatch_source_cancel(dispatchObj_source_);
}
}
@@ -448,7 +453,7 @@ - (BOOL)startReadingFromConnectedChannel:(dispatch_io_t)channel error:(__autorel
return NO;
}
- if (dispatchObj_.channel != channel) {
+ if (dispatchObj_channel_ != channel) {
[self close];
[self setDispatchChannel:channel];
}
@@ -474,7 +479,7 @@ - (BOOL)startReadingFromConnectedChannel:(dispatch_io_t)channel error:(__autorel
return;
}
- BOOL accepted = (channel == dispatchObj_.channel);
+ BOOL accepted = (channel == dispatchObj_channel_);
if (accepted && (delegateFlags_ & kDelegateFlagImplements_ioFrameChannel_shouldAcceptFrameOfType_tag_payloadSize)) {
accepted = [delegate_ ioFrameChannel:self shouldAcceptFrameOfType:type tag:tag payloadSize:payloadSize];
}
@@ -520,7 +525,7 @@ - (BOOL)startReadingFromConnectedChannel:(dispatch_io_t)channel error:(__autorel
- (void)sendFrameOfType:(uint32_t)frameType tag:(uint32_t)tag withPayload:(dispatch_data_t)payload callback:(void(^)(NSError *error))callback {
if (connState_ == kConnStateConnecting || connState_ == kConnStateConnected) {
- [protocol_ sendFrameOfType:frameType tag:tag withPayload:payload overChannel:dispatchObj_.channel callback:callback];
+ [protocol_ sendFrameOfType:frameType tag:tag withPayload:payload overChannel:dispatchObj_channel_ callback:callback];
} else if (callback) {
callback([NSError errorWithDomain:NSPOSIXErrorDomain code:EPERM userInfo:nil]);
}
@@ -564,7 +569,7 @@ - (NSString*)name {
sin_addr = (const void *)&((const struct sockaddr_in*)&sockaddr_)->sin_addr;
}
char *buf = CFAllocatorAllocate(kCFAllocatorDefault, bufsize+1, 0);
- if (inet_ntop(sockaddr_.ss_family, sin_addr, buf, bufsize-1) == NULL) {
+ if (inet_ntop(sockaddr_.ss_family, sin_addr, buf, (unsigned int)bufsize-1) == NULL) {
CFAllocatorDeallocate(kCFAllocatorDefault, buf);
return nil;
}
@@ -586,7 +591,7 @@ - (NSInteger)port {
- (NSString*)description {
if (sockaddr_.ss_len) {
- return [NSString stringWithFormat:@"%@:%u", self.name, self.port];
+ return [NSString stringWithFormat:@"%@:%u", self.name, (unsigned)self.port];
} else {
return @"(?)";
}
@@ -605,14 +610,18 @@ @implementation PTData
- (id)initWithMappedDispatchData:(dispatch_data_t)mappedContiguousData data:(void*)data length:(size_t)length {
if (!(self = [super init])) return nil;
dispatchData_ = mappedContiguousData;
+#if PT_DISPATCH_RETAIN_RELEASE
if (dispatchData_) dispatch_retain(dispatchData_);
+#endif
data_ = data;
length_ = length;
return self;
}
- (void)dealloc {
+#if PT_DISPATCH_RETAIN_RELEASE
if (dispatchData_) dispatch_release(dispatchData_);
+#endif
data_ = NULL;
length_ = 0;
}
View
@@ -0,0 +1,12 @@
+#if (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && (!defined(__IPHONE_6_0) || __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0)) || \
+ (defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && (!defined(__MAC_10_8) || __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8))
+#define PT_DISPATCH_RETAIN_RELEASE 1
+#endif
+
+#if PT_DISPATCH_RETAIN_RELEASE
+#define PT_PRECISE_LIFETIME
+#define PT_PRECISE_LIFETIME_UNUSED
+#else
+#define PT_PRECISE_LIFETIME __attribute__((objc_precise_lifetime))
+#define PT_PRECISE_LIFETIME_UNUSED __attribute__((objc_precise_lifetime, unused))
+#endif
Oops, something went wrong.

0 comments on commit a248c6a

Please sign in to comment.