Skip to content
Permalink
Browse files

* Formatting and minor code changes

* Fixed a memory leak in CF code
  • Loading branch information...
dmoagx committed Apr 29, 2018
1 parent fc3e74c commit 5bd7cde2e69991b5cd9bde424c45e2da5bb34292
@@ -37,6 +37,26 @@
*/
static inline void SPMySQLMutableArrayInsertObject(NSMutableArray *self, id anObject, NSUInteger anIndex)
{
/* A note on CFArrayInsertValueAtIndex():
*
* This function here does look similar to the CF function, however that is not neccesarily the
* case from a performance standpoint.
*
* CFArrayInsertValueAtIndex() is mostly a wrapper around either
* - _CFArrayReplaceValues(obj, …) *or*
* - objc_msgSend(obj, @selector(insertObject:atIndex:), …)
*
* The first case would be fast, but it will only be used if the object is a native CFArrayRef, not
* a toll-free bridged object. In our case however, we always pass in some object of the NSMutableArray cluster,
* so we would always end up in the slowest path (uncached objc method invocation).
*
* Determing the performance of the objc method is more difficult, because there are multiple implementations
* of NSMutableArray and the "real" -[NSMutableArray insertObject:atIndex:] (located in CoreFoundation.framework)
* is only an abstract stub that will raise an exception if called.
*
* NSCFArray's (for CFArrays bridged to objc) implementation will wind up in _CFArrayReplaceValues().
* __NSArrayM's (for +[NSMutableArray array]) implementation is completely independent from the aforementioned ones.
*/
typedef id (*SPMySQLMutableArrayInsertObjectPtr)(NSMutableArray*, SEL, id, NSUInteger);
static SPMySQLMutableArrayInsertObjectPtr cachedMethodPointer;
static SEL cachedSelector;
@@ -946,31 +946,28 @@ - (BOOL)_reconnectAfterBackgroundConnectionLoss
*/
- (BOOL)_waitForNetworkConnectionWithTimeout:(double)timeoutSeconds
{
BOOL hostReachable;
Boolean flagsValid;
SCNetworkReachabilityRef reachabilityTarget;
SCNetworkConnectionFlags reachabilityStatus;

// Set up the reachability target - the host is not important, and is not connected to.
reachabilityTarget = SCNetworkReachabilityCreateWithName(NULL, "dev.mysql.com");
SCNetworkReachabilityRef reachabilityTarget = SCNetworkReachabilityCreateWithName(NULL, "dev.mysql.com");

BOOL hostReachable;
// In a loop until success or the timeout, test reachability
uint64_t loopStart_t = mach_absolute_time();
while (1) {
SCNetworkReachabilityFlags reachabilityStatus;

// Check reachability
flagsValid = SCNetworkReachabilityGetFlags(reachabilityTarget, &reachabilityStatus);
Boolean flagsValid = SCNetworkReachabilityGetFlags(reachabilityTarget, &reachabilityStatus);

hostReachable = flagsValid ? YES : NO;

// Ensure that the network is reachable
if (hostReachable && !(reachabilityStatus & kSCNetworkFlagsReachable)) hostReachable = NO;
if (hostReachable && !(reachabilityStatus & kSCNetworkReachabilityFlagsReachable)) hostReachable = NO;

// Ensure that Airport is up/connected if present
if (hostReachable && (reachabilityStatus & kSCNetworkFlagsConnectionRequired)) hostReachable = NO;
if (hostReachable && (reachabilityStatus & kSCNetworkReachabilityFlagsConnectionRequired)) hostReachable = NO;

// If the host *is* reachable, return success
if (hostReachable) return YES;
if (hostReachable) break;

// If the timeout has been exceeded, break out of the loop
if (_elapsedSecondsSinceAbsoluteTime(loopStart_t) >= timeoutSeconds) break;
@@ -979,8 +976,8 @@ - (BOOL)_waitForNetworkConnectionWithTimeout:(double)timeoutSeconds
usleep(250000);
}

// All checks failed - return failure
return NO;
CFRelease(reachabilityTarget);
return hostReachable;
}

/**
@@ -42,7 +42,7 @@ @implementation SPMySQLEmptyResult
/**
* Override the standard SPMySQLResult interface
*/
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
{
return [super init];
}
@@ -51,7 +51,7 @@
struct st_spmysqlstreamingrowdata *nextRow;
} SPMySQLStreamingRowData;

@interface SPMySQLFastStreamingResult (Private_API)
@interface SPMySQLFastStreamingResult () // Private API

- (void) _downloadAllData;

@@ -317,13 +317,9 @@ - (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state object
return 1;
}

@end

#pragma mark -
#pragma mark Result set internals

@implementation SPMySQLFastStreamingResult (Private_API)

/**
* Used internally to download results in a background thread
*/
@@ -28,7 +28,7 @@
// More info at <https://github.com/sequelpro/sequelpro>


@interface SPMySQLGeometryData : NSObject
@interface SPMySQLGeometryData : NSObject <NSCopying>
{
// Holds the WKB bytes coming from SQL server
Byte *geoBuffer;
@@ -38,8 +38,8 @@

}

- (id)initWithBytes:(const void *)geoData length:(NSUInteger)length;
+ (id)dataWithBytes:(const void *)geoData length:(NSUInteger)length;
- (instancetype)initWithBytes:(const void *)geoData length:(NSUInteger)length;
+ (instancetype)dataWithBytes:(const void *)geoData length:(NSUInteger)length;
- (NSString *)description;
- (NSUInteger)length;
- (NSData *)data;
@@ -28,6 +28,8 @@
// More info at <https://github.com/sequelpro/sequelpro>

