]> git.saurik.com Git - apple/cf.git/blame - Base.subproj/CFInternal.h
CF-368.28.tar.gz
[apple/cf.git] / Base.subproj / CFInternal.h
CommitLineData
9ce05555 1/*
d8925383 2 * Copyright (c) 2005 Apple Computer, Inc. All rights reserved.
9ce05555
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9ce05555
A
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
d8925383 24 Copyright (c) 1998-2005, Apple, Inc. All rights reserved.
9ce05555
A
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>
d8925383 43#include <CoreFoundation/CFRunLoop.h>
9ce05555
A
44#include "CFRuntime.h"
45#if defined(__MACH__)
d8925383 46#include <xlocale.h>
9ce05555 47#include <mach/thread_switch.h>
d8925383 48#include <mach/mach_time.h>
9ce05555
A
49#endif
50#if defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
51#include <sys/time.h>
d8925383 52#include <pthread.h>
9ce05555
A
53#endif
54#include <limits.h>
55#include <pthread.h>
d8925383
A
56#include "auto_stubs.h"
57#include <libkern/OSAtomic.h>
58
0ae65c4b 59
d8925383
A
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"
9ce05555
A
72
73#if !defined(__MACH__)
74#define __private_extern__
75#endif
76
77CF_EXPORT char **_CFArgv(void);
78CF_EXPORT int _CFArgc(void);
79
80CF_EXPORT const char *_CFProcessName(void);
81CF_EXPORT CFStringRef _CFProcessNameString(void);
82
83CF_EXPORT Boolean _CFIsCFM(void);
84
85CF_EXPORT Boolean _CFGetCurrentDirectory(char *path, int maxlen);
86
87CF_EXPORT CFStringRef _CFGetUserName(void);
88CF_EXPORT CFStringRef _CFStringCreateHostName(void);
89
90CF_EXPORT void __CFSetNastyFile(CFTypeRef cf);
91
92CF_EXPORT void _CFMachPortInstallNotifyPort(CFRunLoopRef rl, CFStringRef mode);
93
d8925383
A
94#if defined(__ppc__) || defined(__ppc64__)
95 #define HALT asm __volatile__("trap")
9ce05555 96#elif defined(__i386__)
d8925383
A
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
9ce05555
A
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)
133extern 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. */
d8925383 143/* In the following, N1 and N2 specify an inclusive range N2..N1 with N1 >= N2 */
9ce05555
A
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
153typedef 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
161extern __CFThreadSpecificData *__CFGetThreadSpecificData(void);
d8925383 162__private_extern__ void __CFFinalizeThreadData(void *arg);
9ce05555
A
163
164#if defined(__MACH__) || defined(__LINUX__) || defined(__FREEBSD__)
165extern pthread_key_t __CFTSDKey;
166#endif
167#if defined(__WIN32__)
168extern DWORD __CFTSDKey;
169#endif
170
171//extern void *pthread_getspecific(pthread_key_t key);
172
173CF_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
183CF_EXPORT void CFLog(int p, CFStringRef str, ...);
184
185#define __kCFAllocatorTypeID_CONST 2
186
187CF_INLINE CFAllocatorRef __CFGetDefaultAllocator(void) {
188 CFAllocatorRef allocator = __CFGetThreadSpecificData_inline()->_allocator;
189 if (NULL == allocator) {
190 allocator = kCFAllocatorSystemDefault;
191 }
192 return allocator;
193}
194
195extern 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.
200CF_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);
d8925383 202 if (__builtin_expect(__CFBitfieldGetValue(((const CFRuntimeBase *)cf)->_info, 7, 7), 1)) {
9ce05555
A
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
242CF_EXPORT CFAllocatorRef _CFTemporaryMemoryAllocator(void);
243
244extern SInt64 __CFTimeIntervalToTSR(CFTimeInterval ti);
245extern CFTimeInterval __CFTSRToTimeInterval(SInt64 tsr);
9ce05555
A
246
247extern CFStringRef __CFCopyFormattingDescription(CFTypeRef cf, CFDictionaryRef formatOptions);
248
249/* result is long long or int, depending on doLonglong
250*/
251extern Boolean __CFStringScanInteger(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, Boolean doLonglong, void *result);
252extern Boolean __CFStringScanDouble(CFStringInlineBuffer *buf, CFDictionaryRef locale, SInt32 *indexPtr, double *resultPtr);
253extern Boolean __CFStringScanHex(CFStringInlineBuffer *buf, SInt32 *indexPtr, unsigned *result);
254
255
d8925383 256#ifdef __CONSTANT_CFSTRINGS__
9ce05555 257#define CONST_STRING_DECL(S, V) const CFStringRef S = __builtin___CFStringMakeConstantString(V);
d8925383 258#else
9ce05555 259
d8925383
A
260struct CF_CONST_STRING {
261 CFRuntimeBase _base;
262 uint8_t *_ptr;
263 uint32_t _length;
264};
265
266extern 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) \
271static struct CF_CONST_STRING __ ## S ## __ = {{&__CFConstantStringClassReference, 0x0000, 0x07c8}, V, sizeof(V) - 1}; \
272const CFStringRef S = (CFStringRef) & __ ## S ## __;
273#elif defined(__i386__)
274#define CONST_STRING_DECL(S, V) \
275static struct CF_CONST_STRING __ ## S ## __ = {{&__CFConstantStringClassReference, 0x07c8, 0x0000}, V, sizeof(V) - 1}; \
276const CFStringRef S = (CFStringRef) & __ ## S ## __;
277#else
278#error undefined architecture
279#endif
280#endif
9ce05555
A
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__)
301extern bool __CFOASafe;
302extern void __CFSetLastAllocationEventName(void *ptr, const char *classname);
303#else
304#define __CFOASafe 0
305#define __CFSetLastAllocationEventName(a, b)
306#endif
307
308
309CF_EXPORT CFStringRef _CFCreateLimitedUniqueString(void);
310
311extern 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. */
314CF_EXPORT CFIndex CFBSearch(const void *element, CFIndex elementSize, const void *list, CFIndex count, CFComparatorFunction comparator, void *context);
315
316CF_EXPORT CFHashCode CFHashBytes(UInt8 *bytes, CFIndex length);
317
318CF_EXPORT CFStringEncoding CFStringFileSystemEncoding(void);
319
320CF_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
322extern const void *__CFTypeCollectionRetain(CFAllocatorRef allocator, const void *ptr);
323extern void __CFTypeCollectionRelease(CFAllocatorRef allocator, const void *ptr);
324
9ce05555
A
325
326#if defined(__MACH__)
327
d8925383
A
328typedef OSSpinLock CFSpinLock_t;
329
330#define CFSpinLockInit OS_SPINLOCK_INIT
331
332#if defined(__i386__)
9ce05555
A
333extern void _spin_lock(CFSpinLock_t *lockp);
334extern void _spin_unlock(CFSpinLock_t *lockp);
d8925383
A
335#define OSSpinLockLock(p) _spin_lock(p)
336#define OSSpinLockUnlock(p) _spin_unlock(p)
337#endif
9ce05555
A
338
339CF_INLINE void __CFSpinLock(CFSpinLock_t *lockp) {
d8925383 340 OSSpinLockLock(lockp);
9ce05555
A
341}
342
343CF_INLINE void __CFSpinUnlock(CFSpinLock_t *lockp) {
d8925383
A
344 OSSpinLockUnlock(lockp);
345}
346
347#elif defined(__WIN32__)
348
349typedef LONG CFSpinLock_t;
350
351CF_INLINE void __CFSpinLock(CFSpinLock_t *slock) {
352 while (InterlockedExchange(slock, 1) != 0) {
353 Sleep(1); // 1ms
354 }
355}
356
357CF_INLINE void __CFSpinUnlock(CFSpinLock_t *lock) {
358 *lock = 0;
9ce05555
A
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
d8925383 369#if defined(__svr4__) || defined(__hpux__) || defined(__WIN32__)
9ce05555 370#include <errno.h>
9ce05555
A
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
378extern 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
383CF_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
388CF_EXPORT Boolean _CFCreateDirectory(const char *path);
389CF_EXPORT Boolean _CFRemoveDirectory(const char *path);
390CF_EXPORT Boolean _CFDeleteFile(const char *path);
391
392CF_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
396CF_EXPORT Boolean _CFWriteBytesToFile(CFURLRef url, const void *bytes, CFIndex length);
397#if defined(__MACH__)
398CF_EXPORT Boolean _CFWriteBytesToFileWithAtomicity(CFURLRef url, const void *bytes, unsigned int length, SInt32 mode, Boolean atomic);
399#endif
400
401CF_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
408CF_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
416CF_EXPORT Boolean _CFIsAbsolutePath(UniChar *unichars, CFIndex length);
417CF_EXPORT Boolean _CFStripTrailingPathSlashes(UniChar *unichars, CFIndex *length);
418CF_EXPORT Boolean _CFAppendPathComponent(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *component, CFIndex componentLength);
419CF_EXPORT Boolean _CFAppendPathExtension(UniChar *unichars, CFIndex *length, CFIndex maxLength, UniChar *extension, CFIndex extensionLength);
420CF_EXPORT Boolean _CFTransmutePathSlashes(UniChar *unichars, CFIndex *length, UniChar replSlash);
421CF_EXPORT CFIndex _CFStartOfLastPathComponent(UniChar *unichars, CFIndex length);
422CF_EXPORT CFIndex _CFLengthAfterDeletingLastPathComponent(UniChar *unichars, CFIndex length);
423CF_EXPORT CFIndex _CFStartOfPathExtension(UniChar *unichars, CFIndex length);
424CF_EXPORT CFIndex _CFLengthAfterDeletingPathExtension(UniChar *unichars, CFIndex length);
425
426#if !defined(__MACH__)
427
428#define CF_IS_OBJC(typeID, obj) (false)
429
d8925383
A
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
9ce05555
A
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)
d8925383 443#define CF_OBJC_FUNCDISPATCH5(typeID, rettype, obj, sel, a1, a2, a3, a4, a5)
9ce05555
A
444
445#endif
446
447#if defined(__LINUX__) || defined(__FREEBSD__) || defined(__WIN32__)
448#define __CFISAForTypeID(x) (NULL)
449#endif
450
d8925383 451#define __CFMaxRuntimeTypes 256
9ce05555 452
d8925383 453#if defined(__MACH__)
9ce05555 454
d8925383 455#include <objc/objc-class.h>
9ce05555
A
456
457extern struct objc_class *__CFRuntimeObjCClassTable[];
458CF_INLINE void *__CFISAForTypeID(CFTypeID typeID) {
459 return (void *)(__CFRuntimeObjCClassTable[typeID]);
460}
461
d8925383
A
462#if defined(__ppc__)
463#define __CFSendObjCMsg 0xfffeff00
464#else
9ce05555 465extern void * (*__CFSendObjCMsg)(const void *, SEL, ...);
d8925383 466#endif
9ce05555 467
d8925383 468#if 0
9ce05555
A
469// Although it might seem to make better performance to check for NULL
470// first, doing the other check first is better.
471CF_INLINE int CF_IS_OBJC(CFTypeID typeID, const void *obj) {
472 return (((CFRuntimeBase *)obj)->_isa != __CFISAForTypeID(typeID) && ((CFRuntimeBase *)obj)->_isa > (void *)0xFFF);
473}
d8925383
A
474#else
475#define CF_IS_OBJC(typeID, obj) (false)
476#endif
9ce05555 477
d8925383
A
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
9ce05555 510#define CF_OBJC_FUNCDISPATCH0(typeID, rettype, obj, sel) \
d8925383 511 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
9ce05555 512 {rettype (*func)(const void *, SEL) = (void *)__CFSendObjCMsg; \
d8925383 513 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
9ce05555
A
514 return func((const void *)obj, s);}
515#define CF_OBJC_FUNCDISPATCH1(typeID, rettype, obj, sel, a1) \
d8925383 516 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
9ce05555 517 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
d8925383 518 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
9ce05555
A
519 return func((const void *)obj, s, (a1));}
520#define CF_OBJC_FUNCDISPATCH2(typeID, rettype, obj, sel, a1, a2) \
d8925383 521 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
9ce05555 522 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
d8925383 523 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
9ce05555
A
524 return func((const void *)obj, s, (a1), (a2));}
525#define CF_OBJC_FUNCDISPATCH3(typeID, rettype, obj, sel, a1, a2, a3) \
d8925383 526 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
9ce05555 527 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
d8925383 528 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
9ce05555
A
529 return func((const void *)obj, s, (a1), (a2), (a3));}
530#define CF_OBJC_FUNCDISPATCH4(typeID, rettype, obj, sel, a1, a2, a3, a4) \
d8925383 531 if (__builtin_expect(CF_IS_OBJC(typeID, obj), 0)) \
9ce05555 532 {rettype (*func)(const void *, SEL, ...) = (void *)__CFSendObjCMsg; \
d8925383 533 static SEL s = NULL; if (!s) s = sel_registerName(sel); \
9ce05555 534 return func((const void *)obj, s, (a1), (a2), (a3), (a4));}
d8925383
A
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));}
9ce05555
A
540
541#endif
542
543/* See comments in CFBase.c
544*/
545#if defined(__ppc__) && defined(__MACH__)
546extern void __CF_FAULT_CALLBACK(void **ptr);
547extern void *__CF_INVOKE_CALLBACK(void *, ...);
548
549#define FAULT_CALLBACK(V) __CF_FAULT_CALLBACK(V)
d8925383
A
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))
9ce05555
A
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
d8925383
A
564#if defined(__MACH__)
565
566/* For the support of functionality which needs CarbonCore or other frameworks */
567extern void *__CFLookupCarbonCoreFunction(const char *name);
568extern 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, ...) \
588static 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, ...) \
596static 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
9ce05555
A
610
611__private_extern__ CFArrayRef _CFBundleCopyUserLanguages(Boolean useBackstops);
612
d8925383
A
613/* GC related internal SPIs. */
614extern malloc_zone_t *__CFCollectableZone;
615__private_extern__ void _CFStorageSetWeak(struct __CFStorage *storage);
9ce05555 616
d8925383
A
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
624CF_EXPORT CFAbsoluteTime _CFAbsoluteTimeFromFileTime(const FILETIME *ft);
625#endif
626
627#endif /* ! __COREFOUNDATION_CFINTERNAL__ */