2 * Copyright (c) 2012 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
24 /* CoreFoundation_Prefix.h
25 Copyright (c) 2005-2012, Apple Inc. All rights reserved.
29 #define _DARWIN_UNLIMITED_SELECT 1
31 #include <CoreFoundation/CFBase.h>
38 #if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)
42 #define SystemIntegrityCheck(A, B) do {} while (0)
46 #include <objc/objc.h>
48 typedef signed char BOOL
;
56 #define CRSetCrashLogMessage(A) do {} while (0)
57 #define CRSetCrashLogMessage2(A) do {} while (0)
59 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
60 #import <libkern/OSAtomic.h>
64 /* This macro creates 3 helper functions which are useful in dealing with libdispatch:
65 * __ PREFIX SyncDispatchIsSafe -- returns bool indicating whether calling dispatch_sync() would be safe from self-deadlock
66 * __ PREFIX Queue -- manages and returns a singleton serial queue
68 * Use the macro like this:
69 * DISPATCH_HELPER_FUNCTIONS(fh, NSFileHandle)
72 #define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME) \
73 static Boolean __ ## PREFIX ## SyncDispatchIsSafe(dispatch_queue_t Q) { \
74 dispatch_queue_t C = dispatch_get_current_queue(); \
75 return (!C || Q != C) ? true : false; \
78 static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
79 static volatile dispatch_queue_t __ ## PREFIX ## dq = NULL; \
80 if (!__ ## PREFIX ## dq) { \
81 dispatch_queue_t dq = dispatch_queue_create(# QNAME, NULL); \
82 void * volatile *loc = (void * volatile *)&__ ## PREFIX ## dq; \
83 if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dq, loc)) { \
84 dispatch_release(dq); \
87 return __ ## PREFIX ## dq; \
91 #define LIBAUTO_STUB 1
95 #if DEPLOYMENT_TARGET_MACOSX
96 #include <auto_zone.h>
98 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
99 #include <objc/objc-auto.h>
102 #endif // LIBAUTO_STUB
104 #if DEPLOYMENT_TARGET_WINDOWS
105 // Compatibility with boolean.h
106 #if defined(__x86_64__)
107 typedef unsigned int boolean_t
;
109 typedef int boolean_t
;
113 #if DEPLOYMENT_TARGET_LINUX
115 #define __private_extern__
119 #define strtod_l(a,b,locale) strtod(a,b)
120 #define strtoul_l(a,b,c,locale) strtoul(a,b,c)
121 #define strtol_l(a,b,c,locale) strtol(a,b,c)
122 #define strtoll_l(a,b,c,locale) strtoll(a,b,c)
123 #define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c)
125 #define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
127 #define strlcat(a,b,c) strncat(a,b,c)
128 #define strlcpy(a,b,c) strncpy(a,b,c)
130 #define issetugid() 0
132 // Implemented in CFPlatform.c
133 bool OSAtomicCompareAndSwapPtr(void *oldp
, void *newp
, void *volatile *dst
);
134 bool OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
);
135 bool OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void *volatile *dst
);
137 int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
);
138 int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
);
139 int32_t OSAtomicIncrement32(volatile int32_t *theValue
);
140 int32_t OSAtomicDecrement32(volatile int32_t *theValue
);
142 int32_t OSAtomicAdd32( int32_t theAmount
, volatile int32_t *theValue
);
143 int32_t OSAtomicAdd32Barrier( int32_t theAmount
, volatile int32_t *theValue
);
144 bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue
, int32_t newValue
, volatile int32_t *theValue
);
146 void OSMemoryBarrier();
149 CF_INLINE
size_t malloc_size(void *memblock
) {
150 return malloc_usable_size(memblock
);
155 #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
157 #define MIN(A,B) ((A) < (B) ? (A) : (B))
161 #define MAX(A,B) ((A) > (B) ? (A) : (B))
165 #define ABS(A) ((A) < 0 ? (-(A)) : (A))
169 #if DEPLOYMENT_TARGET_WINDOWS
171 #define MAXPATHLEN MAX_PATH
173 #undef INVALID_HANDLE_VALUE
175 // Defined for source compatibility
178 #define mode_t uint16_t
180 // This works because things aren't actually exported from the DLL unless they have a __declspec(dllexport) on them... so extern by itself is closest to __private_extern__ on Mac OS
181 #define __private_extern__ extern
183 #define __builtin_expect(P1,P2) P1
185 // These are replacements for POSIX calls on Windows, ensuring that the UTF8 parameters are converted to UTF16 before being passed to Windows
186 CF_EXPORT
int _NS_stat(const char *name
, struct _stat
*st
);
187 CF_EXPORT
int _NS_mkdir(const char *name
);
188 CF_EXPORT
int _NS_rmdir(const char *name
);
189 CF_EXPORT
int _NS_chmod(const char *name
, int mode
);
190 CF_EXPORT
int _NS_unlink(const char *name
);
191 CF_EXPORT
char *_NS_getcwd(char *dstbuf
, size_t size
); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
192 CF_EXPORT
char *_NS_getenv(const char *name
);
193 CF_EXPORT
int _NS_rename(const char *oldName
, const char *newName
);
194 CF_EXPORT
int _NS_open(const char *name
, int oflag
, int pmode
= 0);
195 CF_EXPORT
int _NS_chdir(const char *name
);
196 CF_EXPORT
int _NS_mkstemp(char *name
, int bufSize
);
197 CF_EXPORT
int _NS_access(const char *name
, int amode
);
199 #define BOOL WINDOWS_BOOL
201 #define WIN32_LEAN_AND_MEAN
204 #define WINVER 0x0501
208 #define _WIN32_WINNT 0x0501
211 // The order of these includes is important
212 #define FD_SETSIZE 1024
213 #include <winsock2.h>
219 #ifndef HAVE_STRUCT_TIMESPEC
220 #define HAVE_STRUCT_TIMESPEC 1
225 #endif /* HAVE_STRUCT_TIMESPEC */
227 #define __PRETTY_FUNCTION__ __FUNCTION__
229 #define malloc_default_zone() (void *)0
230 #define malloc_zone_from_ptr(a) (void *)0
231 #define malloc_zone_malloc(zone,size) malloc(size)
232 #define malloc_zone_memalign(zone,align,size) malloc(size)
233 #define malloc_zone_calloc(zone,count,size) calloc(count,size)
234 #define bcopy(b1,b2,len) memmove(b2, b1, (size_t)(len))
235 typedef int malloc_zone_t
;
242 #define fsync(a) _commit(a)
243 #define malloc_create_zone(a,b) 123
244 #define malloc_set_zone_name(zone,name)
245 #define malloc_zone_realloc(zone,ptr,size) realloc(ptr,size)
246 #define malloc_zone_free(zone,ptr) free(ptr)
248 // implemented in CFInternal.h
249 #define OSSpinLockLock(A) __CFSpinLock(A)
250 #define OSSpinLockUnlock(A) __CFSpinUnlock(A)
252 typedef int32_t OSSpinLock
;
254 #define OS_SPINLOCK_INIT 0
261 CF_INLINE
size_t malloc_size(void *memblock
) {
262 return _msize(memblock
);
265 CF_INLINE
uint64_t mach_absolute_time() {
267 QueryPerformanceCounter(&count
);
268 // mach_absolute_time is unsigned, but this function returns a signed value.
269 return (uint64_t)count
.QuadPart
;
272 CF_INLINE
long long llabs(long long v
) {
273 if (v
< 0) return -v
;
277 #define strtod_l(a,b,locale) strtod(a,b)
278 #define strtoul_l(a,b,c,locale) strtoul(a,b,c)
279 #define strtol_l(a,b,c,locale) strtol(a,b,c)
280 #define strtoll_l(a,b,c,locale) _strtoi64(a,b,c)
281 #define strncasecmp_l(a, b, c, d) _strnicmp(a, b, c)
282 #define snprintf _snprintf
284 #define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
286 #define strlcat(a,b,c) strncat(a,b,c)
287 #define strlcpy(a,b,c) strncpy(a,b,c)
289 #define sleep(x) Sleep(1000*x)
291 #define issetugid() 0
293 // CF exports these useful atomic operation functions on Windows
294 CF_EXPORT
bool OSAtomicCompareAndSwapPtr(void *oldp
, void *newp
, void *volatile *dst
);
295 CF_EXPORT
bool OSAtomicCompareAndSwapLong(long oldl
, long newl
, long volatile *dst
);
296 CF_EXPORT
bool OSAtomicCompareAndSwapPtrBarrier(void *oldp
, void *newp
, void *volatile *dst
);
298 CF_EXPORT
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst
);
299 CF_EXPORT
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst
);
300 CF_EXPORT
int32_t OSAtomicIncrement32(volatile int32_t *theValue
);
301 CF_EXPORT
int32_t OSAtomicDecrement32(volatile int32_t *theValue
);
303 CF_EXPORT
int32_t OSAtomicAdd32( int32_t theAmount
, volatile int32_t *theValue
);
304 CF_EXPORT
int32_t OSAtomicAdd32Barrier( int32_t theAmount
, volatile int32_t *theValue
);
305 CF_EXPORT
bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue
, int32_t newValue
, volatile int32_t *theValue
);
308 CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
309 CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
311 CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue );
312 CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue );
315 //#ifndef NTDDI_VERSION
316 //#define NTDDI_VERSION NTDDI_WINXP
325 #if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
329 CF_INLINE
int flsl( long mask
) {
331 while (mask
!= 0) mask
= (unsigned long)mask
>> 1, idx
++;
335 CF_INLINE
int popcountll(long long x
) {
339 x
&= x
- 1; // reset LS1B
344 __private_extern__
int asprintf(char **ret
, const char *format
, ...);
352 /* Stubs for functions in libauto. */
354 enum {OBJC_GENERATIONAL
= (1 << 0)};
357 OBJC_RATIO_COLLECTION
= (0 << 0),
358 OBJC_GENERATIONAL_COLLECTION
= (1 << 0),
359 OBJC_FULL_COLLECTION
= (2 << 0),
360 OBJC_EXHAUSTIVE_COLLECTION
= (3 << 0),
361 OBJC_COLLECT_IF_NEEDED
= (1 << 3),
362 OBJC_WAIT_UNTIL_DONE
= (1 << 4),
367 AUTO_TYPE_UNKNOWN
= -1,
368 AUTO_UNSCANNED
= (1 << 0),
369 AUTO_OBJECT
= (1 << 1),
370 AUTO_POINTERS_ONLY
= (1 << 2),
371 AUTO_MEMORY_SCANNED
= !AUTO_UNSCANNED
,
372 AUTO_MEMORY_UNSCANNED
= AUTO_UNSCANNED
,
373 AUTO_MEMORY_ALL_POINTERS
= AUTO_POINTERS_ONLY
,
374 AUTO_MEMORY_ALL_WEAK_POINTERS
= (AUTO_UNSCANNED
| AUTO_POINTERS_ONLY
),
375 AUTO_OBJECT_SCANNED
= AUTO_OBJECT
,
376 AUTO_OBJECT_UNSCANNED
= AUTO_OBJECT
| AUTO_UNSCANNED
,
377 AUTO_OBJECT_ALL_POINTERS
= AUTO_OBJECT
| AUTO_POINTERS_ONLY
379 typedef unsigned long auto_memory_type_t
;
380 typedef struct _auto_zone_t auto_zone_t
;
381 typedef struct auto_weak_callback_block
{
382 struct auto_weak_callback_block
*next
;
383 void (*callback_function
)(void *arg1
, void *arg2
);
386 } auto_weak_callback_block_t
;
388 CF_INLINE
void *objc_memmove_collectable(void *a
, const void *b
, size_t c
) { return memmove(a
, b
, c
); }
389 CF_INLINE
void *objc_collectableZone(void) { return 0; }
391 CF_INLINE
void *auto_zone_allocate_object(void *zone
, size_t size
, auto_memory_type_t type
, int rc
, int clear
) { return 0; }
392 CF_INLINE
const void *auto_zone_base_pointer(void *zone
, const void *ptr
) { return 0; }
393 CF_INLINE
void auto_zone_set_scan_exactly(void *zone
, void *ptr
) {}
394 CF_INLINE
void auto_zone_retain(void *zone
, void *ptr
) {}
395 CF_INLINE
unsigned int auto_zone_release(void *zone
, void *ptr
) { return 0; }
396 CF_INLINE
unsigned int auto_zone_retain_count(void *zone
, const void *ptr
) { return 0; }
397 CF_INLINE
void auto_zone_set_unscanned(void *zone
, void *ptr
) {}
398 CF_INLINE
void auto_zone_set_nofinalize(void *zone
, void *ptr
) {}
399 CF_INLINE
int auto_zone_is_finalized(void *zone
, const void *ptr
) { return 0; }
400 CF_INLINE
size_t auto_zone_size(void *zone
, const void *ptr
) { return 0; }
401 CF_INLINE
void auto_register_weak_reference(void *zone
, const void *referent
, void **referrer
, uintptr_t *counter
, void **listHead
, void **listElement
) {}
402 CF_INLINE
void auto_unregister_weak_reference(void *zone
, const void *referent
, void **referrer
) {}
403 CF_INLINE
int auto_zone_is_valid_pointer(void *zone
, const void *ptr
) { return 0; }
404 CF_INLINE BOOL
objc_isAuto(id object
) { return 0; }
405 CF_INLINE
void* auto_read_weak_reference(void *zone
, void **referrer
) { void *result
= *referrer
; return result
; }
406 CF_INLINE
void auto_assign_weak_reference(void *zone
, const void *value
, const void **location
, auto_weak_callback_block_t
*block
) { *location
= (void *)value
; }
407 CF_INLINE auto_memory_type_t
auto_zone_get_layout_type(void *zone
, void *ptr
) { return AUTO_UNSCANNED
; }
408 CF_INLINE
int auto_zone_set_write_barrier(void *zone
, const void *dest
, const void *new_value
) { return false; }
410 CF_INLINE
void objc_assertRegisteredThreadWithCollector(void) {}
411 CF_INLINE
void objc_registerThreadWithCollector(void) {}
413 CF_INLINE
uintptr_t _object_getExternalHash(id obj
) {
414 return (uintptr_t)obj
;
419 CF_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
420 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
422 CF_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
423 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
425 CF_INLINE BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
426 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
428 CF_INLINE BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
429 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
431 CF_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
432 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
434 CF_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
435 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
437 CF_INLINE id
objc_assign_strongCast(id val
, id
*dest
)
438 { return (*dest
= val
); }
440 CF_INLINE id
objc_assign_global(id val
, id
*dest
)
441 { return (*dest
= val
); }
443 CF_INLINE id
objc_assign_ivar(id val
, id dest
, ptrdiff_t offset
)
444 { return (*(id
*)((char *)dest
+offset
) = val
); }
446 //CF_INLINE void *objc_memmove_collectable(void *dst, const void *src, size_t size) { return memmove(dst, src, size); }
448 CF_INLINE id
objc_read_weak(id
*location
)
449 { return *location
; }
451 CF_INLINE id
objc_assign_weak(id value
, id
*location
)
452 { return (*location
= value
); }
455 CF_INLINE
void objc_finalizeOnMainThread(Class cls
) { }
456 CF_INLINE BOOL
objc_is_finalized(void *ptr
) { return NO
; }
457 CF_INLINE
void objc_clear_stack(unsigned long options
) { }
459 CF_INLINE BOOL
objc_collectingEnabled(void) { return NO
; }
460 CF_INLINE
void objc_start_collector_thread(void) { }
462 CF_INLINE
void objc_collect(unsigned long options
) { }
466 #if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)