]> git.saurik.com Git - apple/cf.git/blob - Base.subproj/CFInternal.h
471aa924e0fd5794a52fbe1a6212b15d9f084807
[apple/cf.git] / Base.subproj / CFInternal.h
1 /*
2 * Copyright (c) 2005 Apple Computer, 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 /* CFInternal.h
24 Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
25 */
26
27 /*
28 NOT TO BE USED OUTSIDE CF!
29 */
30
31 #if !CF_BUILDING_CF
32 #error The header file CFInternal.h is for the exclusive use of CoreFoundation. No other project should include it.
33 #endif
34
35 #if !defined(__COREFOUNDATION_CFINTERNAL__)
36 #define __COREFOUNDATION_CFINTERNAL__ 1
37
38 #include <CoreFoundation/CFBase.h>
39 #include <CoreFoundation/CFURL.h>
40 #include <CoreFoundation/CFString.h>
41 #include <CoreFoundation/CFDate.h>
42 #include <CoreFoundation/CFArray.h>
43 #include <CoreFoundation/CFRunLoop.h>
44 #include "CFRuntime.h"
45 #if defined(__MACH__)
46 #include <xlocale.h>
47 #include <mach/thread_switch.h>
48 #include <mach/mach_time.h>
49 #endif
50 #if defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
51 #include <sys/time.h>
52 #include <pthread.h>
53 #endif
54 #include <limits.h>
55 #include <pthread.h>
56 #include "auto_stubs.h"
57 #include <libkern/OSAtomic.h>
58
59
60 #if defined(__MACH__)
61 #if defined(__ppc__)
62 // This hack is in here because B&I kernel does not set up comm page with Tiger additions yet.
63 #define AtomicCompareAndSwap(mem, old, new) ({bool result; if (((void **)OSMemoryBarrier)[1] == 0x0) {result = ((*(mem) == (old)) ? (*(mem) = (new), 1) : 0);} else {result = OSAtomicCompareAndSwap32(old, new, (int32_t *)mem); OSMemoryBarrier();} result;})
64 #define AtomicAdd32(mem, val) ({if (((void **)OSMemoryBarrier)[1] == 0x0) {*(mem) += (val);} else {OSAtomicAdd32(val, mem); OSMemoryBarrier();} 0;})
65 #else
66 #define AtomicCompareAndSwap(mem, old, new) ({bool result; result = OSAtomicCompareAndSwap32(old, new, (int32_t *)mem); OSMemoryBarrier(); result;})
67 #define AtomicAdd32(mem, val) ({OSAtomicAdd32(val, mem); OSMemoryBarrier(); 0;})
68 #endif
69 #endif
70
71 #include "ForFoundationOnly.h"
72
73 #if !defined(__MACH__)
74 #define __private_extern__
75 #endif
76
77 CF_EXPORT char **_CFArgv(void);
78 CF_EXPORT int _CFArgc(void);
79
80 CF_EXPORT const char *_CFProcessName(void);
81 CF_EXPORT CFStringRef _CFProcessNameString(void);
82
83 CF_EXPORT Boolean _CFIsCFM(void);
84
85 CF_EXPORT Boolean _CFGetCurrentDirectory(char *path, int maxlen);
86
87 CF_EXPORT CFStringRef _CFGetUserName(void);
88 CF_EXPORT CFStringRef _CFStringCreateHostName(void);
89
90 CF_EXPORT void __CFSetNastyFile(CFTypeRef cf);
91
92 CF_EXPORT void _CFMachPortInstallNotifyPort(CFRunLoopRef rl, CFStringRef mode);
93
94 #if defined(__ppc__) || defined(__ppc64__)
95 #define HALT asm __volatile__("trap")
96 #elif defined(__i386__)
97 #if defined(__GNUC__)
98 #define HALT asm __volatile__("int3")
99 #elif defined(_MSC_VER) || defined(__MWERKS__)
100 #define HALT __asm int 3;
101 #else
102 #error Compiler not supported
103 #endif
104 #endif
105
106 #if defined(DEBUG)
107 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
108 do { \
109 if (!(cond)) { \
110 CFLog(prio, CFSTR(desc), a1, a2, a3, a4, a5); \
111 /* HALT; */ \
112 } \
113 } while (0)
114 #else
115 #define __CFAssert(cond, prio, desc, a1, a2, a3, a4, a5) \
116 do {} while (0)
117 #endif
118
119 #define CFAssert(condition, priority, description) \
120 __CFAssert((condition), (priority), description, 0, 0, 0, 0, 0)
121 #define CFAssert1(condition, priority, description, a1) \
122 __CFAssert((condition), (priority), description, (a1), 0, 0, 0, 0)
123 #define CFAssert2(condition, priority, description, a1, a2) \
124 __CFAssert((condition), (priority), description, (a1), (a2), 0, 0, 0)
125 #define CFAssert3(condition, priority, description, a1, a2, a3) \
126 __CFAssert((condition), (priority), description, (a1), (a2), (a3), 0, 0)
127 #define CFAssert4(condition, priority, description, a1, a2, a3, a4) \
128 __CFAssert((condition), (priority), description, (a1), (a2), (a3), (a4), 0)
129
130 #define __kCFLogAssertion 15
131
132 #if defined(DEBUG)
133 extern void __CFGenericValidateType_(CFTypeRef cf, CFTypeID type, const char *func);
134 #define __CFGenericValidateType(cf, type) __CFGenericValidateType_(cf, type, __PRETTY_FUNCTION__)
135 #else
136 #define __CFGenericValidateType(cf, type)
137 #endif
138
139
140 /* Bit manipulation macros */
141 /* Bits are numbered from 31 on left to 0 on right */
142 /* 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. */
143 /* In the following, N1 and N2 specify an inclusive range N2..N1 with N1 >= N2 */
144 #define __CFBitfieldMask(N1, N2) ((((UInt32)~0UL) << (31UL - (N1) + (N2))) >> (31UL - N1))
145 #define __CFBitfieldGetValue(V, N1, N2) (((V) & __CFBitfieldMask(N1, N2)) >> (N2))
146 #define __CFBitfieldSetValue(V, N1, N2, X) ((V) = ((V) & ~__CFBitfieldMask(N1, N2)) | (((X) << (N2)) & __CFBitfieldMask(N1, N2)))
147 #define __CFBitfieldMaxValue(N1, N2) __CFBitfieldGetValue(0xFFFFFFFFUL, (N1), (N2))
148
149 #define __CFBitIsSet(V, N) (((V) & (1UL << (N))) != 0)
150 #define __CFBitSet(V, N) ((V) |= (1UL << (N)))
151 #define __CFBitClear(V, N) ((V) &= ~(1UL << (N)))
152
153 typedef struct ___CFThreadSpecificData {
154 void *_unused1;
155 void *_allocator;
156 void *_runLoop;
157 int _runLoop_pid;
158 // If you add things to this struct, add cleanup to __CFFinalizeThreadData()
159 } __CFThreadSpecificData;
160
161 extern __CFThreadSpecificData *__CFGetThreadSpecificData(void);
162 __private_extern__ void __CFFinalizeThreadData(void *arg);
163
164 #if defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
165 extern pthread_key_t __CFTSDKey;
166 #endif
167 #if defined(__WIN32__)
168 extern DWORD __CFTSDKey;
169 #endif
170
171 //extern void *pthread_getspecific(pthread_key_t key);
172
173 CF_INLINE __CFThreadSpecificData *__CFGetThreadSpecificData_inline(void) {
174 #if defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
175 __CFThreadSpecificData *data = pthread_getspecific(__CFTSDKey);
176 return data ? data : __CFGetThreadSpecificData();
177 #elif defined(__WIN32__)
178 __CFThreadSpecificData *data = TlsGetValue(__CFTSDKey);
179 return data ? data : __CFGetThreadSpecificData();
180 #endif
181 }
182
183 CF_EXPORT void CFLog(int p, CFStringRef str, ...);
184
185 #define __kCFAllocatorTypeID_CONST 2
186
187 CF_INLINE CFAllocatorRef __CFGetDefaultAllocator(void) {
188 CFAllocatorRef allocator = __CFGetThreadSpecificData_inline()->_allocator;
189 if (NULL == allocator) {
190 allocator = kCFAllocatorSystemDefault;
191 }
192 return allocator;
193 }
194
195 extern CFTypeID __CFGenericTypeID(const void *cf);
196
197 // This should only be used in CF types, not toll-free bridged objects!
198 // It should not be used with CFAllocator arguments!
199 // Use CFGetAllocator() in the general case, and this inline function in a few limited (but often called) situations.
200 CF_INLINE CFAllocatorRef __CFGetAllocator(CFTypeRef cf) { // !!! Use with CF types only, and NOT WITH CFAllocator!
201 CFAssert1(__kCFAllocatorTypeID_CONST != __CFGenericTypeID(cf), __kCFLogAssertion, "__CFGetAllocator(): CFAllocator argument", cf);
202 if (__builtin_expect(__CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_info, 7, 7), 1)) {
203 return kCFAllocatorSystemDefault;
204 }
205 return *(CFAllocatorRef *)((char *)cf - sizeof(CFAllocatorRef));
206 }
207
208 // Don't define a __CFGetCurrentRunLoop(), because even internal clients should go through the real one
209
210
211 #if !defined(LLONG_MAX)
212 #if defined(_I64_MAX)
213 #define LLONG_MAX _I64_MAX
214 #else
215 #warning Arbitrarily defining LLONG_MAX
216 #define LLONG_MAX (int64_t)9223372036854775807
217 #endif
218 #endif /* !defined(LLONG_MAX) */
219
220 #if !defined(LLONG_MIN)
221 #if defined(_I64_MIN)
222 #define LLONG_MIN _I64_MIN
223 #else
224 #warning Arbitrarily defining LLONG_MIN
225 #define LLONG_MIN (-LLONG_MAX - (int64_t)1)
226 #endif
227 #endif /* !defined(LLONG_MIN) */
228
229 #if defined(__GNUC__) && !defined(__STRICT_ANSI__)
230 #define __CFMin(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __a : __b; })
231 #define __CFMax(A,B) ({__typeof__(A) __a = (A); __typeof__(B) __b = (B); __a < __b ? __b : __a; })
232 #else /* __GNUC__ */
233 #define __CFMin(A,B) ((A) < (B) ? (A) : (B))
234 #define __CFMax(A,B) ((A) > (B) ? (A) : (B))
235 #endif /* __GNUC__ */
236
237 /* Secret CFAllocator hint bits */
238 #define __kCFAllocatorTempMemory 0x2
239 #define __kCFAllocatorNoPointers 0x10
240 #define __kCFAllocatorDoNotRecordEvent 0x100
241
242 CF_EXPORT CFAllocatorRef _CFTemporaryMemoryAllocator(void);
243
244 extern SInt64 __CFTimeIntervalToTSR(CFTimeInterval ti);
245 extern CFTimeInterval __CFTSRToTimeInterval(SInt64 tsr);
246
247 extern CFStringRef __CFCopyFormattingDescription(CFTypeRef cf, CFDictionaryRef formatOptions);
248
249 /* result is long long or int, depending on doLonglong
250 */
251 extern Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result);
252 extern Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, double *resultPtr);
253 extern Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result);
254
255
256 #ifdef __CONSTANT_CFSTRINGS__
257 #define CONST_STRING_DECL(S, V) const CFStringRef S = __builtin___CFStringMakeConstantString(V);
258 #else
259
260 struct CF_CONST_STRING {
261 CFRuntimeBase _base;
262 uint8_t *_ptr;
263 uint32_t _length;
264 };
265
266 extern int __CFConstantStringClassReference[];
267
268 /* 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 */
269 #if defined(__ppc__) || defined(__ppc64__)
270 #define CONST_STRING_DECL(S, V) \
271 static struct CF_CONST_STRING __ ## S ## __ = {{&__CFConstantStringClassReference, 0x0000, 0x07c8}, V, sizeof(V) - 1}; \
272 const CFStringRef S = (CFStringRef) & __ ## S ## __;
273 #elif defined(__i386__)
274 #define CONST_STRING_DECL(S, V) \
275 static struct CF_CONST_STRING __ ## S ## __ = {{&__CFConstantStringClassReference, 0x07c8, 0x0000}, V, sizeof(V) - 1}; \
276 const CFStringRef S = (CFStringRef) & __ ## S ## __;
277 #else
278 #error undefined architecture
279 #endif
280 #endif
281
282 #if defined(__MACH__)
283 #define __kCFCharacterSetDir "/System/Library/CoreServices"
284 #elif defined(__LINUX__) || defined(__FREEBSD__)
285 #define __kCFCharacterSetDir "/usr/local/share/CoreFoundation"
286 #elif defined(__WIN32__)
287 #define __kCFCharacterSetDir "\\Windows\\CoreFoundation"
288 #endif
289
290
291 /* Buffer size for file pathname */
292 #if defined(__WIN32__)
293 #define CFMaxPathSize ((CFIndex)262)
294 #define CFMaxPathLength ((CFIndex)260)
295 #else
296 #define CFMaxPathSize ((CFIndex)1026)
297 #define CFMaxPathLength ((CFIndex)1024)
298 #endif
299
300 #if defined(__MACH__)
301 extern bool __CFOASafe;
302 extern void __CFSetLastAllocationEventName(void *ptr, const char *classname);
303 #else
304 #define __CFOASafe 0
305 #define __CFSetLastAllocationEventName(a, b)
306 #endif
307
308
309 CF_EXPORT CFStringRef _CFCreateLimitedUniqueString(void);
310
311 extern CFStringRef __CFCopyEthernetAddrString(void);
312
313 /* Comparators are passed the address of the values; this is somewhat different than CFComparatorFunction is used in public API usually. */
314 CF_EXPORT CFIndex CFBSearch(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context);
315
316 CF_EXPORT CFHashCode CFHashBytes(UInt8 *bytes, CFIndex length);
317
318 CF_EXPORT CFStringEncoding CFStringFileSystemEncoding(void);
319
320 CF_EXPORT 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);
321
322 extern const void *__CFTypeCollectionRetain(CFAllocatorRef allocator, const void *ptr);
323 extern void __CFTypeCollectionRelease(CFAllocatorRef allocator, const void *ptr);
324
325
326 #if defined(__MACH__)
327
328 typedef OSSpinLock CFSpinLock_t;
329
330 #define CFSpinLockInit OS_SPINLOCK_INIT
331
332 #if defined(__i386__)
333 extern void _spin_lock(CFSpinLock_t *lockp);
334 extern void _spin_unlock(CFSpinLock_t *lockp);
335 #define OSSpinLockLock(p) _spin_lock(p)
336 #define OSSpinLockUnlock(p) _spin_unlock(p)
337 #endif
338
339 CF_INLINE void __CFSpinLock(CFSpinLock_t *lockp) {
340 OSSpinLockLock(lockp);
341 }
342
343 CF_INLINE void __CFSpinUnlock(CFSpinLock_t *lockp) {
344 OSSpinLockUnlock(lockp);
345 }
346
347 #elif defined(__WIN32__)
348
349 typedef LONG CFSpinLock_t;
350
351 CF_INLINE void __CFSpinLock(CFSpinLock_t *slock) {
352 while (InterlockedExchange(slock, 1) != 0) {
353 Sleep(1); // 1ms
354 }
355 }
356
357 CF_INLINE void __CFSpinUnlock(CFSpinLock_t *lock) {
358 *lock = 0;
359 }
360
361 #else
362
363 #warning CF spin locks not defined for this platform -- CF is not thread-safe
364 #define __CFSpinLock(A) do {} while (0)
365 #define __CFSpinUnlock(A) do {} while (0)
366
367 #endif
368
369 #if defined(__svr4__) || defined(__hpux__) || defined(__WIN32__)
370 #include <errno.h>
371 #elif defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
372 #include <sys/errno.h>
373 #endif
374
375 #define thread_errno() errno
376 #define thread_set_errno(V) do {errno = (V);} while (0)
377
378 extern void *__CFStartSimpleThread(void *func, void *arg);
379
380 /* ==================== Simple file access ==================== */
381 /* For dealing with abstract types. MF:!!! These ought to be somewhere else and public. */
382
383 CF_EXPORT CFStringRef _CFCopyExtensionForAbstractType(CFStringRef abstractType);
384
385 /* ==================== Simple file access ==================== */
386 /* These functions all act on a c-strings which must be in the file system encoding. */
387
388 CF_EXPORT Boolean _CFCreateDirectory(const char *path);
389 CF_EXPORT Boolean _CFRemoveDirectory(const char *path);
390 CF_EXPORT Boolean _CFDeleteFile(const char *path);
391
392 CF_EXPORT Boolean _CFReadBytesFromFile(CFAllocatorRef alloc, CFURLRef url, void **bytes, CFIndex *length, CFIndex maxLength);
393 /* resulting bytes are allocated from alloc which MUST be non-NULL. */
394 /* maxLength of zero means the whole file. Otherwise it sets a limit on the number of bytes read. */
395
396 CF_EXPORT Boolean _CFWriteBytesToFile(CFURLRef url, const void *bytes, CFIndex length);
397 #if defined(__MACH__)
398 CF_EXPORT Boolean _CFWriteBytesToFileWithAtomicity(CFURLRef url, const void *bytes, unsigned int length, SInt32 mode, Boolean atomic);
399 #endif
400
401 CF_EXPORT CFMutableArrayRef _CFContentsOfDirectory(CFAllocatorRef alloc, char *dirPath, void *dirSpec, CFURLRef dirURL, CFStringRef matchingAbstractType);
402 /* On Mac OS 8/9, one of dirSpec, dirPath and dirURL must be non-NULL */
403 /* On all other platforms, one of path and dirURL must be non-NULL */
404 /* If both are present, they are assumed to be in-synch; that is, they both refer to the same directory. */
405 /* alloc may be NULL */
406 /* return value is CFArray of CFURLs */
407
408 CF_EXPORT SInt32 _CFGetFileProperties(CFAllocatorRef alloc, CFURLRef pathURL, Boolean *exists, SInt32 *posixMode, SInt64 *size, CFDateRef *modTime, SInt32 *ownerID, CFArrayRef *dirContents);
409 /* alloc may be NULL */
410 /* 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. */
411
412
413 /* ==================== Simple path manipulation ==================== */
414 /* These functions all act on a UniChar buffers. */
415
416 CF_EXPORT Boolean _CFIsAbsolutePath(UniChar *unichars, CFIndex length);
417 CF_EXPORT Boolean _CFStripTrailingPathSlashes(UniChar *unichars, CFIndex *length);
418 CF_EXPORT Boolean _CFAppendPathComponent(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *component, CFIndex componentLength);
419 CF_EXPORT Boolean _CFAppendPathExtension(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *extension, CFIndex extensionLength);
420 CF_EXPORT Boolean _CFTransmutePathSlashes(UniChar *unichars, CFIndex *length, UniChar replSlash);
421 CF_EXPORT CFIndex _CFStartOfLastPathComponent(UniChar *unichars, CFIndex length);
422 CF_EXPORT CFIndex _CFLengthAfterDeletingLastPathComponent(UniChar *unichars, CFIndex length);
423 CF_EXPORT CFIndex _CFStartOfPathExtension(UniChar *unichars, CFIndex length);
424 CF_EXPORT CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFIndex length);
425
426 #if !defined(__MACH__)
427
428 #define CF_IS_OBJC(typeID, obj) (false)
429
430 #define CF_OBJC_VOIDCALL0(obj, sel)
431 #define CF_OBJC_VOIDCALL1(obj, sel, a1)
432 #define CF_OBJC_VOIDCALL2(obj, sel, a1, a2)
433
434 #define CF_OBJC_CALL0(rettype, retvar, obj, sel)
435 #define CF_OBJC_CALL1(rettype, retvar, obj, sel, a1)
436 #define CF_OBJC_CALL2(rettype, retvar, obj, sel, a1, a2)
437
438 #define CF_OBJC_FUNCDISPATCH0(typeID, rettype, obj, sel)
439 #define CF_OBJC_FUNCDISPATCH1(typeID, rettype, obj, sel, a1)
440 #define CF_OBJC_FUNCDISPATCH2(typeID, rettype, obj, sel, a1, a2)
441 #define CF_OBJC_FUNCDISPATCH3(typeID, rettype, obj, sel, a1, a2, a3)
442 #define CF_OBJC_FUNCDISPATCH4(typeID, rettype, obj, sel, a1, a2, a3, a4)
443 #define CF_OBJC_FUNCDISPATCH5(typeID, rettype, obj, sel, a1, a2, a3, a4, a5)
444
445 #endif
446
447 #if defined(__LINUX__) || defined(__FREEBSD__) || defined(__WIN32__)
448 #define __CFISAForTypeID(x) (NULL)
449 #endif
450
451 #define __CFMaxRuntimeTypes 256
452
453 #if defined(__MACH__)
454
455 #include <objc/objc-class.h>
456
457 extern struct objc_class *__CFRuntimeObjCClassTable[];
458 CF_INLINE void *__CFISAForTypeID(CFTypeID typeID) {
459 return (void *)(__CFRuntimeObjCClassTable[typeID]);
460 }
461
462 #if defined(__ppc__)
463 #define __CFSendObjCMsg 0xfffeff00
464 #else
465 extern void * (*__CFSendObjCMsg)(const void *, SEL, ...);
466 #endif
467
468 #if 0
469 // Although it might seem to make better performance to check for NULL
470 // first, doing the other check first is better.
471 CF_INLINE int CF_IS_OBJC(CFTypeID typeID, const void *obj) {
472 return (((CFRuntimeBase *)obj)->_isa != __CFISAForTypeID(typeID) && ((CFRuntimeBase *)obj)->_isa > (void *)0xFFF);
473 }
474 #else
475 #define CF_IS_OBJC(typeID, obj) (false)
476 #endif
477
478 // Invoke an ObjC method that returns void.
479 // Assumes CF_IS_OBJC has already been checked.
480 #define CF_OBJC_VOIDCALL0(obj, sel) \
481 {void (*func)(const void *, SEL) = (void *)__CFSendObjCMsg; \
482 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
483 func((const void *)obj, s);}
484 #define CF_OBJC_VOIDCALL1(obj, sel, a1) \
485 {void (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
486 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
487 func((const void *)obj, s, (a1));}
488 #define CF_OBJC_VOIDCALL2(obj, sel, a1, a2) \
489 {void (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
490 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
491 func((const void *)obj, s, (a1), (a2));}
492
493
494 // Invoke an ObjC method, leaving the result in "retvar".
495 // Assumes CF_IS_OBJC has already been checked.
496 #define CF_OBJC_CALL0(rettype, retvar, obj, sel) \
497 {rettype (*func)(const void *, SEL) = (void *)__CFSendObjCMsg; \
498 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
499 retvar = func((const void *)obj, s);}
500 #define CF_OBJC_CALL1(rettype, retvar, obj, sel, a1) \
501 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
502 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
503 retvar = func((const void *)obj, s, (a1));}
504 #define CF_OBJC_CALL2(rettype, retvar, obj, sel, a1, a2) \
505 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
506 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
507 retvar = func((const void *)obj, s, (a1), (a2));}
508
509 // Invoke an ObjC method, return the result
510 #define CF_OBJC_FUNCDISPATCH0(typeID, rettype, obj, sel) \
511 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
512 {rettype (*func)(const void *, SEL) = (void *)__CFSendObjCMsg; \
513 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
514 return func((const void *)obj, s);}
515 #define CF_OBJC_FUNCDISPATCH1(typeID, rettype, obj, sel, a1) \
516 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
517 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
518 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
519 return func((const void *)obj, s, (a1));}
520 #define CF_OBJC_FUNCDISPATCH2(typeID, rettype, obj, sel, a1, a2) \
521 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
522 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
523 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
524 return func((const void *)obj, s, (a1), (a2));}
525 #define CF_OBJC_FUNCDISPATCH3(typeID, rettype, obj, sel, a1, a2, a3) \
526 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
527 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
528 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
529 return func((const void *)obj, s, (a1), (a2), (a3));}
530 #define CF_OBJC_FUNCDISPATCH4(typeID, rettype, obj, sel, a1, a2, a3, a4) \
531 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
532 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
533 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
534 return func((const void *)obj, s, (a1), (a2), (a3), (a4));}
535 #define CF_OBJC_FUNCDISPATCH5(typeID, rettype, obj, sel, a1, a2, a3, a4, a5) \
536 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
537 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
538 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
539 return func((const void *)obj, s, (a1), (a2), (a3), (a4), (a5));}
540
541 #endif
542
543 /* See comments in CFBase.c
544 */
545 #if defined(__ppc__) && defined(__MACH__)
546 extern void __CF_FAULT_CALLBACK(void **ptr);
547 extern void *__CF_INVOKE_CALLBACK(void *, ...);
548
549 #define FAULT_CALLBACK(V) __CF_FAULT_CALLBACK(V)
550 #define INVOKE_CALLBACK1(P, A) (((uintptr_t)(P) & 0x2) ? __CF_INVOKE_CALLBACK(P, A) : (P)(A))
551 #define INVOKE_CALLBACK2(P, A, B) (((uintptr_t)(P) & 0x2) ? __CF_INVOKE_CALLBACK(P, A, B) : (P)(A, B))
552 #define INVOKE_CALLBACK3(P, A, B, C) (((uintptr_t)(P) & 0x2) ? __CF_INVOKE_CALLBACK(P, A, B, C) : (P)(A, B, C))
553 #define INVOKE_CALLBACK4(P, A, B, C, D) (((uintptr_t)(P) & 0x2) ? __CF_INVOKE_CALLBACK(P, A, B, C, D) : (P)(A, B, C, D))
554 #define INVOKE_CALLBACK5(P, A, B, C, D, E) (((uintptr_t)(P) & 0x2) ? __CF_INVOKE_CALLBACK(P, A, B, C, D, E) : (P)(A, B, C, D, E))
555 #else
556 #define FAULT_CALLBACK(V)
557 #define INVOKE_CALLBACK1(P, A) (P)(A)
558 #define INVOKE_CALLBACK2(P, A, B) (P)(A, B)
559 #define INVOKE_CALLBACK3(P, A, B, C) (P)(A, B, C)
560 #define INVOKE_CALLBACK4(P, A, B, C, D) (P)(A, B, C, D)
561 #define INVOKE_CALLBACK5(P, A, B, C, D, E) (P)(A, B, C, D, E)
562 #endif
563
564 #if defined(__MACH__)
565
566 /* For the support of functionality which needs CarbonCore or other frameworks */
567 extern void *__CFLookupCarbonCoreFunction(const char *name);
568 extern void *__CFLookupCFNetworkFunction(const char *name);
569
570 // These macros define an upcall or weak "symbol-lookup" wrapper function.
571 // The parameters are:
572 // R : the return type of the function
573 // N : the name of the function (in the other library)
574 // P : the parenthesized parameter list of the function
575 // A : the parenthesized actual argument list to be passed
576 // opt: a fifth optional argument can be passed in which is the
577 // return value of the wrapper when the function cannot be
578 // found; should be of type R, & can be a function call
579 // The name of the resulting wrapper function is:
580 // __CFCarbonCore_N (where N is the second parameter)
581 // __CFNetwork_N (where N is the second parameter)
582 //
583 // Example:
584 // DEFINE_WEAK_CARBONCORE_FUNC(void, DisposeHandle, (Handle h), (h))
585 //
586
587 #define DEFINE_WEAK_CARBONCORE_FUNC(R, N, P, A, ...) \
588 static R __CFCarbonCore_ ## N P { \
589 static R (*dyfunc) P = (void *)(~(uintptr_t)0); \
590 if ((void *)(~(uintptr_t)0) == dyfunc) { \
591 dyfunc = __CFLookupCarbonCoreFunction(#N); } \
592 return dyfunc ? dyfunc A : (R)(0 , ## __VA_ARGS__); \
593 }
594
595 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...) \
596 static R __CFNetwork_ ## N P { \
597 static R (*dyfunc) P = (void *)(~(uintptr_t)0); \
598 if ((void *)(~(uintptr_t)0) == dyfunc) { \
599 dyfunc = __CFLookupCFNetworkFunction(#N); } \
600 return dyfunc ? dyfunc A : (R)(0 , ## __VA_ARGS__); \
601 }
602
603 #else
604
605 #define DEFINE_WEAK_CARBONCORE_FUNC(R, N, P, A, ...)
606 #define DEFINE_WEAK_CFNETWORK_FUNC(R, N, P, A, ...)
607
608 #endif
609
610
611 __private_extern__ CFArrayRef _CFBundleCopyUserLanguages(Boolean useBackstops);
612
613 /* GC related internal SPIs. */
614 extern malloc_zone_t *__CFCollectableZone;
615 __private_extern__ void _CFStorageSetWeak(struct __CFStorage *storage);
616
617 #if defined(__WIN32__)
618 __private_extern__ const char *_CFDLLPath(void);
619 __private_extern__ void __CFStringCleanup(void);
620 __private_extern__ void __CFSocketCleanup(void);
621 __private_extern__ void __CFUniCharCleanup(void);
622 __private_extern__ void __CFStreamCleanup(void);
623 // We should export this as SPI or API to clients - 3514284
624 CF_EXPORT CFAbsoluteTime _CFAbsoluteTimeFromFileTime(const FILETIME *ft);
625 #endif
626
627 #endif /* ! __COREFOUNDATION_CFINTERNAL__ */