]> git.saurik.com Git - apple/cf.git/blobdiff - CFPlugIn_Factory.c
CF-550.tar.gz
[apple/cf.git] / CFPlugIn_Factory.c
index 876cce7bd3b1af2c6c9b0f12e1295df15c501bd8..c30e7f9be84d541f085e409b3e0da2277e5b870f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2008 Apple Inc. All rights reserved.
+ * Copyright (c) 2009 Apple Inc. All rights reserved.
  *
  * @APPLE_LICENSE_HEADER_START@
  * 
@@ -21,7 +21,7 @@
  * @APPLE_LICENSE_HEADER_END@
  */
 /*     CFPlugIn_Factory.c
-       Copyright (c) 1999-2007 Apple Inc.  All rights reserved.
+       Copyright (c) 1999-2009, Apple Inc.  All rights reserved.
        Responsibility: Doug Davidson
 */
 
@@ -34,9 +34,8 @@ static CFMutableDictionaryRef _factoriesByTypeID = NULL; /* Value is array of _C
 
 static void _CFPFactoryAddToTable(_CFPFactory *factory) {
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByFactoryID == NULL) {
+    if (!_factoriesByFactoryID) {
         CFDictionaryValueCallBacks _factoryDictValueCallbacks = {0, NULL, NULL, NULL, NULL};
-        // Use default allocator
         _factoriesByFactoryID = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &_factoryDictValueCallbacks);
     }
     CFDictionarySetValue(_factoriesByFactoryID, factory->_uuid, factory);
@@ -45,9 +44,7 @@ static void _CFPFactoryAddToTable(_CFPFactory *factory) {
 
 static void _CFPFactoryRemoveFromTable(_CFPFactory *factory) {
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByFactoryID != NULL) {
-        CFDictionaryRemoveValue(_factoriesByFactoryID, factory->_uuid);
-    }
+    if (_factoriesByFactoryID) CFDictionaryRemoveValue(_factoriesByFactoryID, factory->_uuid);
     __CFSpinUnlock(&CFPlugInGlobalDataLock);
 }
 
@@ -55,11 +52,9 @@ __private_extern__ _CFPFactory *_CFPFactoryFind(CFUUIDRef factoryID, Boolean ena
     _CFPFactory *result = NULL;
     
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByFactoryID != NULL) {
+    if (_factoriesByFactoryID) {
         result = (_CFPFactory *)CFDictionaryGetValue(_factoriesByFactoryID, factoryID);
-        if (result && result->_enabled != enabled) {
-            result = NULL;
-        }
+        if (result && result->_enabled != enabled) result = NULL;
     }
     __CFSpinUnlock(&CFPlugInGlobalDataLock);
     return result;
@@ -71,24 +66,15 @@ static void _CFPFactoryDeallocate(_CFPFactory *factory) {
     
     _CFPFactoryRemoveFromTable(factory);
 
-    if (factory->_plugIn) {
-        _CFPlugInRemoveFactory(factory->_plugIn, factory);
-    }
+    if (factory->_plugIn) _CFPlugInRemoveFactory(factory->_plugIn, factory);
 
     /* Remove all types for this factory. */
     c = CFArrayGetCount(factory->_types);
-    while (c--) {
-        _CFPFactoryRemoveType(factory, (CFUUIDRef)CFArrayGetValueAtIndex(factory->_types, c));
-    }
+    while (c-- > 0) _CFPFactoryRemoveType(factory, (CFUUIDRef)CFArrayGetValueAtIndex(factory->_types, c));
     CFRelease(factory->_types);
 
-    if (factory->_funcName) {
-        CFRelease(factory->_funcName);
-    }
-
-    if (factory->_uuid) {
-        CFRelease(factory->_uuid);
-    }
+    if (factory->_funcName) CFRelease(factory->_funcName);
+    if (factory->_uuid) CFRelease(factory->_uuid);
 
     CFAllocatorDeallocate(allocator, factory);
     CFRelease(allocator);
