This repository has been archived by the owner on Jul 6, 2021. It is now read-only.
forked from nevali/opencflite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCFPlugIn_Factory.c
293 lines (249 loc) · 10.7 KB
/
CFPlugIn_Factory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Copyright (c) 2008-2009 Brent Fulgham <bfulgham@gmail.org>. All rights reserved.
*
* This source code is a modified version of the CoreFoundation sources released by Apple Inc. under
* the terms of the APSL version 2.0 (see below).
*
* For information about changes from the original Apple source release can be found by reviewing the
* source control system for the project at https://sourceforge.net/svn/?group_id=246198.
*
* The original license information is as follows:
*
* Copyright (c) 2008 Apple Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
/* CFPlugIn_Factory.c
Copyright (c) 1999-2007 Apple Inc. All rights reserved.
Responsibility: Doug Davidson
*/
#include "CFBundle_Internal.h"
#include "CFInternal.h"
static CFSpinLock_t CFPlugInGlobalDataLock = CFSpinLockInit;
static CFMutableDictionaryRef _factoriesByFactoryID = NULL; /* Value is _CFPFactory */
static CFMutableDictionaryRef _factoriesByTypeID = NULL; /* Value is array of _CFPFactory */
static void _CFPFactoryAddToTable(_CFPFactory *factory) {
__CFSpinLock(&CFPlugInGlobalDataLock);
if (_factoriesByFactoryID == NULL) {
CFDictionaryValueCallBacks _factoryDictValueCallbacks = {0, NULL, NULL, NULL, NULL};
// Use default allocator
_factoriesByFactoryID = CFDictionaryCreateMutable(kCFAllocatorSystemDefault, 0, &kCFTypeDictionaryKeyCallBacks, &_factoryDictValueCallbacks);
}
CFDictionarySetValue(_factoriesByFactoryID, factory->_uuid, factory);
__CFSpinUnlock(&CFPlugInGlobalDataLock);
}
static void _CFPFactoryRemoveFromTable(_CFPFactory *factory) {
__CFSpinLock(&CFPlugInGlobalDataLock);
if (_factoriesByFactoryID != NULL) {
CFDictionaryRemoveValue(_factoriesByFactoryID, factory->_uuid);
}
__CFSpinUnlock(&CFPlugInGlobalDataLock);
}
__private_extern__ _CFPFactory *_CFPFactoryFind(CFUUIDRef factoryID, Boolean enabled) {
_CFPFactory *result = NULL;
__CFSpinLock(&CFPlugInGlobalDataLock);
if (_factoriesByFactoryID != NULL) {
result = (_CFPFactory *)CFDictionaryGetValue(_factoriesByFactoryID, factoryID);
if (result && result->_enabled != enabled) {
result = NULL;
}
}
__CFSpinUnlock(&CFPlugInGlobalDataLock);
return result;
}
static void _CFPFactoryDeallocate(_CFPFactory *factory) {
CFAllocatorRef allocator = factory->_allocator;
SInt32 c;
_CFPFactoryRemoveFromTable(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));
}
CFRelease(factory->_types);
if (factory->_funcName) {
CFRelease(factory->_funcName);
}
if (factory->_uuid) {
CFRelease(factory->_uuid);
}
CFAllocatorDeallocate(allocator, factory);
CFRelease(allocator);
}
static _CFPFactory *_CFPFactoryCommonCreate(CFAllocatorRef allocator, CFUUIDRef factoryID) {
_CFPFactory *factory;
UInt32 size;
size = sizeof(_CFPFactory);
allocator = ((NULL == allocator) ? (CFAllocatorRef)CFRetain(__CFGetDefaultAllocator()) : (CFAllocatorRef)CFRetain(allocator));
factory = (_CFPFactory *)CFAllocatorAllocate(allocator, size, 0);
if (NULL == factory) {
CFRelease(allocator);
return NULL;
}
factory->_allocator = allocator;
factory->_uuid = (CFUUIDRef)CFRetain(factoryID);
factory->_enabled = true;
factory->_instanceCount = 0;
_CFPFactoryAddToTable(factory);
factory->_types = CFArrayCreateMutable(allocator, 0, &kCFTypeArrayCallBacks);
return factory;
}
__private_extern__ _CFPFactory *_CFPFactoryCreate(CFAllocatorRef allocator, CFUUIDRef factoryID, CFPlugInFactoryFunction func) {
_CFPFactory *factory = _CFPFactoryCommonCreate(allocator, factoryID);
factory->_func = func;
factory->_plugIn = NULL;
factory->_funcName = NULL;
return factory;
}
__private_extern__ _CFPFactory *_CFPFactoryCreateByName(CFAllocatorRef allocator, CFUUIDRef factoryID, CFPlugInRef plugIn, CFStringRef funcName) {
_CFPFactory *factory = _CFPFactoryCommonCreate(allocator, factoryID);
factory->_func = NULL;
factory->_plugIn = plugIn;
if (plugIn) {
_CFPlugInAddFactory(plugIn, factory);
}
factory->_funcName = (funcName ? (CFStringRef)CFStringCreateCopy(allocator, funcName) : NULL);
return factory;
}
__private_extern__ CFUUIDRef _CFPFactoryGetFactoryID(_CFPFactory *factory) {
return factory->_uuid;
}
__private_extern__ CFPlugInRef _CFPFactoryGetPlugIn(_CFPFactory *factory) {
return factory->_plugIn;
}
__private_extern__ void *_CFPFactoryCreateInstance(CFAllocatorRef allocator, _CFPFactory *factory, CFUUIDRef typeID) {
void *result = NULL;
if (factory->_enabled) {
if (factory->_func == NULL) {
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 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
factory->_func = (void *)((uint32_t)(factory->_func) | 0x1);
}
#endif /* BINARY_SUPPORT_CFM */
}
if (factory->_func) {
// UPPGOOP
FAULT_CALLBACK((void **)&(factory->_func));
result = (void *)INVOKE_CALLBACK2(factory->_func, allocator, typeID);
}
} else {
CFLog(__kCFLogPlugIn, CFSTR("Factory %@ is disabled"), factory->_uuid);
}
return result;
}
__private_extern__ void _CFPFactoryDisable(_CFPFactory *factory) {
factory->_enabled = false;
if (factory->_instanceCount == 0) {
_CFPFactoryDeallocate(factory);
}
}
__private_extern__ Boolean _CFPFactoryIsEnabled(_CFPFactory *factory) {
return factory->_enabled;
}
__private_extern__ void _CFPFactoryFlushFunctionCache(_CFPFactory *factory) {
/* MF:!!! Assert that this factory belongs to a plugIn. */
/* This is called by the factory's plugIn when the plugIn unloads its code. */
factory->_func = NULL;
}
__private_extern__ void _CFPFactoryAddType(_CFPFactory *factory, CFUUIDRef typeID) {
CFMutableArrayRef array;
/* Add the type to the factory's type list */
CFArrayAppendValue(factory->_types, typeID);
/* 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);
}
array = (CFMutableArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
if (array == NULL) {
CFArrayCallBacks _factoryArrayCallbacks = {0, NULL, NULL, NULL, NULL};
// Create this from default allocator
array = CFArrayCreateMutable(kCFAllocatorSystemDefault, 0, &_factoryArrayCallbacks);
CFDictionarySetValue(_factoriesByTypeID, typeID, array);
CFRelease(array);
}
CFArrayAppendValue(array, factory);
__CFSpinUnlock(&CFPlugInGlobalDataLock);
}
__private_extern__ void _CFPFactoryRemoveType(_CFPFactory *factory, CFUUIDRef typeID) {
/* Remove it from the factory's type list */
SInt32 idx;
idx = CFArrayGetFirstIndexOfValue(factory->_types, CFRangeMake(0, CFArrayGetCount(factory->_types)), typeID);
if (idx >=0) {
CFArrayRemoveValueAtIndex(factory->_types, idx);
}
/* Remove the factory from the type's list of factories */
__CFSpinLock(&CFPlugInGlobalDataLock);
if (_factoriesByTypeID != NULL) {
CFMutableArrayRef array = (CFMutableArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
if (array != NULL) {
idx = CFArrayGetFirstIndexOfValue(array, CFRangeMake(0, CFArrayGetCount(array)), factory);
if (idx >=0) {
CFArrayRemoveValueAtIndex(array, idx);
if (CFArrayGetCount(array) == 0) {
CFDictionaryRemoveValue(_factoriesByTypeID, typeID);
}
}
}
}
__CFSpinUnlock(&CFPlugInGlobalDataLock);
}
__private_extern__ Boolean _CFPFactorySupportsType(_CFPFactory *factory, CFUUIDRef typeID) {
SInt32 idx;
idx = CFArrayGetFirstIndexOfValue(factory->_types, CFRangeMake(0, CFArrayGetCount(factory->_types)), typeID);
return ((idx >= 0) ? true : false);
}
__private_extern__ CFArrayRef _CFPFactoryFindForType(CFUUIDRef typeID) {
CFArrayRef result = NULL;
__CFSpinLock(&CFPlugInGlobalDataLock);
if (_factoriesByTypeID != NULL) {
result = (CFArrayRef)CFDictionaryGetValue(_factoriesByTypeID, typeID);
}
__CFSpinUnlock(&CFPlugInGlobalDataLock);
return result;
}
/* These methods are called by CFPlugInInstance when an instance is created or destroyed. If a factory's instance count goes to 0 and the factory has been disabled, the factory is destroyed. */
__private_extern__ void _CFPFactoryAddInstance(_CFPFactory *factory) {
/* MF:!!! Assert that factory is enabled. */
factory->_instanceCount++;
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);
}
}