]> git.saurik.com Git - apple/cf.git/blob - CFInternal.h
CF-1152.14.tar.gz
[apple/cf.git] / CFInternal.h
1 /*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /* CFInternal.h
25 Copyright (c) 1998-2014, Apple Inc. All rights reserved.
26 */
27
28 /*
29 NOT TO BE USED OUTSIDE CF!
30 */
31
32 #if !CF_BUILDING_CF
33 #error The header file CFInternal.h is for the exclusive use of CoreFoundation. No other project should include it.
34 #endif
35
36 #if !defined(__COREFOUNDATION_CFINTERNAL__)
37 #define __COREFOUNDATION_CFINTERNAL__ 1
38
39
40 #define __CF_COMPILE_YEAR__ (__DATE__[7] * 1000 + __DATE__[8] * 100 + __DATE__[9] * 10 + __DATE__[10] - 53328)
41 #define __CF_COMPILE_MONTH__ ((__DATE__[1] + __DATE__[2] == 207) ? 1 : \
42 (__DATE__[1] + __DATE__[2] == 199) ? 2 : \
43 (__DATE__[1] + __DATE__[2] == 211) ? 3 : \
44 (__DATE__[1] + __DATE__[2] == 226) ? 4 : \
45 (__DATE__[1] + __DATE__[2] == 218) ? 5 : \
46 (__DATE__[1] + __DATE__[2] == 227) ? 6 : \
47 (__DATE__[1] + __DATE__[2] == 225) ? 7 : \
48 (__DATE__[1] + __DATE__[2] == 220) ? 8 : \
49 (__DATE__[1] + __DATE__[2] == 213) ? 9 : \
50 (__DATE__[1] + __DATE__[2] == 215) ? 10 : \
51 (__DATE__[1] + __DATE__[2] == 229) ? 11 : \
52 (__DATE__[1] + __DATE__[2] == 200) ? 12 : 0)
53 #define __CF_COMPILE_DAY__ (__DATE__[4] * 10 + __DATE__[5] - (__DATE__[4] == ' ' ? 368 : 528))
54 #define __CF_COMPILE_DATE__ (__CF_COMPILE_YEAR__ * 10000 + __CF_COMPILE_MONTH__ * 100 + __CF_COMPILE_DAY__)
55
56 #define __CF_COMPILE_HOUR__ (__TIME__[0] * 10 + __TIME__[1] - 528)
57 #define __CF_COMPILE_MINUTE__ (__TIME__[3] * 10 + __TIME__[4] - 528)
58 #define __CF_COMPILE_SECOND__ (__TIME__[6] * 10 + __TIME__[7] - 528)
59 #define __CF_COMPILE_TIME__ (__CF_COMPILE_HOUR__ * 10000 + __CF_COMPILE_MINUTE__ * 100 + __CF_COMPILE_SECOND__)
60
61 #define __CF_COMPILE_SECOND_OF_DAY__ (__CF_COMPILE_HOUR__ * 3600 + __CF_COMPILE_MINUTE__ * 60 + __CF_COMPILE_SECOND__)
62
63 // __CF_COMPILE_DAY_OF_EPOCH__ works within Gregorian years 2001 - 2099; the epoch is of course CF's epoch
64 #define __CF_COMPILE_DAY_OF_EPOCH__ ((__CF_COMPILE_YEAR__ - 2001) * 365 + (__CF_COMPILE_YEAR__ - 2001) / 4 \
65 + ((__DATE__[1] + __DATE__[2] == 207) ? 0 : \
66 (__DATE__[1] + __DATE__[2] == 199) ? 31 : \
67 (__DATE__[1] + __DATE__[2] == 211) ? 59 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
68 (__DATE__[1] + __DATE__[2] == 226) ? 90 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
69 (__DATE__[1] + __DATE__[2] == 218) ? 120 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
70 (__DATE__[1] + __DATE__[2] == 227) ? 151 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
71 (__DATE__[1] + __DATE__[2] == 225) ? 181 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
72 (__DATE__[1] + __DATE__[2] == 220) ? 212 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
73 (__DATE__[1] + __DATE__[2] == 213) ? 243 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
74 (__DATE__[1] + __DATE__[2] == 215) ? 273 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
75 (__DATE__[1] + __DATE__[2] == 229) ? 304 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
76 (__DATE__[1] + __DATE__[2] == 200) ? 334 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
77 365 + (__CF_COMPILE_YEAR__ % 4 == 0)) \
78 + __CF_COMPILE_DAY__)
79
80
81 CF_EXTERN_C_BEGIN
82
83 #include <CoreFoundation/CFBase.h>
84 #include <CoreFoundation/CFURL.h>
85 #include <CoreFoundation/CFString.h>
86 #include <CoreFoundation/CFDate.h>
87 #include <CoreFoundation/CFArray.h>
88 #include <CoreFoundation/CFLogUtilities.h>
89 #include <CoreFoundation/CFRuntime.h>
90 #include <limits.h>
91 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
92 #include <xlocale.h>
93 #include <unistd.h>
94 #include <sys/time.h>
95 #include <signal.h>
96 #include <stdio.h>
97 #endif
98 #include <pthread.h>
99
100
101 #if defined(__BIG_ENDIAN__)
102 #define __CF_BIG_ENDIAN__ 1
103 #define __CF_LITTLE_ENDIAN__ 0
104 #endif
105
106 #if defined(__LITTLE_ENDIAN__)
107 #define __CF_LITTLE_ENDIAN__ 1
108 #define __CF_BIG_ENDIAN__ 0
109 #endif
110
111
112 #include <CoreFoundation/ForFoundationOnly.h>
113
114 CF_EXPORT const char *_CFProcessName(void);
115 CF_EXPORT CFStringRef _CFProcessNameString(void);
116
117 CF_EXPORT Boolean _CFGetCurrentDirectory(char *path, int maxlen);
118
119 CF_EXPORT CFArrayRef _CFGetWindowsBinaryDirectories(void);
120
121 CF_EXPORT CFStringRef _CFStringCreateHostName(void);
122
123 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
124 #include <CoreFoundation/CFRunLoop.h>
125 CF_EXPORT void _CFMachPortInstallNotifyPort(CFRunLoopRef rl, CFStringRef mode);
126 #endif
127
128
129 CF_PRIVATE CFIndex __CFActiveProcessorCount();
130
131 #ifndef CLANG_ANALYZER_NORETURN
132 #if __has_feature(attribute_analyzer_noreturn)
133 #define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
134 #else
135 #define CLANG_ANALYZER_NORETURN
136 #endif
137 #endif
138
139 #if DEPLOYMENT_TARGET_WINDOWS
140 #define __builtin_unreachable() do { } while (0)
141 #endif
142
143 #if defined(__i386__) || defined(__x86_64__)
144 #if defined(__GNUC__)
145 #define HALT do {asm __volatile__("int3"); kill(getpid(), 9); __builtin_unreachable(); } while (0)
146 #elif defined(_MSC_VER)
147 #define HALT do { DebugBreak(); abort(); __builtin_unreachable(); } while (0)
148 #else
149 #error Compiler not supported
150 #endif
151 #endif
152
153
154 #if defined(DEBUG)
155 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
156 do { \
157 if (!(cond)) { \
158 CFLog(prio, CFSTR(desc), a1, a2, a3, a4, a5); \
159 /* HALT; */ \
160 } \
161 } while (0)
162 #else
163 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
164 do {} while (0)
165 #endif
166
167 #define CFAssert(condition, priority, description) \
168 __CFAssert((condition), (priority), description, 0, 0, 0, 0, 0)
169 #define CFAssert1(condition, priority, description, a1) \
170 __CFAssert((condition), (priority), description, (a1), 0, 0, 0, 0)
171 #define CFAssert2(condition, priority, description, a1, a2) \
172 __CFAssert((condition), (priority), description, (a1), (a2), 0, 0, 0)
173 #define CFAssert3(condition, priority, description, a1, a2, a3) \
174 __CFAssert((condition), (priority), description, (a1), (a2), (a3), 0, 0)
175 #define CFAssert4(condition, priority, description, a1, a2, a3, a4) \
176 __CFAssert((condition), (priority), description, (a1), (a2), (a3), (a4), 0)
177
178 #define __kCFLogAssertion 3
179
180 // This CF-only log function uses no CF functionality, so it may be called anywhere within CF - including thread teardown or prior to full CF setup
181 CF_PRIVATE void _CFLogSimple(int32_t lev, char *format, ...);
182
183 #if defined(DEBUG)
184 extern void __CFGenericValidateType_(CFTypeRef cf, CFTypeID type, const char *func);
185 #define __CFGenericValidateType(cf, type) __CFGenericValidateType_(cf, type, __PRETTY_FUNCTION__)
186 #else
187 #define __CFGenericValidateType(cf, type) ((void)0)
188 #endif
189
190 #define CF_INFO_BITS (!!(__CF_BIG_ENDIAN__) * 3)
191 #define CF_RC_BITS (!!(__CF_LITTLE_ENDIAN__) * 3)
192
193 /* Bit manipulation macros */
194 /* Bits are numbered from 31 on left to 0 on right */
195 /* May or may not work if you use them on bitfields in types other than UInt32, bitfields the full width of a UInt32, or anything else for which they were not designed. */
196 /* In the following, N1 and N2 specify an inclusive range N2..N1 with N1 >= N2 */
197 #define __CFBitfieldMask(N1, N2) ((((UInt32)~0UL) << (31UL - (N1) + (N2))) >> (31UL - N1))
198 #define __CFBitfieldGetValue(V, N1, N2) (((V) & __CFBitfieldMask(N1, N2)) >> (N2))
199 #define __CFBitfieldSetValue(V, N1, N2, X) ((V) = ((V) & ~__CFBitfieldMask(N1, N2)) | (((X) << (N2)) & __CFBitfieldMask(N1, N2)))
200 #define __CFBitfieldMaxValue(N1, N2) __CFBitfieldGetValue(0xFFFFFFFFUL, (N1), (N2))
201
202 #define __CFBitIsSet(V, N) (((V) & (1UL << (N))) != 0)
203 #define __CFBitSet(V, N) ((V) |= (1UL << (N)))
204 #define __CFBitClear(V, N) ((V) &= ~(1UL << (N)))
205
206 // Foundation uses 20-40
207 // Foundation knows about the value of __CFTSDKeyAutoreleaseData1
208 enum {
209 __CFTSDKeyAllocator = 1,
210 __CFTSDKeyIsInCFLog = 2,
211 __CFTSDKeyIsInNSCache = 3,
212 __CFTSDKeyIsInGCDMainQ = 4,
213 __CFTSDKeyICUConverter = 7,
214 __CFTSDKeyCollatorLocale = 8,
215 __CFTSDKeyCollatorUCollator = 9,
216 __CFTSDKeyRunLoop = 10,
217 __CFTSDKeyRunLoopCntr = 11,
218 __CFTSDKeyMachMessageBoost = 12, // valid only in the context of a CFMachPort callout
219 __CFTSDKeyMachMessageHasVoucher = 13,
220 // autorelease pool stuff must be higher than run loop constants
221 __CFTSDKeyAutoreleaseData2 = 61,
222 __CFTSDKeyAutoreleaseData1 = 62,
223 __CFTSDKeyExceptionData = 63,
224 };
225
226 #define __kCFAllocatorTypeID_CONST 2
227
228 CF_INLINE CFAllocatorRef __CFGetDefaultAllocator(void) {
229 CFAllocatorRef allocator = (CFAllocatorRef)_CFGetTSD(__CFTSDKeyAllocator);
230 if (NULL == allocator) {
231 allocator = kCFAllocatorSystemDefault;
232 }
233 return allocator;
234 }
235
236
237 #if !defined(LLONG_MAX)
238 #if defined(_I64_MAX)
239 #define LLONG_MAX _I64_MAX
240 #else
241 #warning Arbitrarily defining LLONG_MAX
242 #define LLONG_MAX (int64_t)9223372036854775807
243 #endif
244 #endif /* !defined(LLONG_MAX) */
245
246 #if !defined(LLONG_MIN)
247 #if defined(_I64_MIN)
248 #define LLONG_MIN _I64_MIN
249 #else
250 #warning Arbitrarily defining LLONG_MIN
251 #define LLONG_MIN (-LLONG_MAX - (int64_t)1)
252 #endif
253 #endif /* !defined(LLONG_MIN) */
254
255 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
256 #define __CFMin(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
257 #define __CFMax(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
258 #else /* __GNUC__ */
259 #define __CFMin(A,B) ((A) < (B) ? (A) : (B))
260 #define __CFMax(A,B) ((A) > (B) ? (A) : (B))
261 #endif /* __GNUC__ */
262
263 /* Secret CFAllocator hint bits */
264 #define __kCFAllocatorTempMemory 0x2
265 #define __kCFAllocatorNoPointers 0x10
266 #define __kCFAllocatorDoNotRecordEvent 0x100
267 #define __kCFAllocatorGCScannedMemory 0x200 /* GC: memory should be scanned. */
268 #define __kCFAllocatorGCObjectMemory 0x400 /* GC: memory needs to be finalized. */
269
270 CF_INLINE auto_memory_type_t CF_GET_GC_MEMORY_TYPE(CFOptionFlags flags) {
271 auto_memory_type_t type = (flags & __kCFAllocatorGCScannedMemory ? 0 : AUTO_UNSCANNED) | (flags & __kCFAllocatorGCObjectMemory ? AUTO_OBJECT : 0);
272 return type;
273 }
274
275 CF_INLINE void __CFAssignWithWriteBarrier(void **location, void *value) {
276 if (kCFUseCollectableAllocator) {
277 objc_assign_strongCast((id)value, (id *)location);
278 } else {
279 *location = value;
280 }
281 }
282
283 // Zero-retain count CFAllocator functions, i.e. memory that will be collected, no dealloc necessary
284 CF_EXPORT void *_CFAllocatorAllocateGC(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint);
285 CF_EXPORT void *_CFAllocatorReallocateGC(CFAllocatorRef allocator, void *ptr, CFIndex newsize, CFOptionFlags hint);
286 CF_EXPORT void _CFAllocatorDeallocateGC(CFAllocatorRef allocator, void *ptr);
287
288 CF_EXPORT CFAllocatorRef _CFTemporaryMemoryAllocator(void);
289
290 extern uint64_t __CFTimeIntervalToTSR(CFTimeInterval ti);
291 extern CFTimeInterval __CFTSRToTimeInterval(uint64_t tsr);
292 // use this instead of attempting to subtract mach_absolute_time() directly, because that can underflow and give an unexpected answer
293 CF_PRIVATE CFTimeInterval __CFTimeIntervalUntilTSR(uint64_t tsr);
294 CF_PRIVATE dispatch_time_t __CFTSRToDispatchTime(uint64_t tsr);
295 CF_PRIVATE uint64_t __CFTSRToNanoseconds(uint64_t tsr);
296
297 extern CFStringRef __CFCopyFormattingDescription(CFTypeRef cf, CFDictionaryRef formatOptions);
298
299 /* Enhanced string formatting support
300 */
301 CF_PRIVATE CFDictionaryRef _CFStringGetFormatSpecifierConfiguration(CFStringRef aFormatString);
302 CF_PRIVATE CFStringRef _CFStringCopyWithFomatStringConfiguration(CFStringRef aFormatString, CFDictionaryRef formatConfiguration);
303 CF_PRIVATE CFStringRef _CFCopyResolvedFormatStringWithConfiguration(CFTypeRef anObject, CFDictionaryRef aConfiguration, CFDictionaryRef formatOptions);
304
305 /* result is long long or int, depending on doLonglong
306 */
307 extern Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result);
308 extern Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, double *resultPtr);
309 extern Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result);
310
311 extern const char *__CFgetenv(const char *n);
312
313 CF_PRIVATE Boolean __CFProcessIsRestricted();
314
315 // This is really about the availability of C99. We don't have that on Windows, but we should everywhere else.
316 #if DEPLOYMENT_TARGET_WINDOWS
317 #define STACK_BUFFER_DECL(T, N, C) T *N = (T *)_alloca((C) * sizeof(T))
318 #else
319 #define STACK_BUFFER_DECL(T, N, C) T N[C]
320 #endif
321
322
323 CF_EXPORT void * __CFConstantStringClassReferencePtr;
324
325 #ifdef __CONSTANT_CFSTRINGS__
326
327 #define CONST_STRING_DECL(S, V) const CFStringRef S = (const CFStringRef)__builtin___CFStringMakeConstantString(V);
328 #define PE_CONST_STRING_DECL(S, V) CF_PRIVATE const CFStringRef S = (const CFStringRef)__builtin___CFStringMakeConstantString(V);
329
330 #else
331
332 struct CF_CONST_STRING {
333 CFRuntimeBase _base;
334 uint8_t *_ptr;
335 uint32_t _length;
336 };
337
338 CF_EXPORT int __CFConstantStringClassReference[];
339
340 /* CFNetwork also has a copy of the CONST_STRING_DECL macro (for use on platforms without constant string support in cc); please warn cfnetwork-core@group.apple.com of any necessary changes to this macro. -- REW, 1/28/2002 */
341
342 #if __CF_BIG_ENDIAN__
343
344 #define CONST_STRING_DECL(S, V) \
345 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
346 const CFStringRef S = (CFStringRef) & __ ## S ## __;
347 #define PE_CONST_STRING_DECL(S, V) \
348 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
349 CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
350
351 #elif __CF_LITTLE_ENDIAN__
352
353 #define CONST_STRING_DECL(S, V) \
354 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
355 const CFStringRef S = (CFStringRef) & __ ## S ## __;
356 #define PE_CONST_STRING_DECL(S, V) \
357 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
358 CF_PRIVATE const CFStringRef S = (CFStringRef) & __ ## S ## __;
359
360 #endif
361
362 #endif // __CONSTANT_CFSTRINGS__
363
364 CF_EXPORT bool __CFOASafe;
365 CF_EXPORT void __CFSetLastAllocationEventName(void *ptr, const char *classname);
366
367
368
369 /* Comparators are passed the address of the values; this is somewhat different than CFComparatorFunction is used in public API usually. */
370 CF_EXPORT CFIndex CFBSearch(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context);
371
372 CF_EXPORT CFHashCode CFHashBytes(UInt8 *bytes, CFIndex length);
373
374 CF_EXPORT CFStringEncoding CFStringFileSystemEncoding(void);
375
376 CF_PRIVATE CFStringRef __CFStringCreateImmutableFunnel3(CFAllocatorRef alloc, const void *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean possiblyExternalFormat, Boolean tryToReduceUnicode, Boolean hasLengthByte, Boolean hasNullByte, Boolean noCopy, CFAllocatorRef contentsDeallocator, UInt32 converterFlags);
377
378 extern const void *__CFStringCollectionCopy(CFAllocatorRef allocator, const void *ptr);
379 extern const void *__CFTypeCollectionRetain(CFAllocatorRef allocator, const void *ptr);
380 extern void __CFTypeCollectionRelease(CFAllocatorRef allocator, const void *ptr);
381
382 extern CFTypeRef CFMakeUncollectable(CFTypeRef cf);
383
384 CF_PRIVATE void _CFRaiseMemoryException(CFStringRef reason);
385
386 CF_PRIVATE Boolean __CFProphylacticAutofsAccess;
387
388
389 #if DEPLOYMENT_TARGET_MACOSX
390
391 typedef pthread_mutex_t CFLock_t;
392
393 #define CFLockInit ((pthread_mutex_t)PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
394 #define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
395
396 #define __CFLock(LP) ({ \
397 (void)pthread_mutex_lock(LP); })
398
399 #define __CFUnlock(LP) ({ \
400 (void)pthread_mutex_unlock(LP); })
401
402 #define __CFLockTry(LP) ({ \
403 pthread_mutex_trylock(LP) == 0; })
404
405 #elif DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
406
407 typedef pthread_mutex_t CFLock_t;
408
409 #define CFLockInit ((pthread_mutex_t)PTHREAD_ERRORCHECK_MUTEX_INITIALIZER)
410 #define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
411
412 #define __CFLock(LP) ({ \
413 (void)pthread_mutex_lock(LP); })
414
415 #define __CFUnlock(LP) ({ \
416 (void)pthread_mutex_unlock(LP); })
417
418 #define __CFLockTry(LP) ({ \
419 pthread_mutex_trylock(LP) == 0; })
420
421 #elif DEPLOYMENT_TARGET_WINDOWS
422
423 typedef int32_t CFLock_t;
424 #define CFLockInit 0
425 #define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
426
427 CF_INLINE void __CFLock(volatile CFLock_t *lock) {
428 while (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) != 0) {
429 Sleep(0);
430 }
431 }
432
433 CF_INLINE void __CFUnlock(volatile CFLock_t *lock) {
434 MemoryBarrier();
435 *lock = 0;
436 }
437
438 CF_INLINE Boolean __CFLockTry(volatile CFLock_t *lock) {
439 return (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) == 0);
440 }
441
442 #elif DEPLOYMENT_TARGET_LINUX
443
444 typedef int32_t CFLock_t;
445 #define CFLockInit 0
446 #define CF_LOCK_INIT_FOR_STRUCTS(X) (X = CFLockInit)
447
448 CF_INLINE void __CFLock(volatile CFLock_t *lock) {
449 while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
450 sleep(0);
451 }
452 }
453
454 CF_INLINE void __CFUnlock(volatile CFLock_t *lock) {
455 __sync_synchronize();
456 *lock = 0;
457 }
458
459 CF_INLINE Boolean __CFLockTry(volatile CFLock_t *lock) {
460 return (__sync_val_compare_and_swap(lock, 0, ~0) == 0);
461 }
462
463 #else
464
465 #warning CF locks not defined for this platform -- CF is not thread-safe
466 #define __CFLock(A) do {} while (0)
467 #define __CFUnlock(A) do {} while (0)
468
469 #endif
470
471
472 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
473 extern uint8_t __CF120293;
474 extern uint8_t __CF120290;
475 extern void __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__(void);
476 #define CHECK_FOR_FORK() do { __CF120290 = true; if (__CF120293) __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__(); } while (0)
477 #define CHECK_FOR_FORK_RET(...) do { CHECK_FOR_FORK(); if (__CF120293) return __VA_ARGS__; } while (0)
478 #define HAS_FORKED() (__CF120293)
479 #endif
480
481 #if !defined(CHECK_FOR_FORK)
482 #define CHECK_FOR_FORK() do { } while (0)
483 #endif
484
485 #if !defined(CHECK_FOR_FORK_RET)
486 #define CHECK_FOR_FORK_RET(...) do { } while (0)
487 #endif
488
489 #if !defined(HAS_FORKED)
490 #define HAS_FORKED() 0
491 #endif
492
493 #include <errno.h>
494
495 #define thread_errno() errno
496 #define thread_set_errno(V) do {errno = (V);} while (0)
497
498 extern void *__CFStartSimpleThread(void *func, void *arg);
499
500 /* ==================== Simple file access ==================== */
501 /* For dealing with abstract types. MF:!!! These ought to be somewhere else and public. */
502
503 CF_EXPORT CFStringRef _CFCopyExtensionForAbstractType(CFStringRef abstractType);
504
505 /* ==================== Simple file access ==================== */
506 /* These functions all act on a c-strings which must be in the file system encoding. */
507
508 CF_PRIVATE Boolean _CFCreateDirectory(const char *path);
509 CF_PRIVATE Boolean _CFRemoveDirectory(const char *path);
510 CF_PRIVATE Boolean _CFDeleteFile(const char *path);
511
512 CF_PRIVATE Boolean _CFReadBytesFromFile(CFAllocatorRef alloc, CFURLRef url, void **bytes, CFIndex *length, CFIndex maxLength, int extraOpenFlags);
513 /* resulting bytes are allocated from alloc which MUST be non-NULL. */
514 /* maxLength of zero means the whole file. Otherwise it sets a limit on the number of bytes read. */
515
516 CF_EXPORT Boolean _CFWriteBytesToFile(CFURLRef url, const void *bytes, CFIndex length);
517
518 CF_PRIVATE CFMutableArrayRef _CFCreateContentsOfDirectory(CFAllocatorRef alloc, char *dirPath, void *dirSpec, CFURLRef dirURL, CFStringRef matchingAbstractType);
519 /* On Mac OS 8/9, one of dirSpec, dirPath and dirURL must be non-NULL */
520 /* On all other platforms, one of path and dirURL must be non-NULL */
521 /* If both are present, they are assumed to be in-synch; that is, they both refer to the same directory. */
522 /* alloc may be NULL */
523 /* return value is CFArray of CFURLs */
524
525 CF_PRIVATE SInt32 _CFGetPathProperties(CFAllocatorRef alloc, char *path, Boolean *exists, SInt32 *posixMode, SInt64 *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents);
526 /* alloc may be NULL */
527 /* any of exists, posixMode, size, modTime, and dirContents can be NULL. Usually it is not a good idea to pass NULL for exists, since interpretting the other values sometimes requires that you know whether the file existed or not. Except for dirContents, it is pretty cheap to compute any of these things as loing as one of them must be computed. */
528
529 CF_PRIVATE SInt32 _CFGetFileProperties(CFAllocatorRef alloc, CFURLRef pathURL, Boolean *exists, SInt32 *posixMode, SInt64 *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents);
530 /* alloc may be NULL */
531 /* any of exists, posixMode, size, modTime, and dirContents can be NULL. Usually it is not a good idea to pass NULL for exists, since interpretting the other values sometimes requires that you know whether the file existed or not. Except for dirContents, it is pretty cheap to compute any of these things as loing as one of them must be computed. */
532
533
534 /* ==================== Simple path manipulation ==================== */
535
536 CF_EXPORT UniChar _CFGetSlash();
537 CF_PRIVATE CFStringRef _CFGetSlashStr();
538 CF_EXPORT Boolean _CFIsAbsolutePath(UniChar *unichars, CFIndex length);
539 CF_PRIVATE void _CFAppendTrailingPathSlash2(CFMutableStringRef path);
540 CF_PRIVATE void _CFAppendConditionalTrailingPathSlash2(CFMutableStringRef path);
541 CF_EXPORT Boolean _CFAppendPathComponent(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *component, CFIndex componentLength);
542 CF_PRIVATE void _CFAppendPathComponent2(CFMutableStringRef path, CFStringRef component);
543 CF_PRIVATE Boolean _CFAppendPathExtension2(CFMutableStringRef path, CFStringRef extension);
544 CF_EXPORT Boolean _CFAppendPathExtension(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *extension, CFIndex extensionLength);
545 CF_EXPORT Boolean _CFTransmutePathSlashes(UniChar *unichars, CFIndex *length, UniChar replSlash);
546 CF_PRIVATE CFStringRef _CFCreateLastPathComponent(CFAllocatorRef alloc, CFStringRef path, CFIndex *slashIndex);
547 CF_EXPORT CFIndex _CFStartOfLastPathComponent(UniChar *unichars, CFIndex length);
548 CF_PRIVATE CFIndex _CFStartOfLastPathComponent2(CFStringRef path);
549 CF_EXPORT CFIndex _CFLengthAfterDeletingLastPathComponent(UniChar *unichars, CFIndex length);
550 CF_PRIVATE CFIndex _CFLengthAfterDeletingPathExtension2(CFStringRef path);
551 CF_EXPORT CFIndex _CFStartOfPathExtension(UniChar *unichars, CFIndex length);
552 CF_PRIVATE CFIndex _CFStartOfPathExtension2(CFStringRef path);
553 CF_EXPORT CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFIndex length);
554
555 #if __BLOCKS__
556 #if DEPLOYMENT_TARGET_WINDOWS
557 #define DT_DIR 4
558 #define DT_REG 8
559 #define DT_LNK 10
560 #endif
561
562 // This function automatically skips '.' and '..', and '._' files
563 CF_PRIVATE void _CFIterateDirectory(CFStringRef directoryPath, Boolean (^fileHandler)(CFStringRef fileName, uint8_t fileType));
564 #endif
565
566 #define __CFMaxRuntimeTypes 65535
567 #define __CFRuntimeClassTableSize 1024
568
569 extern void _CFRuntimeSetInstanceTypeIDAndIsa(CFTypeRef cf, CFTypeID newTypeID);
570
571 #define CF_OBJC_FUNCDISPATCHV(typeID, obj, ...) do { } while (0)
572 #define CF_OBJC_CALLV(obj, ...) (0)
573 #define CF_IS_OBJC(typeID, obj) (0)
574 #define __CFISAForTypeID(t) (0)
575
576 /* See comments in CFBase.c
577 */
578 #define FAULT_CALLBACK(V)
579 #define INVOKE_CALLBACK1(P, A) (P)(A)
580 #define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
581 #define INVOKE_CALLBACK3(P, A, B, C) (P)(A, B, C)
582 #define INVOKE_CALLBACK4(P, A, B, C, D) (P)(A, B, C, D)
583 #define INVOKE_CALLBACK5(P, A, B, C, D, E) (P)(A, B, C, D, E)
584 #define UNFAULT_CALLBACK(V) do { } while (0)
585
586 /* For the support of functionality which needs CarbonCore or other frameworks */
587 // These macros define an upcall or weak "symbol-lookup" wrapper function.
588 // The parameters are:
589 // R : the return type of the function
590 // N : the name of the function (in the other library)
591 // P : the parenthesized parameter list of the function
592 // A : the parenthesized actual argument list to be passed
593 // FAILACTION: (only for the _FAIL macros) additional code to be
594 // run when the function cannot be found.
595 // opt: a fifth optional argument can be passed in which is the
596 // return value of the wrapper when the function cannot be
597 // found; should be of type R, & can be a function call
598 // The name of the resulting wrapper function is:
599 // __CFCarbonCore_N (where N is the second parameter)
600 // __CFNetwork_N (where N is the second parameter)
601 //
602 // Example:
603 // DEFINE_WEAK_CARBONCORE_FUNC(void, DisposeHandle, (Handle h), (h))
604 //
605
606 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
607
608 extern void *__CFLookupCFNetworkFunction(const char *name);
609
610 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...) \
611 typedef R (*dyfuncptr)P; \
612 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
613 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
614 dyfunc = (dyfuncptr)__CFLookupCFNetworkFunction(#N); } \
615 if (dyfunc) { return dyfunc A ; } \
616 return __VA_ARGS__ ; \
617 }
618
619 #define DEFINE_WEAK_CFNETWORK_FUNC_FAIL(R, N, P, A, FAILACTION, ...) \
620 static R __CFNetwork_ ## N P { \
621 typedef R (*dyfuncptr)P; \
622 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
623 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
624 dyfunc = (dyfuncptr)__CFLookupCFNetworkFunction(#N); } \
625 if (dyfunc) { return dyfunc A ; } \
626 FAILACTION ; \
627 return __VA_ARGS__ ; \
628 }
629
630 #else
631 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...)
632 #define DEFINE_WEAK_CFNETWORK_FUNC_FAIL(R, N, P, A, ...)
633 #endif
634
635 #define DEFINE_WEAK_CARBONCORE_FUNC(R, N, P, A, ...)
636
637 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
638
639 extern void *__CFLookupCoreServicesInternalFunction(const char *name);
640
641 #define DEFINE_WEAK_CORESERVICESINTERNAL_FUNC(R, N, P, A, ...) \
642 static R __CFCoreServicesInternal_ ## N P { \
643 typedef R (*dyfuncptr)P; \
644 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
645 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
646 dyfunc = (dyfuncptr)__CFLookupCoreServicesInternalFunction(#N); \
647 } \
648 if (dyfunc) { \
649 return dyfunc A ; \
650 } \
651 return __VA_ARGS__ ; \
652 }
653
654 #else
655 #define DEFINE_WEAK_CORESERVICESINTERNAL_FUNC(R, N, P, A, ...)
656 #endif
657
658 CF_PRIVATE CFComparisonResult _CFCompareStringsWithLocale(CFStringInlineBuffer *str1, CFRange str1Range, CFStringInlineBuffer *str2, CFRange str2Range, CFOptionFlags options, const void *compareLocale);
659
660
661 CF_PRIVATE CFArrayRef _CFBundleCopyUserLanguages();
662
663
664 // This should only be used in CF types, not toll-free bridged objects!
665 // It should not be used with CFAllocator arguments!
666 // Use CFGetAllocator() in the general case, and this inline function in a few limited (but often called) situations.
667 CF_INLINE CFAllocatorRef __CFGetAllocator(CFTypeRef cf) { // !!! Use with CF types only, and NOT WITH CFAllocator!
668 #if OBJC_HAVE_TAGGED_POINTERS
669 if (_objc_isTaggedPointer(cf)) {
670 return kCFAllocatorSystemDefault;
671 }
672 #endif
673 if (__builtin_expect(__CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 7, 7), 1)) {
674 return kCFAllocatorSystemDefault;
675 }
676 return *(CFAllocatorRef *)((char *)cf - sizeof(CFAllocatorRef));
677 }
678
679 /* !!! Avoid #importing objc.h; e.g. converting this to a .m file */
680 struct __objcFastEnumerationStateEquivalent {
681 unsigned long state;
682 unsigned long *itemsPtr;
683 unsigned long *mutationsPtr;
684 unsigned long extra[5];
685 };
686
687 #if 0
688 #pragma mark -
689 #pragma mark Windows Compatability
690 #endif
691
692 // Need to use the _O_BINARY flag on Windows to get the correct behavior
693 #if DEPLOYMENT_TARGET_WINDOWS
694 #define CF_OPENFLGS (_O_BINARY|_O_NOINHERIT)
695 #else
696 #define CF_OPENFLGS (0)
697 #endif
698
699 #if DEPLOYMENT_TARGET_WINDOWS
700
701 // These are replacements for pthread calls on Windows
702 CF_EXPORT int _NS_pthread_main_np();
703 CF_EXPORT int _NS_pthread_setspecific(pthread_key_t key, const void *val);
704 CF_EXPORT void* _NS_pthread_getspecific(pthread_key_t key);
705 CF_EXPORT int _NS_pthread_key_init_np(int key, void (*destructor)(void *));
706 CF_EXPORT void _NS_pthread_setname_np(const char *name);
707
708 // map use of pthread_set/getspecific to internal API
709 #define pthread_setspecific _NS_pthread_setspecific
710 #define pthread_getspecific _NS_pthread_getspecific
711 #define pthread_key_init_np _NS_pthread_key_init_np
712 #define pthread_main_np _NS_pthread_main_np
713 #define pthread_setname_np _NS_pthread_setname_np
714 #endif
715
716 #if DEPLOYMENT_TARGET_WINDOWS
717 // replacement for DISPATCH_QUEUE_OVERCOMMIT until we get a bug fix in dispatch on Windows
718 // <rdar://problem/7923891> dispatch on Windows: Need queue_private.h
719 #define DISPATCH_QUEUE_OVERCOMMIT 2
720 #endif
721
722 #if DEPLOYMENT_TARGET_WINDOWS
723 CF_PRIVATE const wchar_t *_CFDLLPath(void);
724 #endif
725
726 /* Buffer size for file pathname */
727 #if DEPLOYMENT_TARGET_WINDOWS
728 #define CFMaxPathSize ((CFIndex)262)
729 #define CFMaxPathLength ((CFIndex)260)
730 #define PATH_SEP '\\'
731 #define PATH_SEP_STR CFSTR("\\")
732 #define PATH_MAX MAX_PATH
733 #else
734 #define CFMaxPathSize ((CFIndex)1026)
735 #define CFMaxPathLength ((CFIndex)1024)
736 #define PATH_SEP '/'
737 #define PATH_SEP_STR CFSTR("/")
738 #endif
739
740 CF_INLINE const char *CFPathRelativeToAppleFrameworksRoot(const char *path, Boolean *allocated) {
741 if (path) {
742 const char *platformRoot = __CFgetenv("APPLE_FRAMEWORKS_ROOT");
743 if (platformRoot) {
744 char *newPath = NULL;
745 asprintf(&newPath, "%s%s", platformRoot, path);
746 if (allocated && newPath) {
747 *allocated = true;
748 }
749 return newPath;
750 }
751 }
752 if (allocated) {
753 *allocated = false;
754 }
755 return path;
756 }
757
758 #include <dispatch/dispatch.h>
759 #include <dispatch/private.h>
760
761 #if (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED)
762
763 // Returns a generic dispatch queue for when you want to just throw some work
764 // into the concurrent pile to execute, and don't care about specifics except
765 // to match the QOS of the main thread.
766 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericMatchingMain(void) {
767 return dispatch_get_global_queue(qos_class_main(), DISPATCH_QUEUE_OVERCOMMIT);
768 }
769
770 // Returns a generic dispatch queue for when you want to just throw some work
771 // into the concurrent pile to execute, and don't care about specifics except
772 // to match the QOS of the current thread.
773 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericMatchingCurrent(void) {
774 return dispatch_get_global_queue(qos_class_self(), 0); // DISPATCH_QUEUE_OVERCOMMIT left out intentionally at this point
775 }
776
777 // Returns a generic dispatch queue for when you want to just throw some work
778 // into the concurrent pile to execute, and don't care about specifics except
779 // that it should be in background QOS.
780 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericBackground(void) {
781 // Don't ACTUALLY use BACKGROUND, because of unknowable and unfavorable interactions like (<rdar://problem/16319229>)
782 return dispatch_get_global_queue(QOS_CLASS_UTILITY, DISPATCH_QUEUE_OVERCOMMIT);
783 }
784
785 #else
786
787 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericMatchingMain(void) {
788 return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, DISPATCH_QUEUE_OVERCOMMIT);
789 }
790
791 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericMatchingCurrent(void) {
792 return dispatch_get_global_queue(QOS_CLASS_USER_INTERACTIVE, 0); // DISPATCH_QUEUE_OVERCOMMIT left out intentionally at this point
793 }
794
795 CF_INLINE dispatch_queue_t __CFDispatchQueueGetGenericBackground(void) {
796 return dispatch_get_global_queue(QOS_CLASS_UTILITY, DISPATCH_QUEUE_OVERCOMMIT);
797 }
798
799 #endif
800
801
802 CF_EXTERN_C_END
803
804 #endif /* ! __COREFOUNDATION_CFINTERNAL__ */
805