[29847] users/rhwood/MacPorts.Framework

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 12 02:52:15 PDT 2007


Revision: 29847
          http://trac.macosforge.org/projects/macports/changeset/29847
Author:   rhwood at macports.org
Date:     2007-10-12 02:52:14 -0700 (Fri, 12 Oct 2007)

Log Message:
-----------
Change MPMacPorts into a single-instance-per-thread object, more tightly coupling it to MPInterpreter.
As a result of this, MPIndex no longer stores its own instance of MPMacPorts, but uses [MPMacPorts sharedInstance] to get an MPMacPorts object when it needs one.

Modified Paths:
--------------
    users/rhwood/MacPorts.Framework/MPIndex.h
    users/rhwood/MacPorts.Framework/MPIndex.m
    users/rhwood/MacPorts.Framework/MPMacPorts.h
    users/rhwood/MacPorts.Framework/MPMacPorts.m

Modified: users/rhwood/MacPorts.Framework/MPIndex.h
===================================================================
--- users/rhwood/MacPorts.Framework/MPIndex.h	2007-10-12 09:49:51 UTC (rev 29846)
+++ users/rhwood/MacPorts.Framework/MPIndex.h	2007-10-12 09:52:14 UTC (rev 29847)
@@ -13,18 +13,10 @@
 
 @interface MPIndex : NSMutableDictionary {
 
-	MPMacPorts *macPorts;
 	NSMutableDictionary *embeddedDictionary;
 
 }
 
-- (id)init;
-- (id)initWithCapacity:(unsigned)numItems;
-- (id)initWithMacPorts:(MPMacPorts *)macPortsInstance;
-- (id)initWithCapacity:(unsigned)numItems WithMacPorts:(MPMacPorts *)macPortsInstance;
-
-- (void)finishInitWithCapacity:(unsigned)numItems WithMacPorts:(MPMacPorts *)macPortsInstance;
-
 - (NSArray *)ports;
 - (NSArray *)portNames;
 

Modified: users/rhwood/MacPorts.Framework/MPIndex.m
===================================================================
--- users/rhwood/MacPorts.Framework/MPIndex.m	2007-10-12 09:49:51 UTC (rev 29846)
+++ users/rhwood/MacPorts.Framework/MPIndex.m	2007-10-12 09:52:14 UTC (rev 29847)
@@ -14,7 +14,8 @@
 - (id)init {
 	self = [super init];
 	if (self != nil) {
-		[self finishInitWithCapacity:0 WithMacPorts:[[MPMacPorts alloc] init]];
+		embeddedDictionary = [[NSMutableDictionary alloc] init];
+		[self setIndex];
 	}
 	return self;
 }
@@ -22,41 +23,22 @@
 - (id)initWithCapacity:(unsigned)numItems {
 	self = [super init];
 	if (self != nil) {
-		[self finishInitWithCapacity:(unsigned)numItems WithMacPorts:[[MPMacPorts alloc] init]];
+		embeddedDictionary = [[NSMutableDictionary alloc] initWithCapacity:numItems];
+		[self setIndex];
 	}
 	return self;
 }
 