@@ -98,15 +84,14 @@ static _CFPFactory *_CFPFactoryCommonCreate(CFAllocatorRef allocator, CFUUIDRef
     _CFPFactory *factory;
     UInt32 size;
     size = sizeof(_CFPFactory);
-    allocator = ((NULL == allocator) ? (CFAllocatorRef)CFRetain(__CFGetDefaultAllocator()) : (CFAllocatorRef)CFRetain(allocator));
+    allocator = (allocator ? (CFAllocatorRef)CFRetain(allocator) : (CFAllocatorRef)CFRetain(__CFGetDefaultAllocator()));
     factory = (_CFPFactory *)CFAllocatorAllocate(allocator, size, 0);
-    if (NULL == factory) {
+    if (!factory) {
         CFRelease(allocator);
         return NULL;
     }
 
     factory->_allocator = allocator;
-
     factory->_uuid = (CFUUIDRef)CFRetain(factoryID);
     factory->_enabled = true;
     factory->_instanceCount = 0;
@@ -133,9 +118,7 @@ __private_extern__ _CFPFactory *_CFPFactoryCreateByName(CFAllocatorRef allocator
 
     factory->_func = NULL;
     factory->_plugIn = plugIn;
-    if (plugIn) {
-        _CFPlugInAddFactory(plugIn, factory);
-    }
+    if (plugIn) _CFPlugInAddFactory(plugIn, factory);
     factory->_funcName = (funcName ? (CFStringRef)CFStringCreateCopy(allocator, funcName) : NULL);
 
     return factory;
@@ -152,15 +135,12 @@ __private_extern__ CFPlugInRef _CFPFactoryGetPlugIn(_CFPFactory *factory) {
 __private_extern__ void *_CFPFactoryCreateInstance(CFAllocatorRef allocator, _CFPFactory *factory, CFUUIDRef typeID) {
     void *result = NULL;
     if (factory->_enabled) {
-        if (factory->_func == NULL) {
+        if (!factory->_func) {
             factory->_func = (CFPlugInFactoryFunction)CFBundleGetFunctionPointerForName(factory->_plugIn, factory->_funcName);
-            if (factory->_func == NULL) {
-                CFLog(__kCFLogPlugIn, CFSTR("Cannot find function pointer %@ for factory %@ in %@"), factory->_funcName, factory->_uuid, factory->_plugIn);
-            }
+            if (!factory->_func) CFLog(__kCFLogPlugIn, CFSTR("Cannot find function pointer %@ for factory %@ in %@"), factory->_funcName, factory->_uuid, factory->_plugIn);
 #if BINARY_SUPPORT_CFM
-            else {
-                // return values from CFBundleGetFunctionPointerForName will always be dyld, but
-                // we must force-fault them because pointers to glue code do not fault correctly
+            if (factory->_func) {
+                // return values from CFBundleGetFunctionPointerForName will always be dyld, but we must force-fault them because pointers to glue code do not fault correctly
                 factory->_func = (void *)((uint32_t)(factory->_func) | 0x1);
             }
 #endif /* BINARY_SUPPORT_CFM */
@@ -178,9 +158,7 @@ __private_extern__ void *_CFPFactoryCreateInstance(CFAllocatorRef allocator, _CF
 
 __private_extern__ void _CFPFactoryDisable(_CFPFactory *factory) {
     factory->_enabled = false;
-    if (factory->_instanceCount == 0) {
-        _CFPFactoryDeallocate(factory);
-    }
+    if (factory->_instanceCount == 0) _CFPFactoryDeallocate(factory);
 }
 
 __private_extern__ Boolean _CFPFactoryIsEnabled(_CFPFactory *factory) {
@@ -201,12 +179,9 @@ __private_extern__ void _CFPFactoryAddType(_CFPFactory *factory, CFUUIDRef typeI
 
     /* Add the factory to the type's array of factories */
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByTypeID == NULL) {
-        // Create this from default allocator
-        _factoriesByTypeID = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
-    }
+    if (!_factoriesByTypeID) _factoriesByTypeID = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
     array = (CFMutableArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
-    if (array == NULL) {
+    if (!array) {
         CFArrayCallBacks _factoryArrayCallbacks = {0, NULL, NULL, NULL, NULL};
         // Create this from default allocator
         array = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &_factoryArrayCallbacks);
@@ -222,21 +197,17 @@ __private_extern__ void _CFPFactoryRemoveType(_CFPFactory *factory, CFUUIDRef ty
     SInt32 idx;
 
     idx = CFArrayGetFirstIndexOfValue(factory->_types, CFRangeMake(0, CFArrayGetCount(factory->_types)), typeID);
-    if (idx >=0) {
-        CFArrayRemoveValueAtIndex(factory->_types, idx);
-    }
+    if (idx >= 0) CFArrayRemoveValueAtIndex(factory->_types, idx);
 
     /* Remove the factory from the type's list of factories */
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByTypeID != NULL) {
+    if (_factoriesByTypeID) {
         CFMutableArrayRef array = (CFMutableArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
-        if (array != NULL) {
+        if (array) {
             idx = CFArrayGetFirstIndexOfValue(array, CFRangeMake(0, CFArrayGetCount(array)), factory);
-            if (idx >=0) {
+            if (idx >= 0) {
                 CFArrayRemoveValueAtIndex(array, idx);
-                if (CFArrayGetCount(array) == 0) {
-                    CFDictionaryRemoveValue(_factoriesByTypeID, typeID);
-                }
+                if (CFArrayGetCount(array) == 0) CFDictionaryRemoveValue(_factoriesByTypeID, typeID);
             }
         }
     }
@@ -247,16 +218,14 @@ __private_extern__ Boolean _CFPFactorySupportsType(_CFPFactory *factory, CFUUIDR
     SInt32 idx;
 
     idx = CFArrayGetFirstIndexOfValue(factory->_types, CFRangeMake(0, CFArrayGetCount(factory->_types)), typeID);
-    return ((idx >= 0) ? true : false);
+    return (idx >= 0 ? true : false);
 }
 
 __private_extern__ CFArrayRef _CFPFactoryFindForType(CFUUIDRef typeID) {
     CFArrayRef result = NULL;
 
     __CFSpinLock(&CFPlugInGlobalDataLock);
-    if (_factoriesByTypeID != NULL) {
-        result = (CFArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
-    }
+    if (_factoriesByTypeID) result = (CFArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
     __CFSpinUnlock(&CFPlugInGlobalDataLock);
 
     return result;
@@ -266,18 +235,12 @@ __private_extern__ CFArrayRef _CFPFactoryFindForType(CFUUIDRef typeID) {
 __private_extern__ void _CFPFactoryAddInstance(_CFPFactory *factory) {
     /* MF:!!! Assert that factory is enabled. */
     factory->_instanceCount++;
-    if (factory->_plugIn) {
-        _CFPlugInAddPlugInInstance(factory->_plugIn);
-    }
+    if (factory->_plugIn) _CFPlugInAddPlugInInstance(factory->_plugIn);
 }
 
 __private_extern__ void _CFPFactoryRemoveInstance(_CFPFactory *factory) {
     /* MF:!!! Assert that _instanceCount > 0. */
     factory->_instanceCount--;
-    if (factory->_plugIn) {
-        _CFPlugInRemovePlugInInstance(factory->_plugIn);
-    }
-    if ((factory->_instanceCount == 0) && (!factory->_enabled)) {
-        _CFPFactoryDeallocate(factory);
-    }
+    if (factory->_plugIn) _CFPlugInRemovePlugInInstance(factory->_plugIn);
+    if (factory->_instanceCount == 0 && !factory->_enabled) _CFPFactoryDeallocate(factory);
 }