#import "SPMySQLGeometryData.h"
#include <stdlib.h>
#include <string.h>

enum wkbType
{
@@ -57,7 +59,7 @@ @implementation SPMySQLGeometryData
/**
* Initialize the SPMySQLGeometryData object
*/
- (id)init
- (instancetype)init
{
if ((self = [super init])) {
geoBuffer = nil;
@@ -69,7 +71,7 @@ - (id)init
/**
* Initialize the SPMySQLGeometryData object with the WKB data
*/
- (id)initWithBytes:(const void *)geoData length:(NSUInteger)length
- (instancetype)initWithBytes:(const void *)geoData length:(NSUInteger)length
{
if ((self = [self init])) {
bufferLength = length;
@@ -82,7 +84,7 @@ - (id)initWithBytes:(const void *)geoData length:(NSUInteger)length
/**
* Return an autorelease SPMySQLGeometryData object
*/
+ (id)dataWithBytes:(const void *)geoData length:(NSUInteger)length
+ (instancetype)dataWithBytes:(const void *)geoData length:(NSUInteger)length
{
return [[[SPMySQLGeometryData alloc] initWithBytes:geoData length:length] autorelease];
}
@@ -37,7 +37,9 @@
NSTimer *wrappedTimer;
}

- (id)initWithInterval:(NSTimeInterval)anInterval target:(id)aTarget selector:(SEL)aSelector;
- (instancetype)initWithInterval:(NSTimeInterval)anInterval target:(id)aTarget selector:(SEL)aSelector NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;

- (void)invalidate;

@end
@@ -30,9 +30,8 @@


#import "SPMySQLKeepAliveTimer.h"
#import "SPMySQL Private APIs.h"

@interface SPMySQLKeepAliveTimer (Private_API)
@interface SPMySQLKeepAliveTimer () // Private API

- (void)_initKeepAliveTimer;
- (void)_forwardPing;
@@ -46,7 +45,7 @@ @implementation SPMySQLKeepAliveTimer
/**
* Prevent SPMySQLKeepAliveTimer from being init'd normally.
*/
- (id)init
- (instancetype)init
{
[NSException raise:NSInternalInconsistencyException format:@"SPMySQLKeepAliveTimers should not be init'd directly; use initWithInterval:target:selector: instead."];
return nil;
@@ -60,7 +59,7 @@ - (id)init
* After initialisation, the delegate should be set to ensure that the timer events
* are received.
*/
- (id)initWithInterval:(NSTimeInterval)anInterval target:(id)aTarget selector:(SEL)aSelector
- (instancetype)initWithInterval:(NSTimeInterval)anInterval target:(id)aTarget selector:(SEL)aSelector
{
if ((self = [super init])) {
wrappedTimer = nil;
@@ -96,13 +95,11 @@ - (void)invalidate

- (void)dealloc
{
[wrappedTimer dealloc];
[wrappedTimer release];
[super dealloc];
}

@end

@implementation SPMySQLKeepAliveTimer (Private_API)
#pragma mark - Private API

/**
* Set up the timer to tickle the target. This must be set up on the main thread
@@ -65,7 +65,7 @@ typedef enum {
}

// Master init method
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding;
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding;

// Result set information
- (NSUInteger)numberOfFields;
@@ -59,7 +59,7 @@ + (void)initialize
/**
* Standard initialisation - not intended for external use.
*/
- (id)init
- (instancetype)init
{
if ((self = [super init])) {
stringEncoding = NSASCIIStringEncoding;
@@ -83,7 +83,7 @@ - (id)init
* Standard init method, constructing the SPMySQLResult around a MySQL
* result pointer and the encoding to use when working with the data.
*/
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
{

// If no result set was passed in, return nil.
@@ -46,7 +46,8 @@

@property (readonly, assign) BOOL dataDownloaded;

- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection;
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection;
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding NS_UNAVAILABLE;

// Allow result fetching to be cancelled
- (void)cancelResultLoad;
@@ -51,7 +51,7 @@ @implementation SPMySQLStreamingResult
/**
* Prevent SPMySQLStreamingResults from being init'd as SPMySQLResults.
*/
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding
{
[NSException raise:NSInternalInconsistencyException format:@"SPMySQLFullStreamingResults should not be init'd as SPMySQLResults; use initWithMySQLResult:stringEncoding:connection:withFullStreaming: instead."];
return nil;
@@ -63,7 +63,7 @@ - (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)the
* As opposed to SPMySQLResult, defaults to returning rows as arrays, as the result
* sets are likely to be larger and processed in loops.
*/
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection
{

// If no result set was passed in, return nil.
@@ -105,9 +105,8 @@ + (void)initialize
* The download of results is not started at once - instead, it must be triggered manually
* via -startDownload, which allows assignment of a result set to replace before use.
*/
- (id)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection
- (instancetype)initWithMySQLResult:(void *)theResult stringEncoding:(NSStringEncoding)theStringEncoding connection:(SPMySQLConnection *)theConnection
{

// If no result set was passed in, return nil.
if (!theResult) return nil;

0 comments on commit 5bd7cde

Please sign in to comment.
You can’t perform that action at this time.