-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathCFBag.c
319 lines (283 loc) · 14.8 KB
/
CFBag.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* CFBag.c
Copyright (c) 1998-2019, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2019, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
Responsibility: Michael LeHew
*/
#include "CFBag.h"
#include "CFInternal.h"
#include "CFRuntime_Internal.h"
#include "CFBasicHash.h"
#include "CFString.h"
const CFBagCallBacks kCFTypeBagCallBacks = {0, __CFTypeCollectionRetain, __CFTypeCollectionRelease, CFCopyDescription, CFEqual, CFHash};
const CFBagCallBacks kCFCopyStringBagCallBacks = {0, __CFStringCollectionCopy, __CFTypeCollectionRelease, CFCopyDescription, CFEqual, CFHash};
static Boolean __CFBagEqual(CFTypeRef cf1, CFTypeRef cf2) {
return __CFBasicHashEqual((CFBasicHashRef)cf1, (CFBasicHashRef)cf2);
}
static CFHashCode __CFBagHash(CFTypeRef cf) {
return __CFBasicHashHash((CFBasicHashRef)cf);
}
static CFStringRef __CFBagCopyDescription(CFTypeRef cf) {
return __CFBasicHashCopyDescription((CFBasicHashRef)cf);
}
static void __CFBagDeallocate(CFTypeRef cf) {
__CFBasicHashDeallocate((CFBasicHashRef)cf);
}
const CFRuntimeClass __CFBagClass = {
_kCFRuntimeScannedObject,
"CFBag",
NULL, // init
NULL, // copy
__CFBagDeallocate,
__CFBagEqual,
__CFBagHash,
NULL, //
__CFBagCopyDescription
};
CFTypeID CFBagGetTypeID(void) {
return _kCFRuntimeIDCFBag;
}
static CFBasicHashRef __CFBagCreateGeneric(CFAllocatorRef allocator, CFBagCallBacks const *const inCallbacks) {
CFOptionFlags flags = kCFBasicHashLinearHashing | kCFBasicHashHasCounts; // kCFBasicHashExponentialHashing
CFBasicHashCallbacks callbacks;
callbacks.retainKey = inCallbacks ? (uintptr_t (*)(CFAllocatorRef, uintptr_t))inCallbacks->retain : NULL;
callbacks.releaseKey = inCallbacks ? (void (*)(CFAllocatorRef, uintptr_t))inCallbacks->release : NULL;
callbacks.equateKeys = inCallbacks ? (Boolean (*)(uintptr_t, uintptr_t))inCallbacks->equal : NULL;
callbacks.hashKey = inCallbacks ? (CFHashCode (*)(uintptr_t))inCallbacks->hash : NULL;
callbacks.getIndirectKey = NULL;
callbacks.copyKeyDescription = inCallbacks ? (CFStringRef (*)(uintptr_t))inCallbacks->copyDescription : NULL;
callbacks.retainValue = callbacks.retainKey;
callbacks.releaseValue = callbacks.releaseKey;
callbacks.equateValues = callbacks.equateKeys;
callbacks.copyValueDescription = callbacks.copyKeyDescription;
CFBasicHashRef ht = CFBasicHashCreate(allocator, flags, &callbacks);
return ht;
}
CF_PRIVATE CFBagRef __CFBagCreateTransfer(CFAllocatorRef allocator, const void **klist, CFIndex numValues) {
const void **vlist = klist;
CFTypeID typeID = CFBagGetTypeID();
CFAssert2(0 <= numValues, __kCFLogAssertion, "%s(): numValues (%ld) cannot be less than zero", __PRETTY_FUNCTION__, numValues);
CFOptionFlags flags = kCFBasicHashLinearHashing | kCFBasicHashHasCounts; // kCFBasicHashExponentialHashing
CFBasicHashCallbacks callbacks;
callbacks.retainKey = (uintptr_t (*)(CFAllocatorRef, uintptr_t))kCFTypeBagCallBacks.retain;
callbacks.releaseKey = (void (*)(CFAllocatorRef, uintptr_t))kCFTypeBagCallBacks.release;
callbacks.equateKeys = (Boolean (*)(uintptr_t, uintptr_t))kCFTypeBagCallBacks.equal;
callbacks.hashKey = (CFHashCode (*)(uintptr_t))kCFTypeBagCallBacks.hash;
callbacks.getIndirectKey = NULL;
callbacks.copyKeyDescription = (CFStringRef (*)(uintptr_t))kCFTypeBagCallBacks.copyDescription;
callbacks.retainValue = callbacks.retainKey;
callbacks.releaseValue = callbacks.releaseKey;
callbacks.equateValues = callbacks.equateKeys;
callbacks.copyValueDescription = callbacks.copyKeyDescription;
CFBasicHashRef ht = CFBasicHashCreate(allocator, flags, &callbacks);
CFBasicHashSuppressRC(ht);
if (0 < numValues) CFBasicHashSetCapacity(ht, numValues);
for (CFIndex idx = 0; idx < numValues; idx++) {
CFBasicHashAddValue(ht, (uintptr_t)klist[idx], (uintptr_t)vlist[idx]);
}
CFBasicHashUnsuppressRC(ht);
CFBasicHashMakeImmutable(ht);
_CFRuntimeSetInstanceTypeIDAndIsa(ht, typeID);
if (__CFOASafe) __CFSetLastAllocationEventName(ht, "CFBag (immutable)");
return (CFBagRef)ht;
}
CFBagRef CFBagCreate(CFAllocatorRef allocator, const void **klist, CFIndex numValues, const CFBagCallBacks *callbacks) {
const void **vlist = klist;
CFTypeID typeID = CFBagGetTypeID();
CFAssert2(0 <= numValues, __kCFLogAssertion, "%s(): numValues (%ld) cannot be less than zero", __PRETTY_FUNCTION__, numValues);
CFBasicHashRef ht = __CFBagCreateGeneric(allocator, callbacks);
if (!ht) return NULL;
if (0 < numValues) CFBasicHashSetCapacity(ht, numValues);
for (CFIndex idx = 0; idx < numValues; idx++) {
CFBasicHashAddValue(ht, (uintptr_t)klist[idx], (uintptr_t)vlist[idx]);
}
CFBasicHashMakeImmutable(ht);
_CFRuntimeSetInstanceTypeIDAndIsa(ht, typeID);
if (__CFOASafe) __CFSetLastAllocationEventName(ht, "CFBag (immutable)");
return (CFBagRef)ht;
}
CFMutableBagRef CFBagCreateMutable(CFAllocatorRef allocator, CFIndex capacity, const CFBagCallBacks *callbacks) {
CFTypeID typeID = CFBagGetTypeID();
CFAssert2(0 <= capacity, __kCFLogAssertion, "%s(): capacity (%ld) cannot be less than zero", __PRETTY_FUNCTION__, capacity);
CFBasicHashRef ht = __CFBagCreateGeneric(allocator, callbacks);
if (!ht) return NULL;
_CFRuntimeSetInstanceTypeIDAndIsa(ht, typeID);
if (__CFOASafe) __CFSetLastAllocationEventName(ht, "CFBag (mutable)");
return (CFMutableBagRef)ht;
}
CFBagRef CFBagCreateCopy(CFAllocatorRef allocator, CFBagRef other) {
CFTypeID typeID = CFBagGetTypeID();
CFAssert1(other, __kCFLogAssertion, "%s(): other CFBag cannot be NULL", __PRETTY_FUNCTION__);
__CFGenericValidateType(other, typeID);
Boolean markImmutable = false;
CFBasicHashRef ht = NULL;
if (CF_IS_OBJC(typeID, other)) {
CFIndex numValues = CFBagGetCount(other);
const void *vbuffer[256];
Boolean const useStack = numValues <= 256;
const void **vlist = (useStack) ? vbuffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, numValues * sizeof(const void *), 0);
const void **klist = vlist;
CFBagGetValues(other, vlist);
ht = __CFBagCreateGeneric(allocator, &kCFTypeBagCallBacks);
if (ht && 0 < numValues) CFBasicHashSetCapacity(ht, numValues);
for (CFIndex idx = 0; ht && idx < numValues; idx++) {
CFBasicHashAddValue(ht, (uintptr_t)klist[idx], (uintptr_t)vlist[idx]);
}
if (!useStack) CFAllocatorDeallocate(kCFAllocatorSystemDefault, vlist);
markImmutable = true;
} else { // non-objc types
ht = CFBasicHashCreateCopy(allocator, (CFBasicHashRef)other);
markImmutable = true;
}
if (ht && markImmutable) {
CFBasicHashMakeImmutable(ht);
_CFRuntimeSetInstanceTypeIDAndIsa(ht, typeID);
if (__CFOASafe) __CFSetLastAllocationEventName(ht, "CFBag (immutable)");
return (CFBagRef)ht;
}
return (CFBagRef)ht;
}
CFMutableBagRef CFBagCreateMutableCopy(CFAllocatorRef allocator, CFIndex capacity, CFBagRef other) {
CFTypeID typeID = CFBagGetTypeID();
CFAssert1(other, __kCFLogAssertion, "%s(): other CFBag cannot be NULL", __PRETTY_FUNCTION__);
__CFGenericValidateType(other, typeID);
CFAssert2(0 <= capacity, __kCFLogAssertion, "%s(): capacity (%ld) cannot be less than zero", __PRETTY_FUNCTION__, capacity);
CFBasicHashRef ht = NULL;
if (CF_IS_OBJC(typeID, other) || CF_IS_SWIFT(typeID, other)) {
CFIndex numValues = CFBagGetCount(other);
const void *vbuffer[256], *kbuffer[256];
Boolean const useStack = numValues <= 256;
const void **vlist = (useStack) ? vbuffer : (const void **)CFAllocatorAllocate(kCFAllocatorSystemDefault, numValues * sizeof(const void *), 0);
const void **klist = vlist;
CFBagGetValues(other, vlist);
ht = __CFBagCreateGeneric(allocator, & kCFTypeBagCallBacks);
if (ht && 0 < numValues) CFBasicHashSetCapacity(ht, numValues);
for (CFIndex idx = 0; ht && idx < numValues; idx++) {
CFBasicHashAddValue(ht, (uintptr_t)klist[idx], (uintptr_t)vlist[idx]);
}
if (klist != kbuffer && klist != vlist) CFAllocatorDeallocate(kCFAllocatorSystemDefault, klist);
if (!useStack) CFAllocatorDeallocate(kCFAllocatorSystemDefault, vlist);
} else {
ht = CFBasicHashCreateCopy(allocator, (CFBasicHashRef)other);
}
if (!ht) return NULL;
_CFRuntimeSetInstanceTypeIDAndIsa(ht, typeID);
if (__CFOASafe) __CFSetLastAllocationEventName(ht, "CFBag (mutable)");
return (CFMutableBagRef)ht;
}
CFIndex CFBagGetCount(CFBagRef hc) {
__CFGenericValidateType(hc, CFBagGetTypeID());
return CFBasicHashGetCount((CFBasicHashRef)hc);
}
// This function is for Foundation's benefit; no one else should use it.
CF_EXPORT CFIndex _CFBagGetUniqueCount(CFBagRef hc) {
__CFGenericValidateType(hc, CFBagGetTypeID());
return CFBasicHashGetUsedBucketCount((CFBasicHashRef)hc);
}
CFIndex CFBagGetCountOfValue(CFBagRef hc, const void *key) {
__CFGenericValidateType(hc, CFBagGetTypeID());
return CFBasicHashGetCountOfKey((CFBasicHashRef)hc, (uintptr_t)key);
}
Boolean CFBagContainsValue(CFBagRef hc, const void *key) {
__CFGenericValidateType(hc, CFBagGetTypeID());
return (0 < CFBasicHashGetCountOfKey((CFBasicHashRef)hc, (uintptr_t)key));
}
const void *CFBagGetValue(CFBagRef hc, const void *key) {
__CFGenericValidateType(hc, CFBagGetTypeID());
CFBasicHashBucket bkt = CFBasicHashFindBucket((CFBasicHashRef)hc, (uintptr_t)key);
return (0 < bkt.count ? (const void *)bkt.weak_value : 0);
}
Boolean CFBagGetValueIfPresent(CFBagRef hc, const void *key, const void **value) {
__CFGenericValidateType(hc, CFBagGetTypeID());
CFBasicHashBucket bkt = CFBasicHashFindBucket((CFBasicHashRef)hc, (uintptr_t)key);
if (0 < bkt.count) {
if (value) {
*value = (const void *)bkt.weak_value;
}
return true;
}
return false;
}
void CFBagGetValues(CFBagRef hc, const void **keybuf) {
const void **valuebuf = 0;
__CFGenericValidateType(hc, CFBagGetTypeID());
CFBasicHashGetElements((CFBasicHashRef)hc, CFBagGetCount(hc), (uintptr_t *)valuebuf, (uintptr_t *)keybuf);
}
void CFBagApplyFunction(CFBagRef hc, CFBagApplierFunction applier, void *context) {
FAULT_CALLBACK((void **)&(applier));
__CFGenericValidateType(hc, CFBagGetTypeID());
CFBasicHashApply((CFBasicHashRef)hc, ^(CFBasicHashBucket bkt) {
for (CFIndex cnt = bkt.count; cnt--;) {
INVOKE_CALLBACK2(applier, (const void *)bkt.weak_value, context);
}
return (Boolean)true;
});
}
// This function is for Foundation's benefit; no one else should use it.
CF_EXPORT unsigned long _CFBagFastEnumeration(CFBagRef hc, struct __objcFastEnumerationStateEquivalent *state, void *stackbuffer, unsigned long count) {
if (CF_IS_SWIFT(CFBagGetTypeID(), hc)) return 0;
if (CF_IS_OBJC(CFBagGetTypeID(), hc)) return 0;
__CFGenericValidateType(hc, CFBagGetTypeID());
return __CFBasicHashFastEnumeration((CFBasicHashRef)hc, (struct __objcFastEnumerationStateEquivalent2 *)state, stackbuffer, count);
}
// This function is for Foundation's benefit; no one else should use it.
CF_EXPORT Boolean _CFBagIsMutable(CFBagRef hc) {
if (CF_IS_SWIFT(CFBagGetTypeID(), hc)) return false;
if (CF_IS_OBJC(CFBagGetTypeID(), hc)) return false;
__CFGenericValidateType(hc, CFBagGetTypeID());
return CFBasicHashIsMutable((CFBasicHashRef)hc);
}
// This function is for Foundation's benefit; no one else should use it.
CF_EXPORT void _CFBagSetCapacity(CFMutableBagRef hc, CFIndex cap) {
if (CF_IS_SWIFT(CFBagGetTypeID(), hc)) return;
if (CF_IS_OBJC(CFBagGetTypeID(), hc)) return;
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
CFAssert3(CFBagGetCount(hc) <= cap, __kCFLogAssertion, "%s(): desired capacity (%ld) is less than count (%ld)", __PRETTY_FUNCTION__, cap, CFBagGetCount(hc));
CFBasicHashSetCapacity((CFBasicHashRef)hc, cap);
}
void CFBagAddValue(CFMutableBagRef hc, const void *key) {
const void *value = key;
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
if (!CFBasicHashIsMutable((CFBasicHashRef)hc)) {
CFLog(3, CFSTR("%s(): immutable collection %p given to mutating function"), __PRETTY_FUNCTION__, hc);
}
CFBasicHashAddValue((CFBasicHashRef)hc, (uintptr_t)key, (uintptr_t)value);
}
void CFBagReplaceValue(CFMutableBagRef hc, const void *key) {
const void *value = key;
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
if (!CFBasicHashIsMutable((CFBasicHashRef)hc)) {
CFLog(3, CFSTR("%s(): immutable collection %p given to mutating function"), __PRETTY_FUNCTION__, hc);
}
CFBasicHashReplaceValue((CFBasicHashRef)hc, (uintptr_t)key, (uintptr_t)value);
}
void CFBagSetValue(CFMutableBagRef hc, const void *key) {
const void *value = key;
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
if (!CFBasicHashIsMutable((CFBasicHashRef)hc)) {
CFLog(3, CFSTR("%s(): immutable collection %p given to mutating function"), __PRETTY_FUNCTION__, hc);
}
CFBasicHashSetValue((CFBasicHashRef)hc, (uintptr_t)key, (uintptr_t)value);
}
void CFBagRemoveValue(CFMutableBagRef hc, const void *key) {
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
if (!CFBasicHashIsMutable((CFBasicHashRef)hc)) {
CFLog(3, CFSTR("%s(): immutable collection %p given to mutating function"), __PRETTY_FUNCTION__, hc);
}
CFBasicHashRemoveValue((CFBasicHashRef)hc, (uintptr_t)key);
}
void CFBagRemoveAllValues(CFMutableBagRef hc) {
__CFGenericValidateType(hc, CFBagGetTypeID());
CFAssert2(CFBasicHashIsMutable((CFBasicHashRef)hc), __kCFLogAssertion, "%s(): immutable collection %p passed to mutating operation", __PRETTY_FUNCTION__, hc);
if (!CFBasicHashIsMutable((CFBasicHashRef)hc)) {
CFLog(3, CFSTR("%s(): immutable collection %p given to mutating function"), __PRETTY_FUNCTION__, hc);
}
CFBasicHashRemoveAllValues((CFBasicHashRef)hc);
}