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