]> git.saurik.com Git - apple/cf.git/blob - CFInternal.h
CF-744.tar.gz
[apple/cf.git] / CFInternal.h
1 /*
2 * Copyright (c) 2012 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-2012, 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 #define __CF_COMPILE_YEAR__ (__DATE__[7] * 1000 + __DATE__[8] * 100 + __DATE__[9] * 10 + __DATE__[10] - 53328)
40 #define __CF_COMPILE_MONTH__ ((__DATE__[1] + __DATE__[2] == 207) ? 1 : \
41 (__DATE__[1] + __DATE__[2] == 199) ? 2 : \
42 (__DATE__[1] + __DATE__[2] == 211) ? 3 : \
43 (__DATE__[1] + __DATE__[2] == 226) ? 4 : \
44 (__DATE__[1] + __DATE__[2] == 218) ? 5 : \
45 (__DATE__[1] + __DATE__[2] == 227) ? 6 : \
46 (__DATE__[1] + __DATE__[2] == 225) ? 7 : \
47 (__DATE__[1] + __DATE__[2] == 220) ? 8 : \
48 (__DATE__[1] + __DATE__[2] == 213) ? 9 : \
49 (__DATE__[1] + __DATE__[2] == 215) ? 10 : \
50 (__DATE__[1] + __DATE__[2] == 229) ? 11 : \
51 (__DATE__[1] + __DATE__[2] == 200) ? 12 : 0)
52 #define __CF_COMPILE_DAY__ (__DATE__[4] * 10 + __DATE__[5] - (__DATE__[4] == ' ' ? 368 : 528))
53 #define __CF_COMPILE_DATE__ (__CF_COMPILE_YEAR__ * 10000 + __CF_COMPILE_MONTH__ * 100 + __CF_COMPILE_DAY__)
54
55 #define __CF_COMPILE_HOUR__ (__TIME__[0] * 10 + __TIME__[1] - 528)
56 #define __CF_COMPILE_MINUTE__ (__TIME__[3] * 10 + __TIME__[4] - 528)
57 #define __CF_COMPILE_SECOND__ (__TIME__[6] * 10 + __TIME__[7] - 528)
58 #define __CF_COMPILE_TIME__ (__CF_COMPILE_HOUR__ * 10000 + __CF_COMPILE_MINUTE__ * 100 + __CF_COMPILE_SECOND__)
59
60 #define __CF_COMPILE_SECOND_OF_DAY__ (__CF_COMPILE_HOUR__ * 3600 + __CF_COMPILE_MINUTE__ * 60 + __CF_COMPILE_SECOND__)
61
62 // __CF_COMPILE_DAY_OF_EPOCH__ works within Gregorian years 2001 - 2099; the epoch is of course CF's epoch
63 #define __CF_COMPILE_DAY_OF_EPOCH__ ((__CF_COMPILE_YEAR__ - 2001) * 365 + (__CF_COMPILE_YEAR__ - 2001) / 4 \
64 + ((__DATE__[1] + __DATE__[2] == 207) ? 0 : \
65 (__DATE__[1] + __DATE__[2] == 199) ? 31 : \
66 (__DATE__[1] + __DATE__[2] == 211) ? 59 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
67 (__DATE__[1] + __DATE__[2] == 226) ? 90 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
68 (__DATE__[1] + __DATE__[2] == 218) ? 120 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
69 (__DATE__[1] + __DATE__[2] == 227) ? 151 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
70 (__DATE__[1] + __DATE__[2] == 225) ? 181 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
71 (__DATE__[1] + __DATE__[2] == 220) ? 212 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
72 (__DATE__[1] + __DATE__[2] == 213) ? 243 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
73 (__DATE__[1] + __DATE__[2] == 215) ? 273 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
74 (__DATE__[1] + __DATE__[2] == 229) ? 304 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
75 (__DATE__[1] + __DATE__[2] == 200) ? 334 + (__CF_COMPILE_YEAR__ % 4 == 0) : \
76 365 + (__CF_COMPILE_YEAR__ % 4 == 0)) \
77 + __CF_COMPILE_DAY__)
78
79
80 CF_EXTERN_C_BEGIN
81
82 #include <CoreFoundation/CFBase.h>
83 #include <CoreFoundation/CFURL.h>
84 #include <CoreFoundation/CFString.h>
85 #include <CoreFoundation/CFDate.h>
86 #include <CoreFoundation/CFArray.h>
87 #include <CoreFoundation/CFLogUtilities.h>
88 #include <CoreFoundation/CFRuntime.h>
89 #include <limits.h>
90 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_LINUX
91 #include <xlocale.h>
92 #include <unistd.h>
93 #include <sys/time.h>
94 #include <signal.h>
95 #endif
96 #include <pthread.h>
97
98
99 #if defined(__BIG_ENDIAN__)
100 #define __CF_BIG_ENDIAN__ 1
101 #define __CF_LITTLE_ENDIAN__ 0
102 #endif
103
104 #if defined(__LITTLE_ENDIAN__)
105 #define __CF_LITTLE_ENDIAN__ 1
106 #define __CF_BIG_ENDIAN__ 0
107 #endif
108
109
110 #include <CoreFoundation/ForFoundationOnly.h>
111
112 CF_EXPORT const char *_CFProcessName(void);
113 CF_EXPORT CFStringRef _CFProcessNameString(void);
114
115 CF_EXPORT Boolean _CFGetCurrentDirectory(char *path, int maxlen);
116
117 CF_EXPORT CFArrayRef _CFGetWindowsBinaryDirectories(void);
118
119 CF_EXPORT CFStringRef _CFStringCreateHostName(void);
120
121 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
122 #include <CoreFoundation/CFRunLoop.h>
123 CF_EXPORT void _CFMachPortInstallNotifyPort(CFRunLoopRef rl, CFStringRef mode);
124 #endif
125
126 __private_extern__ CFIndex __CFActiveProcessorCount();
127
128 #if defined(__i386__) || defined(__x86_64__)
129 #if defined(__GNUC__)
130 #define HALT do {asm __volatile__("int3"); kill(getpid(), 9); } while (0)
131 #elif defined(_MSC_VER)
132 #define HALT do { DebugBreak(); abort(); } while (0)
133 #else
134 #error Compiler not supported
135 #endif
136 #endif
137
138
139 #if defined(DEBUG)
140 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
141 do { \
142 if (!(cond)) { \
143 CFLog(prio, CFSTR(desc), a1, a2, a3, a4, a5); \
144 /* HALT; */ \
145 } \
146 } while (0)
147 #else
148 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
149 do {} while (0)
150 #endif
151
152 #define CFAssert(condition, priority, description) \
153 __CFAssert((condition), (priority), description, 0, 0, 0, 0, 0)
154 #define CFAssert1(condition, priority, description, a1) \
155 __CFAssert((condition), (priority), description, (a1), 0, 0, 0, 0)
156 #define CFAssert2(condition, priority, description, a1, a2) \
157 __CFAssert((condition), (priority), description, (a1), (a2), 0, 0, 0)
158 #define CFAssert3(condition, priority, description, a1, a2, a3) \
159 __CFAssert((condition), (priority), description, (a1), (a2), (a3), 0, 0)
160 #define CFAssert4(condition, priority, description, a1, a2, a3, a4) \
161 __CFAssert((condition), (priority), description, (a1), (a2), (a3), (a4), 0)
162
163 #define __kCFLogAssertion 3
164
165 // 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
166 __private_extern__ void _CFLogSimple(int32_t lev, char *format, ...);
167
168 #if defined(DEBUG)
169 extern void __CFGenericValidateType_(CFTypeRef cf, CFTypeID type, const char *func);
170 #define __CFGenericValidateType(cf, type) __CFGenericValidateType_(cf, type, __PRETTY_FUNCTION__)
171 #else
172 #define __CFGenericValidateType(cf, type) ((void)0)
173 #endif
174
175 #define CF_INFO_BITS (!!(__CF_BIG_ENDIAN__) * 3)
176 #define CF_RC_BITS (!!(__CF_LITTLE_ENDIAN__) * 3)
177
178 /* Bit manipulation macros */
179 /* Bits are numbered from 31 on left to 0 on right */
180 /* 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. */
181 /* In the following, N1 and N2 specify an inclusive range N2..N1 with N1 >= N2 */
182 #define __CFBitfieldMask(N1, N2) ((((UInt32)~0UL) << (31UL - (N1) + (N2))) >> (31UL - N1))
183 #define __CFBitfieldGetValue(V, N1, N2) (((V) & __CFBitfieldMask(N1, N2)) >> (N2))
184 #define __CFBitfieldSetValue(V, N1, N2, X) ((V) = ((V) & ~__CFBitfieldMask(N1, N2)) | (((X) << (N2)) & __CFBitfieldMask(N1, N2)))
185 #define __CFBitfieldMaxValue(N1, N2) __CFBitfieldGetValue(0xFFFFFFFFUL, (N1), (N2))
186
187 #define __CFBitIsSet(V, N) (((V) & (1UL << (N))) != 0)
188 #define __CFBitSet(V, N) ((V) |= (1UL << (N)))
189 #define __CFBitClear(V, N) ((V) &= ~(1UL << (N)))
190
191 // Foundation uses 20-40
192 // Foundation knows about the value of __CFTSDKeyAutoreleaseData1
193 enum {
194 __CFTSDKeyAllocator = 1,
195 __CFTSDKeyIsInCFLog = 2,
196 __CFTSDKeyIsInNSCache = 3,
197 __CFTSDKeyIsInGCDMainQ = 4,
198 __CFTSDKeyICUConverter = 7,
199 __CFTSDKeyCollatorLocale = 8,
200 __CFTSDKeyCollatorUCollator = 9,
201 __CFTSDKeyRunLoop = 10,
202 __CFTSDKeyRunLoopCntr = 11,
203 // autorelease pool stuff must be higher than run loop constants
204 __CFTSDKeyAutoreleaseData2 = 61,
205 __CFTSDKeyAutoreleaseData1 = 62,
206 __CFTSDKeyExceptionData = 63,
207 };
208
209 #define __kCFAllocatorTypeID_CONST 2
210
211 CF_INLINE CFAllocatorRef __CFGetDefaultAllocator(void) {
212 CFAllocatorRef allocator = (CFAllocatorRef)_CFGetTSD(__CFTSDKeyAllocator);
213 if (NULL == allocator) {
214 allocator = kCFAllocatorSystemDefault;
215 }
216 return allocator;
217 }
218
219
220 #if !defined(LLONG_MAX)
221 #if defined(_I64_MAX)
222 #define LLONG_MAX _I64_MAX
223 #else
224 #warning Arbitrarily defining LLONG_MAX
225 #define LLONG_MAX (int64_t)9223372036854775807
226 #endif
227 #endif /* !defined(LLONG_MAX) */
228
229 #if !defined(LLONG_MIN)
230 #if defined(_I64_MIN)
231 #define LLONG_MIN _I64_MIN
232 #else
233 #warning Arbitrarily defining LLONG_MIN
234 #define LLONG_MIN (-LLONG_MAX - (int64_t)1)
235 #endif
236 #endif /* !defined(LLONG_MIN) */
237
238 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
239 #define __CFMin(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
240 #define __CFMax(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
241 #else /* __GNUC__ */
242 #define __CFMin(A,B) ((A) < (B) ? (A) : (B))
243 #define __CFMax(A,B) ((A) > (B) ? (A) : (B))
244 #endif /* __GNUC__ */
245
246 /* Secret CFAllocator hint bits */
247 #define __kCFAllocatorTempMemory 0x2
248 #define __kCFAllocatorNoPointers 0x10
249 #define __kCFAllocatorDoNotRecordEvent 0x100
250 #define __kCFAllocatorGCScannedMemory 0x200 /* GC: memory should be scanned. */
251 #define __kCFAllocatorGCObjectMemory 0x400 /* GC: memory needs to be finalized. */
252
253 CF_INLINE auto_memory_type_t CF_GET_GC_MEMORY_TYPE(CFOptionFlags flags) {
254 auto_memory_type_t type = (flags & __kCFAllocatorGCScannedMemory ? 0 : AUTO_UNSCANNED) | (flags & __kCFAllocatorGCObjectMemory ? AUTO_OBJECT : 0);
255 return type;
256 }
257
258 CF_INLINE void __CFAssignWithWriteBarrier(void **location, void *value) {
259 if (kCFUseCollectableAllocator) {
260 objc_assign_strongCast((id)value, (id *)location);
261 } else {
262 *location = value;
263 }
264 }
265
266 // Zero-retain count CFAllocator functions, i.e. memory that will be collected, no dealloc necessary
267 CF_EXPORT void *_CFAllocatorAllocateGC(CFAllocatorRef allocator, CFIndex size, CFOptionFlags hint);
268 CF_EXPORT void *_CFAllocatorReallocateGC(CFAllocatorRef allocator, void *ptr, CFIndex newsize, CFOptionFlags hint);
269 CF_EXPORT void _CFAllocatorDeallocateGC(CFAllocatorRef allocator, void *ptr);
270
271 CF_EXPORT CFAllocatorRef _CFTemporaryMemoryAllocator(void);
272
273 extern SInt64 __CFTimeIntervalToTSR(CFTimeInterval ti);
274 extern CFTimeInterval __CFTSRToTimeInterval(SInt64 tsr);
275
276 extern CFStringRef __CFCopyFormattingDescription(CFTypeRef cf, CFDictionaryRef formatOptions);
277
278 /* Enhanced string formatting support
279 */
280 __private_extern__ CFDictionaryRef _CFStringGetFormatSpecifierConfiguration(CFStringRef aFormatString);
281 __private_extern__ CFStringRef _CFStringCopyWithFomatStringConfiguration(CFStringRef aFormatString, CFDictionaryRef formatConfiguration);
282 __private_extern__ CFStringRef _CFCopyResolvedFormatStringWithConfiguration(CFTypeRef anObject, CFDictionaryRef aConfiguration, CFDictionaryRef formatOptions);
283
284 /* result is long long or int, depending on doLonglong
285 */
286 extern Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result);
287 extern Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFTypeRef locale, SInt32 *indexPtr, double *resultPtr);
288 extern Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result);
289
290 extern const char *__CFgetenv(const char *n);
291
292 // This is really about the availability of C99. We don't have that on Windows, but we should everywhere else.
293 #if DEPLOYMENT_TARGET_WINDOWS
294 #define STACK_BUFFER_DECL(T, N, C) T *N = (T *)_alloca((C) * sizeof(T))
295 #else
296 #define STACK_BUFFER_DECL(T, N, C) T N[C]
297 #endif
298
299
300 CF_EXPORT void * __CFConstantStringClassReferencePtr;
301
302 #ifdef __CONSTANT_CFSTRINGS__
303
304 #define CONST_STRING_DECL(S, V) const CFStringRef S = (const CFStringRef)__builtin___CFStringMakeConstantString(V);
305 #define PE_CONST_STRING_DECL(S, V) __private_extern__ const CFStringRef S = (const CFStringRef)__builtin___CFStringMakeConstantString(V);
306
307 #else
308
309 struct CF_CONST_STRING {
310 CFRuntimeBase _base;
311 uint8_t *_ptr;
312 uint32_t _length;
313 };
314
315 CF_EXPORT int __CFConstantStringClassReference[];
316
317 /* 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 */
318
319 #if __CF_BIG_ENDIAN__
320
321 #define CONST_STRING_DECL(S, V) \
322 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
323 const CFStringRef S = (CFStringRef) & __ ## S ## __;
324 #define PE_CONST_STRING_DECL(S, V) \
325 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0x00, 0x00, 0x07, 0xc8}}, (uint8_t *)V, sizeof(V) - 1}; \
326 __private_extern__ const CFStringRef S = (CFStringRef) & __ ## S ## __;
327
328 #elif __CF_LITTLE_ENDIAN__
329
330 #define CONST_STRING_DECL(S, V) \
331 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
332 const CFStringRef S = (CFStringRef) & __ ## S ## __;
333 #define PE_CONST_STRING_DECL(S, V) \
334 static struct CF_CONST_STRING __ ## S ## __ = {{(uintptr_t)&__CFConstantStringClassReference, {0xc8, 0x07, 0x00, 0x00}}, (uint8_t *)(V), sizeof(V) - 1}; \
335 __private_extern__ const CFStringRef S = (CFStringRef) & __ ## S ## __;
336
337 #endif
338
339 #endif // __CONSTANT_CFSTRINGS__
340
341 CF_EXPORT bool __CFOASafe;
342 CF_EXPORT void __CFSetLastAllocationEventName(void *ptr, const char *classname);
343
344
345
346 /* Comparators are passed the address of the values; this is somewhat different than CFComparatorFunction is used in public API usually. */
347 CF_EXPORT CFIndex CFBSearch(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context);
348
349 CF_EXPORT CFHashCode CFHashBytes(UInt8 *bytes, CFIndex length);
350
351 CF_EXPORT CFStringEncoding CFStringFileSystemEncoding(void);
352
353 __private_extern__ 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);
354
355 extern const void *__CFStringCollectionCopy(CFAllocatorRef allocator, const void *ptr);
356 extern const void *__CFTypeCollectionRetain(CFAllocatorRef allocator, const void *ptr);
357 extern void __CFTypeCollectionRelease(CFAllocatorRef allocator, const void *ptr);
358
359 extern CFTypeRef CFMakeUncollectable(CFTypeRef cf);
360
361 __private_extern__ void _CFRaiseMemoryException(CFStringRef reason);
362
363 __private_extern__ Boolean __CFProphylacticAutofsAccess;
364
365
366 #if DEPLOYMENT_TARGET_MACOSX
367
368 typedef OSSpinLock CFSpinLock_t;
369
370 #define CFSpinLockInit OS_SPINLOCK_INIT
371 #define CF_SPINLOCK_INIT_FOR_STRUCTS(X) (X = CFSpinLockInit)
372
373 #define __CFSpinLock(LP) ({ \
374 OSSpinLock *__lockp__ = (LP); \
375 OSSpinLock __lockv__ = *__lockp__; \
376 if (0 != __lockv__ && ~0 != __lockv__ && (uintptr_t)__lockp__ != (uintptr_t)__lockv__) { \
377 CFLog(3, CFSTR("In '%s', file %s, line %d, during lock, spin lock %p has value 0x%x, which is neither locked nor unlocked. The memory has been smashed."), __PRETTY_FUNCTION__, __FILE__, __LINE__, __lockp__, __lockv__); \
378 /* HALT; */ \
379 } \
380 OSSpinLockLock(__lockp__); })
381
382 #define __CFSpinUnlock(LP) ({ \
383 OSSpinLock *__lockp__ = (LP); \
384 OSSpinLock __lockv__ = *__lockp__; \
385 if (~0 != __lockv__ && (uintptr_t)__lockp__ != (uintptr_t)__lockv__) { \
386 CFLog(3, CFSTR("In '%s', file %s, line %d, during unlock, spin lock %p has value 0x%x, which is not locked. The memory has been smashed or the lock is being unlocked when not locked."), __PRETTY_FUNCTION__, __FILE__, __LINE__, __lockp__, __lockv__); \
387 /* HALT; */ \
388 } \
389 OSSpinLockUnlock(__lockp__); })
390
391 #define __CFSpinLockTry(LP) ({ \
392 OSSpinLock *__lockp__ = (LP); \
393 OSSpinLock __lockv__ = *__lockp__; \
394 if (0 != __lockv__ && ~0 != __lockv__ && (uintptr_t)__lockp__ != (uintptr_t)__lockv__) { \
395 CFLog(3, CFSTR("In '%s', file %s, line %d, during lock, spin lock %p has value 0x%x, which is neither locked nor unlocked. The memory has been smashed."), __PRETTY_FUNCTION__, __FILE__, __LINE__, __lockp__, __lockv__); \
396 /* HALT; */ \
397 } \
398 OSSpinLockTry(__lockp__); })
399
400 #elif DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
401
402 typedef OSSpinLock CFSpinLock_t;
403
404 #define CFSpinLockInit OS_SPINLOCK_INIT
405 #define CF_SPINLOCK_INIT_FOR_STRUCTS(X) (X = CFSpinLockInit)
406
407 #define __CFSpinLock(LP) ({ \
408 OSSpinLock *__lockp__ = (LP); \
409 OSSpinLockLock(__lockp__); })
410
411 #define __CFSpinUnlock(LP) ({ \
412 OSSpinLock *__lockp__ = (LP); \
413 OSSpinLockUnlock(__lockp__); })
414
415 #define __CFSpinLockTry(LP) ({ \
416 OSSpinLock *__lockp__ = (LP); \
417 OSSpinLockTry(__lockp__); })
418
419 #elif DEPLOYMENT_TARGET_WINDOWS
420
421 typedef int32_t CFSpinLock_t;
422 #define CFSpinLockInit 0
423 #define CF_SPINLOCK_INIT_FOR_STRUCTS(X) (X = CFSpinLockInit)
424
425 CF_INLINE void __CFSpinLock(volatile CFSpinLock_t *lock) {
426 while (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) != 0) {
427 Sleep(0);
428 }
429 }
430
431 CF_INLINE void __CFSpinUnlock(volatile CFSpinLock_t *lock) {
432 MemoryBarrier();
433 *lock = 0;
434 }
435
436 CF_INLINE Boolean __CFSpinLockTry(volatile CFSpinLock_t *lock) {
437 return (InterlockedCompareExchange((LONG volatile *)lock, ~0, 0) == 0);
438 }
439
440 #elif DEPLOYMENT_TARGET_LINUX
441
442 typedef int32_t CFSpinLock_t;
443 #define CFSpinLockInit 0
444 #define CF_SPINLOCK_INIT_FOR_STRUCTS(X) (X = CFSpinLockInit)
445
446 CF_INLINE void __CFSpinLock(volatile CFSpinLock_t *lock) {
447 while (__sync_val_compare_and_swap(lock, 0, ~0) != 0) {
448 sleep(0);
449 }
450 }
451
452 CF_INLINE void __CFSpinUnlock(volatile CFSpinLock_t *lock) {
453 __sync_synchronize();
454 *lock = 0;
455 }
456
457 CF_INLINE Boolean __CFSpinLockTry(volatile CFSpinLock_t *lock) {
458 return (__sync_val_compare_and_swap(lock, 0, ~0) == 0);
459 }
460
461 #else
462
463 #warning CF spin locks not defined for this platform -- CF is not thread-safe
464 #define __CFSpinLock(A) do {} while (0)
465 #define __CFSpinUnlock(A) do {} while (0)
466
467 #endif
468
469
470 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
471 extern uint8_t __CF120293;
472 extern uint8_t __CF120290;
473 extern void __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__(void);
474 #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)
475 #define CHECK_FOR_FORK_RET(...) do { CHECK_FOR_FORK(); if (__CF120293) return __VA_ARGS__; } while (0)
476 #define HAS_FORKED() (__CF120293)
477 #endif
478
479 #if !defined(CHECK_FOR_FORK)
480 #define CHECK_FOR_FORK() do { } while (0)
481 #endif
482
483 #if !defined(CHECK_FOR_FORK_RET)
484 #define CHECK_FOR_FORK_RET(...) do { } while (0)
485 #endif
486
487 #if !defined(HAS_FORKED)
488 #define HAS_FORKED() 0
489 #endif
490
491 #include <errno.h>
492
493 #define thread_errno() errno
494 #define thread_set_errno(V) do {errno = (V);} while (0)
495
496 extern void *__CFStartSimpleThread(void *func, void *arg);
497
498 /* ==================== Simple file access ==================== */
499 /* For dealing with abstract types. MF:!!! These ought to be somewhere else and public. */
500
501 CF_EXPORT CFStringRef _CFCopyExtensionForAbstractType(CFStringRef abstractType);
502
503 /* ==================== Simple file access ==================== */
504 /* These functions all act on a c-strings which must be in the file system encoding. */
505
506 CF_EXPORT Boolean _CFCreateDirectory(const char *path);
507 CF_EXPORT Boolean _CFRemoveDirectory(const char *path);
508 CF_EXPORT Boolean _CFDeleteFile(const char *path);
509
510 CF_EXPORT Boolean _CFReadBytesFromPathAndGetFD(CFAllocatorRef alloc, const char *path, void **bytes, CFIndex *length, CFIndex maxLength, int extraOpenFlags, int *fd);
511 CF_EXPORT Boolean _CFReadBytesFromPath(CFAllocatorRef alloc, const char *path, void **bytes, CFIndex *length, CFIndex maxLength, int extraOpenFlags);
512 CF_EXPORT 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_EXPORT CFMutableArrayRef _CFContentsOfDirectory(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_EXPORT 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_EXPORT 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 /* These functions all act on a UniChar buffers. */
536
537 CF_EXPORT UniChar _CFGetSlash();
538 CF_EXPORT Boolean _CFIsAbsolutePath(UniChar *unichars, CFIndex length);
539 CF_EXPORT Boolean _CFStripTrailingPathSlashes(UniChar *unichars, CFIndex *length);
540 __private_extern__ Boolean _CFAppendTrailingPathSlash(UniChar *unichars, CFIndex *length, CFIndex maxLength);
541 CF_EXPORT Boolean _CFAppendPathComponent(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *component, CFIndex componentLength);
542 CF_EXPORT Boolean _CFAppendPathExtension(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *extension, CFIndex extensionLength);
543 CF_EXPORT Boolean _CFTransmutePathSlashes(UniChar *unichars, CFIndex *length, UniChar replSlash);
544 CF_EXPORT CFIndex _CFStartOfLastPathComponent(UniChar *unichars, CFIndex length);
545 CF_EXPORT CFIndex _CFLengthAfterDeletingLastPathComponent(UniChar *unichars, CFIndex length);
546 CF_EXPORT CFIndex _CFStartOfPathExtension(UniChar *unichars, CFIndex length);
547 CF_EXPORT CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFIndex length);
548
549 #define __CFMaxRuntimeTypes 65535
550
551 // Tagged pointer support
552 // Low-bit set means tagged object, next 3 bits (currently)
553 // define the tagged object class, next 4 bits are for type
554 // information for the specific tagged object class. Thus,
555 // the low byte is for type info, and the rest of a pointer
556 // (32 or 64-bit) is for payload, whatever the tagged class.
557 //
558 // Note that the specific integers used to identify the
559 // specific tagged classes can and will change from release
560 // to release (that's why this stuff is in CF*Internal*.h),
561 // as can the definition of type info vs payload above.
562 //
563 #if __LP64__
564 #define CF_IS_TAGGED_OBJ(PTR) ((uintptr_t)(PTR) & 0x1)
565 #define CF_TAGGED_OBJ_TYPE(PTR) ((uintptr_t)(PTR) & 0xF)
566 #else
567 #define CF_IS_TAGGED_OBJ(PTR) 0
568 #define CF_TAGGED_OBJ_TYPE(PTR) 0
569 #endif
570
571 enum {
572 kCFTaggedObjectID_Invalid = 0,
573 kCFTaggedObjectID_Atom = (0 << 1) + 1,
574 kCFTaggedObjectID_Undefined3 = (1 << 1) + 1,
575 kCFTaggedObjectID_Undefined2 = (2 << 1) + 1,
576 kCFTaggedObjectID_Integer = (3 << 1) + 1,
577 kCFTaggedObjectID_DateTS = (4 << 1) + 1,
578 kCFTaggedObjectID_ManagedObjectID = (5 << 1) + 1, // Core Data
579 kCFTaggedObjectID_Date = (6 << 1) + 1,
580 kCFTaggedObjectID_Undefined7 = (7 << 1) + 1,
581 };
582
583
584 #define __CFRuntimeClassTableSize 1024
585
586 extern uintptr_t __CFRuntimeObjCClassTable[];
587 CF_INLINE uintptr_t __CFISAForTypeID(CFTypeID typeID) {
588 return (typeID < __CFRuntimeClassTableSize) ? __CFRuntimeObjCClassTable[typeID] : 0;
589 }
590
591 CF_INLINE Boolean CF_IS_OBJC(CFTypeID typeID, const void *obj) {
592 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
593 if (CF_IS_TAGGED_OBJ(obj)) return true;
594 uintptr_t cfisa = ((CFRuntimeBase *)obj)->_cfisa;
595 if (cfisa == 0) return false;
596 #if 0
597 // Temporarily disabled
598 #if __LP64__
599 if (cfisa < 0x10000000UL) {
600 CFLog(kCFLogLevelWarning, CFSTR("*** Warning: CF tested pointer %p for objectness and found its isa pointer to be bogus (%p)"), obj, cfisa);
601 return false;
602 }
603 #else
604 if (cfisa < 0x1000UL) {
605 CFLog(kCFLogLevelWarning, CFSTR("*** Warning: CF tested pointer %p for objectness and found its isa pointer to be bogus (%p)"), obj, cfisa);
606 return false;
607 }
608 #endif
609 #endif
610 if (cfisa == (uintptr_t)__CFConstantStringClassReferencePtr) return false;
611 uintptr_t type_isa = (uintptr_t)(typeID < __CFRuntimeClassTableSize ? __CFRuntimeObjCClassTable[typeID] : 0);
612 if (cfisa == type_isa) return false;
613 return true;
614 #else
615 return false;
616 #endif
617 }
618
619
620 #define CF_OBJC_FUNCDISPATCHV(typeID, obj, ...) do { } while (0)
621 #define CF_OBJC_CALLV(obj, ...) (0)
622
623
624 /* See comments in CFBase.c
625 */
626 #define FAULT_CALLBACK(V)
627 #define INVOKE_CALLBACK1(P, A) (P)(A)
628 #define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
629 #define INVOKE_CALLBACK3(P, A, B, C) (P)(A, B, C)
630 #define INVOKE_CALLBACK4(P, A, B, C, D) (P)(A, B, C, D)
631 #define INVOKE_CALLBACK5(P, A, B, C, D, E) (P)(A, B, C, D, E)
632 #define UNFAULT_CALLBACK(V) do { } while (0)
633
634 /* For the support of functionality which needs CarbonCore or other frameworks */
635 // These macros define an upcall or weak "symbol-lookup" wrapper function.
636 // The parameters are:
637 // R : the return type of the function
638 // N : the name of the function (in the other library)
639 // P : the parenthesized parameter list of the function
640 // A : the parenthesized actual argument list to be passed
641 // FAILACTION: (only for the _FAIL macros) additional code to be
642 // run when the function cannot be found.
643 // opt: a fifth optional argument can be passed in which is the
644 // return value of the wrapper when the function cannot be
645 // found; should be of type R, & can be a function call
646 // The name of the resulting wrapper function is:
647 // __CFCarbonCore_N (where N is the second parameter)
648 // __CFNetwork_N (where N is the second parameter)
649 //
650 // Example:
651 // DEFINE_WEAK_CARBONCORE_FUNC(void, DisposeHandle, (Handle h), (h))
652 //
653
654 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
655
656 extern void *__CFLookupCFNetworkFunction(const char *name);
657
658 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...) \
659 typedef R (*dyfuncptr)P; \
660 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
661 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
662 dyfunc = (dyfuncptr)__CFLookupCFNetworkFunction(#N); } \
663 if (dyfunc) { return dyfunc A ; } \
664 return __VA_ARGS__ ; \
665 }
666
667 #define DEFINE_WEAK_CFNETWORK_FUNC_FAIL(R, N, P, A, FAILACTION, ...) \
668 static R __CFNetwork_ ## N P { \
669 typedef R (*dyfuncptr)P; \
670 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
671 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
672 dyfunc = (dyfuncptr)__CFLookupCFNetworkFunction(#N); } \
673 if (dyfunc) { return dyfunc A ; } \
674 FAILACTION ; \
675 return __VA_ARGS__ ; \
676 }
677
678 #else
679 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...)
680 #define DEFINE_WEAK_CFNETWORK_FUNC_FAIL(R, N, P, A, ...)
681 #endif
682
683 #define DEFINE_WEAK_CARBONCORE_FUNC(R, N, P, A, ...)
684
685 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
686
687 extern void *__CFLookupCoreServicesInternalFunction(const char *name);
688
689 #define DEFINE_WEAK_CORESERVICESINTERNAL_FUNC(R, N, P, A, ...) \
690 static R __CFCoreServicesInternal_ ## N P { \
691 typedef R (*dyfuncptr)P; \
692 static dyfuncptr dyfunc = (dyfuncptr)(~(uintptr_t)0); \
693 if ((dyfuncptr)(~(uintptr_t)0) == dyfunc) { \
694 dyfunc = (dyfuncptr)__CFLookupCoreServicesInternalFunction(#N); \
695 } \
696 if (dyfunc) { \
697 return dyfunc A ; \
698 } \
699 return __VA_ARGS__ ; \
700 }
701
702 #else
703 #define DEFINE_WEAK_CORESERVICESINTERNAL_FUNC(R, N, P, A, ...)
704 #endif
705
706 __private_extern__ CFComparisonResult _CFCompareStringsWithLocale(CFStringInlineBuffer *str1, CFRange str1Range, CFStringInlineBuffer *str2, CFRange str2Range, CFOptionFlags options, const void *compareLocale);
707
708
709 __private_extern__ CFArrayRef _CFBundleCopyUserLanguages(Boolean useBackstops);
710
711
712 // This should only be used in CF types, not toll-free bridged objects!
713 // It should not be used with CFAllocator arguments!
714 // Use CFGetAllocator() in the general case, and this inline function in a few limited (but often called) situations.
715 CF_INLINE CFAllocatorRef __CFGetAllocator(CFTypeRef cf) { // !!! Use with CF types only, and NOT WITH CFAllocator!
716 if (CF_IS_TAGGED_OBJ(cf)) {
717 return kCFAllocatorSystemDefault;
718 }
719 if (__builtin_expect(__CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_cfinfo[CF_INFO_BITS], 7, 7), 1)) {
720 return kCFAllocatorSystemDefault;
721 }
722 return *(CFAllocatorRef *)((char *)cf - sizeof(CFAllocatorRef));
723 }
724
725 /* !!! Avoid #importing objc.h; e.g. converting this to a .m file */
726 struct __objcFastEnumerationStateEquivalent {
727 unsigned long state;
728 unsigned long *itemsPtr;
729 unsigned long *mutationsPtr;
730 unsigned long extra[5];
731 };
732
733 #pragma mark -
734 #pragma mark Windows Compatability
735
736 // Need to use the _O_BINARY flag on Windows to get the correct behavior
737 #if DEPLOYMENT_TARGET_WINDOWS
738 #define CF_OPENFLGS (_O_BINARY|_O_NOINHERIT)
739 #else
740 #define CF_OPENFLGS (0)
741 #endif
742
743 #if DEPLOYMENT_TARGET_WINDOWS
744
745 // These are replacements for pthread calls on Windows
746 CF_EXPORT int _NS_pthread_main_np();
747 CF_EXPORT int _NS_pthread_setspecific(pthread_key_t key, const void *val);
748 CF_EXPORT void* _NS_pthread_getspecific(pthread_key_t key);
749 CF_EXPORT int _NS_pthread_key_init_np(int key, void (*destructor)(void *));
750 CF_EXPORT void _NS_pthread_setname_np(const char *name);
751
752 // map use of pthread_set/getspecific to internal API
753 #define pthread_setspecific _NS_pthread_setspecific
754 #define pthread_getspecific _NS_pthread_getspecific
755 #define pthread_key_init_np _NS_pthread_key_init_np
756 #define pthread_main_np _NS_pthread_main_np
757 #define pthread_setname_np _NS_pthread_setname_np
758 #endif
759
760 #if DEPLOYMENT_TARGET_WINDOWS
761 // replacement for DISPATCH_QUEUE_OVERCOMMIT until we get a bug fix in dispatch on Windows
762 // <rdar://problem/7923891> dispatch on Windows: Need queue_private.h
763 #define DISPATCH_QUEUE_OVERCOMMIT 2
764 #endif
765
766 #if DEPLOYMENT_TARGET_WINDOWS
767 __private_extern__ const wchar_t *_CFDLLPath(void);
768 #endif
769
770 /* Buffer size for file pathname */
771 #if DEPLOYMENT_TARGET_WINDOWS
772 #define CFMaxPathSize ((CFIndex)262)
773 #define CFMaxPathLength ((CFIndex)260)
774 #define PATH_SEP '\\'
775 #define PATH_SEP_STR CFSTR("\\")
776 #define PATH_MAX MAX_PATH
777 #else
778 #define CFMaxPathSize ((CFIndex)1026)
779 #define CFMaxPathLength ((CFIndex)1024)
780 #define PATH_SEP '/'
781 #define PATH_SEP_STR CFSTR("/")
782 #endif
783
784 CF_EXTERN_C_END
785
786 #endif /* ! __COREFOUNDATION_CFINTERNAL__ */
787