]> git.saurik.com Git - apple/cf.git/blob - CoreFoundation_Prefix.h
CF-635.tar.gz
[apple/cf.git] / CoreFoundation_Prefix.h
1 /*
2 * Copyright (c) 2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /* CoreFoundation_Prefix.h
25 Copyright (c) 2005-2011, Apple Inc. All rights reserved.
26 */
27
28
29 #define _DARWIN_UNLIMITED_SELECT 1
30
31 #include <CoreFoundation/CFBase.h>
32
33
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <string.h>
37
38 #if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)
39 extern "C" {
40 #endif
41
42 #define SystemIntegrityCheck(A, B) do {} while (0)
43
44
45 #if INCLUDE_OBJC
46 #include <objc/objc.h>
47 #else
48 typedef signed char BOOL;
49 typedef char * id;
50 typedef char * Class;
51 #define YES (BOOL)1
52 #define NO (BOOL)0
53 #define nil NULL
54 #endif
55
56 #if DEPLOYMENT_TARGET_MACOSX && defined(__ppc__)
57 #define SUPPORT_CFM 1
58 #endif
59
60 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
61 #import <libkern/OSAtomic.h>
62 #import <pthread.h>
63 #endif
64
65 /* This macro creates 3 helper functions which are useful in dealing with libdispatch:
66 * __ PREFIX SyncDispatchIsSafe -- returns bool indicating whether calling dispatch_sync() would be safe from self-deadlock
67 * __ PREFIX Queue -- manages and returns a singleton serial queue
68 *
69 * Use the macro like this:
70 * DISPATCH_HELPER_FUNCTIONS(fh, NSFileHandle)
71 */
72
73 #define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME) \
74 static Boolean __ ## PREFIX ## SyncDispatchIsSafe(dispatch_queue_t Q) { \
75 dispatch_queue_t C = dispatch_get_current_queue(); \
76 return (!C || Q != C) ? true : false; \
77 } \
78 \
79 static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
80 static volatile dispatch_queue_t __ ## PREFIX ## dq = NULL; \
81 if (!__ ## PREFIX ## dq) { \
82 dispatch_queue_t dq = dispatch_queue_create(# QNAME, NULL); \
83 void * volatile *loc = (void * volatile *)&__ ## PREFIX ## dq; \
84 if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dq, loc)) { \
85 dispatch_release(dq); \
86 } \
87 } \
88 return __ ## PREFIX ## dq; \
89 } \
90
91
92 #define LIBAUTO_STUB 1
93
94 #ifndef LIBAUTO_STUB
95
96 #if DEPLOYMENT_TARGET_MACOSX
97 #include <auto_zone.h>
98 #endif
99 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
100 #include <objc/objc-auto.h>
101 #endif
102
103 #endif // LIBAUTO_STUB
104
105 #if DEPLOYMENT_TARGET_WINDOWS
106 // Compatibility with boolean.h
107 #if defined(__x86_64__)
108 typedef unsigned int boolean_t;
109 #else
110 typedef int boolean_t;
111 #endif
112 #endif
113
114 #if DEPLOYMENT_TARGET_LINUX
115
116 #define __private_extern__
117 #define __strong
118 #define __weak
119
120 #define strtod_l(a,b,locale) strtod(a,b)
121 #define strtoul_l(a,b,c,locale) strtoul(a,b,c)
122 #define strtol_l(a,b,c,locale) strtol(a,b,c)
123 #define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c)
124
125 #define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
126
127 #define strlcat(a,b,c) strncat(a,b,c)
128 #define strlcpy(a,b,c) strncpy(a,b,c)
129
130 #define issetugid() 0
131
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);
136
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);
141
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 );
145
146 void OSMemoryBarrier();
147
148 #include <malloc.h>
149 CF_INLINE size_t malloc_size(void *memblock) {
150 return malloc_usable_size(memblock);
151 }
152
153 #endif
154
155 #if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
156 #if !defined(MIN)
157 #define MIN(A,B) ((A) < (B) ? (A) : (B))
158 #endif
159
160 #if !defined(MAX)
161 #define MAX(A,B) ((A) > (B) ? (A) : (B))
162 #endif
163
164 #if !defined(ABS)
165 #define ABS(A) ((A) < 0 ? (-(A)) : (A))
166 #endif
167 #endif
168
169 #if DEPLOYMENT_TARGET_WINDOWS
170
171 #define MAXPATHLEN MAX_PATH
172 #undef MAX_PATH
173 #undef INVALID_HANDLE_VALUE
174
175 // Defined for source compatibility
176 #define ino_t _ino_t
177 #define off_t _off_t
178 #define mode_t uint16_t
179
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
182
183 #define __builtin_expect(P1,P2) P1
184
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);
198
199 #define BOOL WINDOWS_BOOL
200
201 #define WIN32_LEAN_AND_MEAN
202
203 #ifndef WINVER
204 #define WINVER 0x0501
205 #endif
206
207 #ifndef _WIN32_WINNT
208 #define _WIN32_WINNT 0x0501
209 #endif
210
211 // The order of these includes is important
212 #include <winsock2.h>
213 #include <windows.h>
214 #include <pthread.h>
215
216 #undef BOOL
217
218 #ifndef HAVE_STRUCT_TIMESPEC
219 #define HAVE_STRUCT_TIMESPEC 1
220 struct timespec {
221 long tv_sec;
222 long tv_nsec;
223 };
224 #endif /* HAVE_STRUCT_TIMESPEC */
225
226 #define __PRETTY_FUNCTION__ __FUNCTION__
227
228 #define malloc_default_zone() (void *)0
229 #define malloc_zone_from_ptr(a) (void *)0
230 #define malloc_zone_malloc(zone,size) malloc(size)
231 #define malloc_zone_memalign(zone,align,size) malloc(size)
232 #define malloc_zone_calloc(zone,count,size) calloc(count,size)
233 #define bcopy(b1,b2,len) memmove(b2, b1, (size_t)(len))
234 typedef int malloc_zone_t;
235 typedef int uid_t;
236 typedef int gid_t;
237 #define geteuid() 0
238 #define getuid() 0
239 #define getegid() 0
240
241 #define fsync(a) _commit(a)
242 #define malloc_create_zone(a,b) 123
243 #define malloc_set_zone_name(zone,name)
244 #define malloc_zone_realloc(zone,ptr,size) realloc(ptr,size)
245 #define malloc_zone_free(zone,ptr) free(ptr)
246
247 // implemented in CFInternal.h
248 #define OSSpinLockLock(A) __CFSpinLock(A)
249 #define OSSpinLockUnlock(A) __CFSpinUnlock(A)
250
251 typedef int32_t OSSpinLock;
252
253 #define OS_SPINLOCK_INIT 0
254
255 #include <stdint.h>
256 #include <stdbool.h>
257 #include <stdio.h>
258 #include <malloc.h>
259
260 CF_INLINE size_t malloc_size(void *memblock) {
261 return _msize(memblock);
262 }
263
264 #define mach_absolute_time() ((uint64_t)(CFAbsoluteTimeGetCurrent() * 1000000000.0))
265
266 #define strtod_l(a,b,locale) strtod(a,b)
267 #define strtoul_l(a,b,c,locale) strtoul(a,b,c)
268 #define strtol_l(a,b,c,locale) strtol(a,b,c)
269 #define strncasecmp_l(a, b, c, d) _strnicmp(a, b, c)
270 #define snprintf _snprintf
271
272 #define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
273
274 #define strlcat(a,b,c) strncat(a,b,c)
275 #define strlcpy(a,b,c) strncpy(a,b,c)
276
277 #define sleep(x) Sleep(1000*x)
278
279 #define issetugid() 0
280
281 // CF exports these useful atomic operation functions on Windows
282 CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
283 CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
284 CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
285
286 CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
287 CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
288 CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue);
289 CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue);
290
291 CF_EXPORT int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
292 CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
293 CF_EXPORT bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
294
295 /*
296 CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
297 CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
298
299 CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue );
300 CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue );
301 */
302
303 //#ifndef NTDDI_VERSION
304 //#define NTDDI_VERSION NTDDI_WINXP
305 //#endif
306
307 #include <io.h>
308 #include <fcntl.h>
309 #include <errno.h>
310
311 #endif
312
313 #if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
314
315 #include <stdarg.h>
316
317 CF_INLINE int flsl( long mask ) {
318 int idx = 0;
319 while (mask != 0) mask = (unsigned long)mask >> 1, idx++;
320 return idx;
321 }
322
323 CF_INLINE int popcountll(long long x) {
324 int count = 0;
325 while (x) {
326 count++;
327 x &= x - 1; // reset LS1B
328 }
329 return count;
330 }
331
332 __private_extern__ int asprintf(char **ret, const char *format, ...);
333
334 #endif
335
336 #ifdef LIBAUTO_STUB
337
338 #include <stddef.h>
339
340 /* Stubs for functions in libauto. */
341
342 enum {OBJC_GENERATIONAL = (1 << 0)};
343
344 enum {
345 OBJC_RATIO_COLLECTION = (0 << 0),
346 OBJC_GENERATIONAL_COLLECTION = (1 << 0),
347 OBJC_FULL_COLLECTION = (2 << 0),
348 OBJC_EXHAUSTIVE_COLLECTION = (3 << 0),
349 OBJC_COLLECT_IF_NEEDED = (1 << 3),
350 OBJC_WAIT_UNTIL_DONE = (1 << 4),
351 };
352
353
354 enum {
355 AUTO_TYPE_UNKNOWN = -1,
356 AUTO_UNSCANNED = (1 << 0),
357 AUTO_OBJECT = (1 << 1),
358 AUTO_POINTERS_ONLY = (1 << 2),
359 AUTO_MEMORY_SCANNED = !AUTO_UNSCANNED,
360 AUTO_MEMORY_UNSCANNED = AUTO_UNSCANNED,
361 AUTO_MEMORY_ALL_POINTERS = AUTO_POINTERS_ONLY,
362 AUTO_MEMORY_ALL_WEAK_POINTERS = (AUTO_UNSCANNED | AUTO_POINTERS_ONLY),
363 AUTO_OBJECT_SCANNED = AUTO_OBJECT,
364 AUTO_OBJECT_UNSCANNED = AUTO_OBJECT | AUTO_UNSCANNED,
365 AUTO_OBJECT_ALL_POINTERS = AUTO_OBJECT | AUTO_POINTERS_ONLY
366 };
367 typedef unsigned long auto_memory_type_t;
368 typedef struct _auto_zone_t auto_zone_t;
369 typedef struct auto_weak_callback_block {
370 struct auto_weak_callback_block *next;
371 void (*callback_function)(void *arg1, void *arg2);
372 void *arg1;
373 void *arg2;
374 } auto_weak_callback_block_t;
375
376 CF_INLINE void *objc_memmove_collectable(void *a, const void *b, size_t c) { return memmove(a, b, c); }
377 CF_INLINE void *objc_collectableZone(void) { return 0; }
378
379 CF_INLINE void *auto_zone_allocate_object(void *zone, size_t size, auto_memory_type_t type, int rc, int clear) { return 0; }
380 CF_INLINE const void *auto_zone_base_pointer(void *zone, const void *ptr) { return 0; }
381 CF_INLINE void auto_zone_set_scan_exactly(void *zone, void *ptr) {}
382 CF_INLINE void auto_zone_retain(void *zone, void *ptr) {}
383 CF_INLINE unsigned int auto_zone_release(void *zone, void *ptr) { return 0; }
384 CF_INLINE unsigned int auto_zone_retain_count(void *zone, const void *ptr) { return 0; }
385 CF_INLINE void auto_zone_set_unscanned(void *zone, void *ptr) {}
386 CF_INLINE void auto_zone_set_nofinalize(void *zone, void *ptr) {}
387 CF_INLINE int auto_zone_is_finalized(void *zone, const void *ptr) { return 0; }
388 CF_INLINE size_t auto_zone_size(void *zone, const void *ptr) { return 0; }
389 CF_INLINE void auto_register_weak_reference(void *zone, const void *referent, void **referrer, uintptr_t *counter, void **listHead, void **listElement) {}
390 CF_INLINE void auto_unregister_weak_reference(void *zone, const void *referent, void **referrer) {}
391 CF_INLINE int auto_zone_is_valid_pointer(void *zone, const void *ptr) { return 0; }
392 CF_INLINE BOOL objc_isAuto(id object) { return 0; }
393 CF_INLINE void* auto_read_weak_reference(void *zone, void **referrer) { void *result = *referrer; return result; }
394 CF_INLINE void auto_assign_weak_reference(void *zone, const void *value, const void **location, auto_weak_callback_block_t *block) { *location = (void *)value; }
395 CF_INLINE auto_memory_type_t auto_zone_get_layout_type(void *zone, void *ptr) { return AUTO_UNSCANNED; }
396 CF_INLINE int auto_zone_set_write_barrier(void *zone, const void *dest, const void *new_value) { return false; }
397
398 CF_INLINE void objc_assertRegisteredThreadWithCollector(void) {}
399 CF_INLINE void objc_registerThreadWithCollector(void) {}
400
401 CF_INLINE uintptr_t _object_getExternalHash(id obj) {
402 return (uintptr_t)obj;
403 }
404
405 // from objc-auto.h
406
407 CF_INLINE BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation)
408 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
409
410 CF_INLINE BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation)
411 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
412
413 CF_INLINE BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation)
414 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
415
416 CF_INLINE BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation)
417 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
418
419 CF_INLINE BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation)
420 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
421
422 CF_INLINE BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation)
423 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
424
425 CF_INLINE id objc_assign_strongCast(id val, id *dest)
426 { return (*dest = val); }
427
428 CF_INLINE id objc_assign_global(id val, id *dest)
429 { return (*dest = val); }
430
431 CF_INLINE id objc_assign_ivar(id val, id dest, ptrdiff_t offset)
432 { return (*(id*)((char *)dest+offset) = val); }
433
434 //CF_INLINE void *objc_memmove_collectable(void *dst, const void *src, size_t size) { return memmove(dst, src, size); }
435
436 CF_INLINE id objc_read_weak(id *location)
437 { return *location; }
438
439 CF_INLINE id objc_assign_weak(id value, id *location)
440 { return (*location = value); }
441
442
443 CF_INLINE void objc_finalizeOnMainThread(Class cls) { }
444 CF_INLINE BOOL objc_is_finalized(void *ptr) { return NO; }
445 CF_INLINE void objc_clear_stack(unsigned long options) { }
446
447 CF_INLINE BOOL objc_collectingEnabled(void) { return NO; }
448 CF_INLINE void objc_start_collector_thread(void) { }
449
450 CF_INLINE void objc_collect(unsigned long options) { }
451
452 #endif
453
454 // Need to use the _O_BINARY flag on Windows to get the correct behavior
455 #if DEPLOYMENT_TARGET_WINDOWS
456 #define CF_OPENFLGS (_O_BINARY|_O_NOINHERIT)
457 #else
458 #define CF_OPENFLGS (0)
459 #endif
460
461 #if DEPLOYMENT_TARGET_WINDOWS
462
463 // These are replacements for pthread calls on Windows
464 CF_EXPORT int _NS_pthread_main_np();
465 CF_EXPORT int _NS_pthread_setspecific(pthread_key_t key, const void *val);
466 CF_EXPORT void* _NS_pthread_getspecific(pthread_key_t key);
467 CF_EXPORT int _NS_pthread_key_init_np(int key, void (*destructor)(void *));
468 CF_EXPORT void _NS_pthread_setname_np(const char *name);
469
470 // map use of pthread_set/getspecific to internal API
471 #define pthread_setspecific _NS_pthread_setspecific
472 #define pthread_getspecific _NS_pthread_getspecific
473 #define pthread_key_init_np _NS_pthread_key_init_np
474 #define pthread_main_np _NS_pthread_main_np
475 #define pthread_setname_np _NS_pthread_setname_np
476 #endif
477
478 #if DEPLOYMENT_TARGET_WINDOWS
479 // replacement for DISPATCH_QUEUE_OVERCOMMIT until we get a bug fix in dispatch on Windows
480 // <rdar://problem/7923891> dispatch on Windows: Need queue_private.h
481 #define DISPATCH_QUEUE_OVERCOMMIT 2
482 #endif
483
484 #if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)
485 } // extern "C"
486 #endif