]> git.saurik.com Git - apple/cf.git/blame_incremental - CoreFoundation_Prefix.h
CF-1153.18.tar.gz
[apple/cf.git] / CoreFoundation_Prefix.h
... / ...
CommitLineData
1/*
2 * Copyright (c) 2015 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-2014, 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)
39extern "C" {
40#endif
41
42#if DEPLOYMENT_TARGET_IPHONESIMULATOR // work around <rdar://problem/16507706>
43#include <pthread/qos.h>
44#define qos_class_self() (QOS_CLASS_UTILITY)
45#define qos_class_main() (QOS_CLASS_UTILITY)
46#define pthread_set_qos_class_self_np(A, B) do {} while (0)
47#define pthread_override_qos_class_start_np(A, B, C) (NULL)
48#define pthread_override_qos_class_end_np(A) do {} while (0)
49#elif (DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED)
50#include <pthread/qos.h>
51#endif
52
53#define SystemIntegrityCheck(A, B) do {} while (0)
54
55
56#if INCLUDE_OBJC
57#include <objc/objc.h>
58#else
59typedef signed char BOOL;
60typedef char * id;
61typedef char * Class;
62#ifndef YES
63#define YES (BOOL)1
64#endif
65#ifndef NO
66#define NO (BOOL)0
67#endif
68#ifndef nil
69#define nil NULL
70#endif
71#endif
72
73#define CRSetCrashLogMessage(A) do {} while (0)
74#define CRSetCrashLogMessage2(A) do {} while (0)
75
76#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
77#import <libkern/OSAtomic.h>
78#import <pthread.h>
79#endif
80
81/* This macro creates some helper functions which are useful in dealing with libdispatch:
82 * __ PREFIX Queue -- manages and returns a singleton serial queue
83 *
84 * Use the macro like this:
85 * DISPATCH_HELPER_FUNCTIONS(fh, NSFileHandle)
86 */
87
88#define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME) \
89static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
90 static volatile dispatch_queue_t __ ## PREFIX ## dq = NULL; \
91 if (!__ ## PREFIX ## dq) { \
92 dispatch_queue_t dq = dispatch_queue_create("com.apple." # QNAME, NULL); \
93 void * volatile *loc = (void * volatile *)&__ ## PREFIX ## dq; \
94 if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dq, loc)) { \
95 dispatch_release(dq); \
96 } \
97 } \
98 return __ ## PREFIX ## dq; \
99} \
100
101
102#define LIBAUTO_STUB 1
103
104#ifndef LIBAUTO_STUB
105
106#if DEPLOYMENT_TARGET_MACOSX
107#include <auto_zone.h>
108#endif
109#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
110#include <objc/objc-auto.h>
111#endif
112
113#endif // LIBAUTO_STUB
114
115#if DEPLOYMENT_TARGET_WINDOWS
116// Compatibility with boolean.h
117#if defined(__x86_64__)
118typedef unsigned int boolean_t;
119#else
120typedef int boolean_t;
121#endif
122#endif
123
124#if DEPLOYMENT_TARGET_LINUX
125
126#define CF_PRIVATE
127#define __strong
128#define __weak
129
130#define strtod_l(a,b,locale) strtod(a,b)
131#define strtoul_l(a,b,c,locale) strtoul(a,b,c)
132#define strtol_l(a,b,c,locale) strtol(a,b,c)
133#define strtoll_l(a,b,c,locale) strtoll(a,b,c)
134#define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c)
135
136#define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
137
138#include <pthread.h>
139
140CF_INLINE size_t
141strlcpy(char * dst, const char * src, size_t maxlen) {
142 const size_t srclen = strlen(src);
143 if (srclen < maxlen) {
144 memcpy(dst, src, srclen+1);
145 } else if (maxlen != 0) {
146 memcpy(dst, src, maxlen-1);
147 dst[maxlen-1] = '\0';
148 }
149 return srclen;
150}
151
152CF_INLINE size_t
153strlcat(char * dst, const char * src, size_t maxlen) {
154 const size_t srclen = strlen(src);
155 const size_t dstlen = strnlen(dst, maxlen);
156 if (dstlen == maxlen) return maxlen+srclen;
157 if (srclen < maxlen-dstlen) {
158 memcpy(dst+dstlen, src, srclen+1);
159 } else {
160 memcpy(dst+dstlen, src, maxlen-dstlen-1);
161 dst[maxlen-1] = '\0';
162 }
163 return dstlen + srclen;
164}
165
166#define issetugid() 0
167
168// Implemented in CFPlatform.c
169bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
170bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
171bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
172bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
173
174int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
175int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
176int32_t OSAtomicIncrement32(volatile int32_t *theValue);
177int32_t OSAtomicDecrement32(volatile int32_t *theValue);
178
179int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
180int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
181bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
182
183void OSMemoryBarrier();
184
185#include <malloc.h>
186CF_INLINE size_t malloc_size(void *memblock) {
187 return malloc_usable_size(memblock);
188}
189
190// substitute for dispatch_once
191typedef pthread_once_t dispatch_once_t;
192typedef void (^dispatch_block_t)(void);
193void dispatch_once(dispatch_once_t *predicate, dispatch_block_t block);
194
195#endif
196
197#if DEPLOYMENT_TARGET_WINDOWS || DEPLOYMENT_TARGET_LINUX
198#if !defined(MIN)
199#define MIN(A,B) ((A) < (B) ? (A) : (B))
200#endif
201
202#if !defined(MAX)
203#define MAX(A,B) ((A) > (B) ? (A) : (B))
204#endif
205
206#if !defined(ABS)
207#define ABS(A) ((A) < 0 ? (-(A)) : (A))
208#endif
209#endif
210
211#if DEPLOYMENT_TARGET_WINDOWS
212
213#define MAXPATHLEN MAX_PATH
214#undef MAX_PATH
215#undef INVALID_HANDLE_VALUE
216
217// Defined for source compatibility
218#define ino_t _ino_t
219#define off_t _off_t
220#define mode_t uint16_t
221
222// 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
223#define CF_PRIVATE extern
224
225#define __builtin_expect(P1,P2) P1
226
227// These are replacements for POSIX calls on Windows, ensuring that the UTF8 parameters are converted to UTF16 before being passed to Windows
228CF_EXPORT int _NS_stat(const char *name, struct _stat *st);
229CF_EXPORT int _NS_mkdir(const char *name);
230CF_EXPORT int _NS_rmdir(const char *name);
231CF_EXPORT int _NS_chmod(const char *name, int mode);
232CF_EXPORT int _NS_unlink(const char *name);
233CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
234CF_EXPORT char *_NS_getenv(const char *name);
235CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
236CF_EXPORT int _NS_open(const char *name, int oflag, int pmode = 0);
237CF_EXPORT int _NS_chdir(const char *name);
238CF_EXPORT int _NS_mkstemp(char *name, int bufSize);
239CF_EXPORT int _NS_access(const char *name, int amode);
240
241#define BOOL WINDOWS_BOOL
242
243#define WIN32_LEAN_AND_MEAN
244
245#ifndef WINVER
246#define WINVER 0x0501
247#endif
248
249#ifndef _WIN32_WINNT
250#define _WIN32_WINNT 0x0501
251#endif
252
253// The order of these includes is important
254#define FD_SETSIZE 1024
255#include <winsock2.h>
256#include <windows.h>
257#include <pthread.h>
258
259#undef BOOL
260
261#ifndef HAVE_STRUCT_TIMESPEC
262#define HAVE_STRUCT_TIMESPEC 1
263struct timespec {
264 long tv_sec;
265 long tv_nsec;
266};
267#endif /* HAVE_STRUCT_TIMESPEC */
268
269#define __PRETTY_FUNCTION__ __FUNCTION__
270
271#define malloc_default_zone() (void *)0
272#define malloc_zone_from_ptr(a) (void *)0
273#define malloc_zone_malloc(zone,size) malloc(size)
274#define malloc_zone_memalign(zone,align,size) malloc(size)
275#define malloc_zone_calloc(zone,count,size) calloc(count,size)
276#define bcopy(b1,b2,len) memmove(b2, b1, (size_t)(len))
277typedef int malloc_zone_t;
278typedef int uid_t;
279typedef int gid_t;
280#define geteuid() 0
281#define getuid() 0
282#define getegid() 0
283
284#define scalbn(A, B) _scalb(A, B)
285
286#define fsync(a) _commit(a)
287#define malloc_create_zone(a,b) 123
288#define malloc_set_zone_name(zone,name)
289#define malloc_zone_realloc(zone,ptr,size) realloc(ptr,size)
290#define malloc_zone_free(zone,ptr) free(ptr)
291
292// implemented in CFInternal.h
293#define OSSpinLockLock(A) __CFLock(A)
294#define OSSpinLockUnlock(A) __CFUnlock(A)
295
296typedef int32_t OSSpinLock;
297
298#define OS_SPINLOCK_INIT 0
299
300#include <stdint.h>
301#include <stdbool.h>
302#include <stdio.h>
303#include <malloc.h>
304
305CF_INLINE size_t malloc_size(void *memblock) {
306 return _msize(memblock);
307}
308
309CF_INLINE uint64_t mach_absolute_time() {
310 LARGE_INTEGER count;
311 QueryPerformanceCounter(&count);
312 // mach_absolute_time is unsigned, but this function returns a signed value.
313 return (uint64_t)count.QuadPart;
314}
315
316CF_INLINE long long llabs(long long v) {
317 if (v < 0) return -v;
318 return v;
319}
320
321#define strtod_l(a,b,locale) strtod(a,b)
322#define strtoul_l(a,b,c,locale) strtoul(a,b,c)
323#define strtol_l(a,b,c,locale) strtol(a,b,c)
324#define strtoll_l(a,b,c,locale) _strtoi64(a,b,c)
325#define strncasecmp(a, b, c) _strnicmp(a, b, c)
326#define strncasecmp_l(a, b, c, d) _strnicmp(a, b, c)
327#define snprintf _snprintf
328
329#define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
330
331CF_INLINE size_t
332strlcpy(char * dst, const char * src, size_t maxlen) {
333 const size_t srclen = strlen(src);
334 if (srclen < maxlen) {
335 memcpy(dst, src, srclen+1);
336 } else if (maxlen != 0) {
337 memcpy(dst, src, maxlen-1);
338 dst[maxlen-1] = '\0';
339 }
340 return srclen;
341}
342
343CF_INLINE size_t
344strlcat(char * dst, const char * src, size_t maxlen) {
345 const size_t srclen = strlen(src);
346 const size_t dstlen = strnlen(dst, maxlen);
347 if (dstlen == maxlen) return maxlen+srclen;
348 if (srclen < maxlen-dstlen) {
349 memcpy(dst+dstlen, src, srclen+1);
350 } else {
351 memcpy(dst+dstlen, src, maxlen-dstlen-1);
352 dst[maxlen-1] = '\0';
353 }
354 return dstlen + srclen;
355}
356
357#define sleep(x) Sleep(1000*x)
358
359#define issetugid() 0
360
361// CF exports these useful atomic operation functions on Windows
362CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
363CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
364CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
365
366CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
367CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
368CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue);
369CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue);
370
371CF_EXPORT int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
372CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
373CF_EXPORT bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
374
375/*
376CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
377CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
378
379CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue );
380CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue );
381*/
382
383//#ifndef NTDDI_VERSION
384//#define NTDDI_VERSION NTDDI_WINXP
385//#endif
386
387#include <io.h>
388#include <fcntl.h>
389#include <errno.h>
390
391#endif
392
393#if !defined(CF_PRIVATE)
394#define CF_PRIVATE __attribute__((__visibility__("hidden")))
395#endif
396
397#if DEPLOYMENT_TARGET_LINUX || DEPLOYMENT_TARGET_WINDOWS
398
399#include <stdarg.h>
400
401CF_INLINE int flsl( long mask ) {
402 int idx = 0;
403 while (mask != 0) mask = (unsigned long)mask >> 1, idx++;
404 return idx;
405}
406
407CF_INLINE int popcountll(long long x) {
408 int count = 0;
409 while (x) {
410 count++;
411 x &= x - 1; // reset LS1B
412 }
413 return count;
414}
415
416CF_PRIVATE int asprintf(char **ret, const char *format, ...);
417
418#endif
419
420#ifdef LIBAUTO_STUB
421
422#include <stddef.h>
423
424/* Stubs for functions in libauto. */
425
426enum {OBJC_GENERATIONAL = (1 << 0)};
427
428enum {
429 OBJC_RATIO_COLLECTION = (0 << 0),
430 OBJC_GENERATIONAL_COLLECTION = (1 << 0),
431 OBJC_FULL_COLLECTION = (2 << 0),
432 OBJC_EXHAUSTIVE_COLLECTION = (3 << 0),
433 OBJC_COLLECT_IF_NEEDED = (1 << 3),
434 OBJC_WAIT_UNTIL_DONE = (1 << 4),
435};
436
437
438enum {
439 AUTO_TYPE_UNKNOWN = -1,
440 AUTO_UNSCANNED = (1 << 0),
441 AUTO_OBJECT = (1 << 1),
442 AUTO_POINTERS_ONLY = (1 << 2),
443 AUTO_MEMORY_SCANNED = !AUTO_UNSCANNED,
444 AUTO_MEMORY_UNSCANNED = AUTO_UNSCANNED,
445 AUTO_MEMORY_ALL_POINTERS = AUTO_POINTERS_ONLY,
446 AUTO_MEMORY_ALL_WEAK_POINTERS = (AUTO_UNSCANNED | AUTO_POINTERS_ONLY),
447 AUTO_OBJECT_SCANNED = AUTO_OBJECT,
448 AUTO_OBJECT_UNSCANNED = AUTO_OBJECT | AUTO_UNSCANNED,
449 AUTO_OBJECT_ALL_POINTERS = AUTO_OBJECT | AUTO_POINTERS_ONLY
450};
451typedef unsigned long auto_memory_type_t;
452typedef struct _auto_zone_t auto_zone_t;
453typedef struct auto_weak_callback_block {
454 struct auto_weak_callback_block *next;
455 void (*callback_function)(void *arg1, void *arg2);
456 void *arg1;
457 void *arg2;
458} auto_weak_callback_block_t;
459
460CF_INLINE void *objc_memmove_collectable(void *a, const void *b, size_t c) { return memmove(a, b, c); }
461CF_INLINE void *objc_collectableZone(void) { return 0; }
462
463CF_INLINE void *auto_zone_allocate_object(void *zone, size_t size, auto_memory_type_t type, int rc, int clear) { return 0; }
464CF_INLINE const void *auto_zone_base_pointer(void *zone, const void *ptr) { return 0; }
465CF_INLINE void auto_zone_set_scan_exactly(void *zone, void *ptr) {}
466CF_INLINE void auto_zone_retain(void *zone, void *ptr) {}
467CF_INLINE unsigned int auto_zone_release(void *zone, void *ptr) { return 0; }
468CF_INLINE unsigned int auto_zone_retain_count(void *zone, const void *ptr) { return 0; }
469CF_INLINE void auto_zone_set_unscanned(void *zone, void *ptr) {}
470CF_INLINE void auto_zone_set_nofinalize(void *zone, void *ptr) {}
471CF_INLINE int auto_zone_is_finalized(void *zone, const void *ptr) { return 0; }
472CF_INLINE size_t auto_zone_size(void *zone, const void *ptr) { return 0; }
473CF_INLINE void auto_register_weak_reference(void *zone, const void *referent, void **referrer, uintptr_t *counter, void **listHead, void **listElement) {}
474CF_INLINE void auto_unregister_weak_reference(void *zone, const void *referent, void **referrer) {}
475CF_INLINE int auto_zone_is_valid_pointer(void *zone, const void *ptr) { return 0; }
476CF_INLINE BOOL objc_isAuto(id object) { return 0; }
477CF_INLINE void* auto_read_weak_reference(void *zone, void **referrer) { void *result = *referrer; return result; }
478CF_INLINE void auto_assign_weak_reference(void *zone, const void *value, const void **location, auto_weak_callback_block_t *block) { *location = (void *)value; }
479CF_INLINE auto_memory_type_t auto_zone_get_layout_type(void *zone, void *ptr) { return AUTO_UNSCANNED; }
480CF_INLINE int auto_zone_set_write_barrier(void *zone, const void *dest, const void *new_value) { return false; }
481
482CF_INLINE void objc_assertRegisteredThreadWithCollector(void) {}
483CF_INLINE void objc_registerThreadWithCollector(void) {}
484
485CF_INLINE uintptr_t _object_getExternalHash(id obj) {
486 return (uintptr_t)obj;
487}
488
489// from objc-auto.h
490
491CF_INLINE BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation)
492{ return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
493
494CF_INLINE BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation)
495{ return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
496
497CF_INLINE BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation)
498{ return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
499
500CF_INLINE BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation)
501{ return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
502
503CF_INLINE BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation)
504{ return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
505
506CF_INLINE BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation)
507{ return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
508
509CF_INLINE id objc_assign_strongCast(id val, id *dest)
510{ return (*dest = val); }
511
512CF_INLINE id objc_assign_global(id val, id *dest)
513{ return (*dest = val); }
514
515CF_INLINE id objc_assign_ivar(id val, id dest, ptrdiff_t offset)
516{ return (*(id*)((char *)dest+offset) = val); }
517
518//CF_INLINE void *objc_memmove_collectable(void *dst, const void *src, size_t size) { return memmove(dst, src, size); }
519
520CF_INLINE id objc_read_weak(id *location)
521{ return *location; }
522
523CF_INLINE id objc_assign_weak(id value, id *location)
524{ return (*location = value); }
525
526
527CF_INLINE void objc_finalizeOnMainThread(Class cls) { }
528CF_INLINE BOOL objc_is_finalized(void *ptr) { return NO; }
529CF_INLINE void objc_clear_stack(unsigned long options) { }
530
531CF_INLINE BOOL objc_collectingEnabled(void) { return NO; }
532CF_INLINE void objc_start_collector_thread(void) { }
533
534CF_INLINE void objc_collect(unsigned long options) { }
535
536#endif
537
538#if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)
539} // extern "C"
540#endif