-- (id)initWithMacPorts:(MPMacPorts *)macPortsInstance {
-	self = [super init];
-	if (self != nil) {
-		[self finishInitWithCapacity:0 WithMacPorts:(MPMacPorts *)macPortsInstance];
-	}
-	return self;
-}
-
-- (id)initWithCapacity:(unsigned)numItems WithMacPorts:(MPMacPorts *)macPortsInstance {
-	self = [super init];
-	if (self != nil) {
-		[self finishInitWithCapacity:(unsigned)numItems WithMacPorts:(MPMacPorts *)macPortsInstance];
-	}
-	return self;
-}
-
-- (void)finishInitWithCapacity:(unsigned)numItems WithMacPorts:(MPMacPorts *)macPortsInstance {
-	embeddedDictionary = [[NSMutableDictionary alloc] initWithCapacity:numItems];
-	macPorts = macPortsInstance;
-	[self setIndex];
-}
-
 - (void)dealloc {
 	[embeddedDictionary release];
 	[super dealloc];
 }
 
 - (void)setIndex {
-	NSEnumerator* portIndexEnumerator;
-	NSString* portIndex;
+	NSEnumerator *portIndexEnumerator;
+	NSString *portIndex;
+	MPMacPorts *macPorts;
+	macPorts = [MPMacPorts sharedInstance];
 	[self removeAllObjects];
 	portIndexEnumerator = [[macPorts sources] objectEnumerator];
 	while (portIndex = [portIndexEnumerator nextObject]) {
@@ -69,6 +51,8 @@
 	NSArray* lines;
 	NSEnumerator* linesEnumerator;
 	id line;
+	MPMacPorts *macPorts;
+	macPorts = [MPMacPorts sharedInstance];
 	file = [[NSString alloc] initWithContentsOfFile:[macPorts pathToPortIndex:portIndex]];
 	lines = [file componentsSeparatedByString:@"\n"];
 	linesEnumerator = [lines objectEnumerator];

Modified: users/rhwood/MacPorts.Framework/MPMacPorts.h
===================================================================
--- users/rhwood/MacPorts.Framework/MPMacPorts.h	2007-10-12 09:49:51 UTC (rev 29846)
+++ users/rhwood/MacPorts.Framework/MPMacPorts.h	2007-10-12 09:52:14 UTC (rev 29847)
@@ -21,6 +21,8 @@
 	
 }
 
++ (MPMacPorts *)sharedInstance;
+
 - (NSDictionary *)search:(NSString *)query;
 - (NSDictionary *)search:(NSString *)query caseSensitive:(BOOL)sensitivity;
 - (NSDictionary *)search:(NSString *)query caseSensitive:(BOOL)sensitivity matchStyle:(NSString *)style;

Modified: users/rhwood/MacPorts.Framework/MPMacPorts.m
===================================================================
--- users/rhwood/MacPorts.Framework/MPMacPorts.m	2007-10-12 09:49:51 UTC (rev 29846)
+++ users/rhwood/MacPorts.Framework/MPMacPorts.m	2007-10-12 09:52:14 UTC (rev 29847)
@@ -12,18 +12,51 @@
 @implementation MPMacPorts
 
 - (id) init {
-	self = [super init];
-	if (self != nil) {
+	if (self = [super init]) {
 		interpreter = [MPInterpreter sharedInterpreter];
 	}
 	return self;
 }
 
-- (void) dealloc {
-	[interpreter release];
-	[super dealloc];
++ (MPMacPorts *)sharedInstance {
+	@synchronized(self) {
+		if ([[[NSThread currentThread] threadDictionary] objectForKey:@"sharedMPMacPorts"] == nil) {
+			[[self alloc] init]; // assignment not done here
+		}
+	}
+	return [[[NSThread currentThread] threadDictionary] objectForKey:@"sharedMPMacPorts"];
 }
 
++ (id)allocWithZone:(NSZone*)zone {
+	@synchronized(self) {
+		if ([[[NSThread currentThread] threadDictionary] objectForKey:@"sharedMPMacPorts"] == nil) {
+			[[[NSThread currentThread] threadDictionary] setObject:[super allocWithZone:zone] forKey:@"sharedMPMacPorts"];
+			return [[[NSThread currentThread] threadDictionary] objectForKey:@"sharedMPMacPorts"];	// assignment and return on first allocation
+		}
+	}
+	return nil;	// subsequent allocation attempts return nil
+}
+
+- (id)copyWithZone:(NSZone*)zone {
+    return self;
+}
+
+- (id)retain {
+    return self;
+}
+
+- (unsigned)retainCount {
+    return UINT_MAX;  //denotes an object that cannot be released
+}
+
+- (void)release {
+    //do nothing
+}
+
+- (id)autorelease {
+    return self;
+}
+
 #pragma MacPorts API
 
 - (NSDictionary *)search:(NSString *)query {

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20071012/cfd1f3f7/attachment.html


More information about the macports-changes mailing list