forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoreFoundation_Prefix.h
474 lines (372 loc) · 14.1 KB
/
CoreFoundation_Prefix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* CoreFoundation_Prefix.h
Copyright (c) 2005-2018, Apple Inc. and the Swift project authors
Portions Copyright (c) 2014-2018, Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
*/
#ifndef __COREFOUNDATION_PREFIX_H__
#define __COREFOUNDATION_PREFIX_H__ 1
#define _DARWIN_UNLIMITED_SELECT 1
#include <CoreFoundation/CFAvailability.h>
#define __HAS_DISPATCH__ 1
#include <CoreFoundation/CFBase.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#if TARGET_OS_WIN32 && defined(__cplusplus)
extern "C" {
#endif
#if DEPLOYMENT_TARGET_IPHONESIMULATOR // work around <rdar://problem/16507706>
#include <pthread.h>
#include <pthread/qos.h>
#define qos_class_self() (QOS_CLASS_UTILITY)
#define qos_class_main() (QOS_CLASS_UTILITY)
#define pthread_set_qos_class_self_np(A, B) do {} while (0)
#define pthread_override_qos_class_start_np(A, B, C) (NULL)
#define pthread_override_qos_class_end_np(A) do {} while (0)
#elif TARGET_OS_MAC
#include <pthread.h>
#include <pthread/qos.h>
#endif
#define SystemIntegrityCheck(A, B) do {} while (0)
#if INCLUDE_OBJC
#include <objc/objc.h>
#else
typedef signed char BOOL;
typedef char * id;
typedef char * Class;
#ifndef YES
#define YES (BOOL)1
#endif
#ifndef NO
#define NO (BOOL)0
#endif
#ifndef nil
#define nil NULL
#endif
#endif
#define CRSetCrashLogMessage(A) do {} while (0)
#define CRSetCrashLogMessage2(A) do {} while (0)
#if TARGET_OS_MAC
#include <libkern/OSAtomic.h>
#include <pthread.h>
#endif
/* This macro creates some helper functions which are useful in dealing with libdispatch:
* __ PREFIX Queue -- manages and returns a singleton serial queue
*
* Use the macro like this:
* DISPATCH_HELPER_FUNCTIONS(fh, NSFileHandle)
*/
#if __HAS_DISPATCH__
#define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME) \
static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
static volatile dispatch_queue_t __ ## PREFIX ## dq = NULL; \
if (!__ ## PREFIX ## dq) { \
dispatch_queue_t dq = dispatch_queue_create("com.apple." # QNAME, NULL); \
void * volatile *loc = (void * volatile *)&__ ## PREFIX ## dq; \
if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dq, loc)) { \
dispatch_release(dq); \
} \
} \
return __ ## PREFIX ## dq; \
}
#else
#define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME)
#endif
// hint to the analyzer that the caller is no longer responsable for the object and that it will be transfered to the reciver that is opaque to the caller
#if __clang_analyzer__
#define CF_TRANSFER_OWNERSHIP(obj) (__typeof(obj))[(id)obj autorelease]
#else
#define CF_TRANSFER_OWNERSHIP(obj) obj
#endif
// hint to the analyzer that the retain/releases are balanced in other locations; the string should be searchable to identify the coorisponding location for the retain/release. These macros should be used with great caution in that they distort the actual retain/release nature of what is happening to the analyzer. Reasonable locations would be in the cases where a value needs to be retained over the lifespan of an external event like a remote machine/process etc.
// NOTE: these seem like they may be backwards - however they are intended to be promises to the analyzer of what will come to pass
#if __clang_analyzer__
#define CF_RELEASE_BALANCED_ELSEWHERE(obj, identified_location) if (obj) CFRetain(obj)
#else
#define CF_RELEASE_BALANCED_ELSEWHERE(obj, identified_location) do { } while (0)
#endif
#if __clang_analyzer__
#define CF_RETAIN_BALANCED_ELSEWHERE(obj, identified_location) if (obj) CFRelease(obj)
#else
#define CF_RETAIN_BALANCED_ELSEWHERE(obj, identified_location) do { } while (0)
#endif
#if TARGET_OS_WIN32
// Compatibility with boolean.h
#if defined(__x86_64__)
typedef unsigned int boolean_t;
#else
typedef int boolean_t;
#endif
#endif
#if TARGET_OS_BSD
#include <string.h>
#include <sys/stat.h> // mode_t
#endif
#if TARGET_OS_LINUX
#define CF_PRIVATE extern __attribute__((visibility("hidden")))
#define __weak
#define strtod_l(a,b,locale) strtod(a,b)
#define strtoul_l(a,b,c,locale) strtoul(a,b,c)
#define strtol_l(a,b,c,locale) strtol(a,b,c)
#define strtoll_l(a,b,c,locale) strtoll(a,b,c)
#define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c)
#define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
#include <pthread.h>
#if TARGET_OS_ANDROID
typedef unsigned long fd_mask;
#endif
#if !TARGET_OS_ANDROID && !TARGET_OS_CYGWIN
CF_INLINE size_t
strlcpy(char * dst, const char * src, size_t maxlen) {
const size_t srclen = strlen(src);
if (srclen < maxlen) {
memcpy(dst, src, srclen+1);
} else if (maxlen != 0) {
memcpy(dst, src, maxlen-1);
dst[maxlen-1] = '\0';
}
return srclen;
}
CF_INLINE size_t
strlcat(char * dst, const char * src, size_t maxlen) {
const size_t srclen = strlen(src);
const size_t dstlen = strnlen(dst, maxlen);
if (dstlen == maxlen) return maxlen+srclen;
if (srclen < maxlen-dstlen) {
memcpy(dst+dstlen, src, srclen+1);
} else {
memcpy(dst+dstlen, src, maxlen-dstlen-1);
dst[maxlen-1] = '\0';
}
return dstlen + srclen;
}
#endif
#if !TARGET_OS_CYGWIN
#define issetugid() 0
#endif
// Implemented in CFPlatform.c
bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
int32_t OSAtomicIncrement32(volatile int32_t *theValue);
int32_t OSAtomicDecrement32(volatile int32_t *theValue);
int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
void OSMemoryBarrier();
#if TARGET_OS_CYGWIN
#define HAVE_STRUCT_TIMESPEC 1
#define strncasecmp_l(a, b, c, d) strncasecmp(a, b, c)
#define _NO_BOOL_TYPEDEF
#undef interface
#endif
#include <malloc.h>
CF_INLINE size_t malloc_size(void *memblock) {
return malloc_usable_size(memblock);
}
#include <time.h>
CF_INLINE uint64_t mach_absolute_time() {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (uint64_t)ts.tv_nsec + (uint64_t)ts.tv_sec * 1000000000UL;
}
#define malloc_default_zone() (void *)0
#endif
#if TARGET_OS_BSD
#define HAVE_STRUCT_TIMESPEC 1
#define CF_PRIVATE extern __attribute__((visibility("hidden")))
#define __strong
#define __weak
// Implemented in CFPlatform.c
bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
int32_t OSAtomicIncrement32(volatile int32_t *theValue);
int32_t OSAtomicDecrement32(volatile int32_t *theValue);
int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
void OSMemoryBarrier();
#endif
#if TARGET_OS_LINUX
#include <sys/param.h>
#endif
#if TARGET_OS_WIN32 || TARGET_OS_LINUX
#if !defined(ABS)
#define ABS(A) ((A) < 0 ? (-(A)) : (A))
#endif
#endif
#if TARGET_OS_WIN32
#define MAXPATHLEN MAX_PATH
#undef MAX_PATH
#undef INVALID_HANDLE_VALUE
// Defined for source compatibility
#define ino_t _ino_t
#define off_t _off_t
typedef int mode_t;
// 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
#define CF_PRIVATE extern
#define __builtin_expect(P1,P2) P1
#include <sys/stat.h>
// These are replacements for POSIX calls on Windows, ensuring that the UTF8 parameters are converted to UTF16 before being passed to Windows
CF_EXPORT int _NS_stat(const char *name, struct _stat *st);
CF_EXPORT int _NS_mkdir(const char *name);
CF_EXPORT int _NS_rmdir(const char *name);
CF_EXPORT int _NS_chmod(const char *name, int mode);
CF_EXPORT int _NS_unlink(const char *name);
CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
CF_EXPORT char *_NS_getenv(const char *name);
CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
CF_EXPORT int _NS_open(const char *name, int oflag, int pmode);
CF_EXPORT int _NS_chdir(const char *name);
CF_EXPORT int _NS_mkstemp(char *name, int bufSize);
CF_EXPORT int _NS_access(const char *name, int amode);
#define BOOL WINDOWS_BOOL
#define WIN32_LEAN_AND_MEAN
#ifndef WINVER
#define WINVER 0x0601
#endif
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0601
#endif
// The order of these includes is important
#define FD_SETSIZE 1024
#include <winsock2.h>
#include <windows.h>
#include <time.h>
#undef BOOL
#define __PRETTY_FUNCTION__ __FUNCTION__
#define malloc_default_zone() (void *)0
#define malloc_zone_from_ptr(a) (void *)0
#define malloc_zone_malloc(zone,size) malloc(size)
#define malloc_zone_memalign(zone,align,size) malloc(size)
#define malloc_zone_calloc(zone,count,size) calloc(count,size)
#define bcopy(b1,b2,len) memmove(b2, b1, (size_t)(len))
typedef int malloc_zone_t;
typedef int uid_t;
typedef int gid_t;
#define geteuid() 0
#define getuid() 0
#define getegid() 0
#define fsync(a) _commit(a)
#define malloc_create_zone(a,b) 123
#define malloc_set_zone_name(zone,name)
#define malloc_zone_realloc(zone,ptr,size) realloc(ptr,size)
#define malloc_zone_free(zone,ptr) free(ptr)
// implemented in CFInternal.h
#define OSSpinLockLock(A) __CFLock(A)
#define OSSpinLockUnlock(A) __CFUnlock(A)
typedef int32_t OSSpinLock;
#define OS_SPINLOCK_INIT 0
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
CF_INLINE size_t malloc_size(void *memblock) {
return _msize(memblock);
}
CF_INLINE uint64_t mach_absolute_time() {
LARGE_INTEGER count;
QueryPerformanceCounter(&count);
// mach_absolute_time is unsigned, but this function returns a signed value.
return (uint64_t)count.QuadPart;
}
CF_INLINE long long llabs(long long v) {
if (v < 0) return -v;
return v;
}
#define strtod_l(a,b,locale) strtod(a,b)
#define strtoul_l(a,b,c,locale) strtoul(a,b,c)
#define strtol_l(a,b,c,locale) strtol(a,b,c)
#define strtoll_l(a,b,c,locale) _strtoi64(a,b,c)
#define strncasecmp(a, b, c) _strnicmp(a, b, c)
#define strncasecmp_l(a, b, c, d) _strnicmp(a, b, c)
#define snprintf _snprintf
#define fprintf_l(a,locale,b,...) fprintf(a, b, __VA_ARGS__)
CF_INLINE size_t
strlcpy(char * dst, const char * src, size_t maxlen) {
const size_t srclen = strlen(src);
if (srclen < maxlen) {
memcpy(dst, src, srclen+1);
} else if (maxlen != 0) {
memcpy(dst, src, maxlen-1);
dst[maxlen-1] = '\0';
}
return srclen;
}
CF_INLINE size_t
strlcat(char * dst, const char * src, size_t maxlen) {
const size_t srclen = strlen(src);
const size_t dstlen = strnlen(dst, maxlen);
if (dstlen == maxlen) return maxlen+srclen;
if (srclen < maxlen-dstlen) {
memcpy(dst+dstlen, src, srclen+1);
} else {
memcpy(dst+dstlen, src, maxlen-dstlen-1);
dst[maxlen-1] = '\0';
}
return dstlen + srclen;
}
#define sleep(x) Sleep(1000*x)
#define issetugid() 0
// CF exports these useful atomic operation functions on Windows
CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue);
CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue);
CF_EXPORT int32_t OSAtomicAdd32( int32_t theAmount, volatile int32_t *theValue );
CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
CF_EXPORT bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
void OSMemoryBarrier();
/*
CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue );
CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue );
*/
//#ifndef NTDDI_VERSION
//#define NTDDI_VERSION NTDDI_WINXP
//#endif
#include <io.h>
#include <fcntl.h>
#include <errno.h>
#endif
#if !defined(CF_PRIVATE)
#define CF_PRIVATE extern __attribute__((__visibility__("hidden")))
#endif
// [FIXED_35517899] We can't currently support this, but would like to leave things annotated
#if !defined(CF_TEST_PRIVATE)
#define CF_TEST_PRIVATE CF_PRIVATE
#endif
#if TARGET_OS_LINUX || TARGET_OS_WIN32
#include <stdarg.h>
CF_INLINE int flsl( long mask ) {
int idx = 0;
while (mask != 0) mask = (unsigned long)mask >> 1, idx++;
return idx;
}
CF_INLINE int popcountll(long long x) {
int count = 0;
while (x) {
count++;
x &= x - 1; // reset LS1B
}
return count;
}
CF_PRIVATE int asprintf(char **ret, const char *format, ...);
#endif
#if TARGET_OS_WIN32 && defined(__cplusplus)
} // extern "C"
#endif
#endif // __COREFOUNDATION_PREFIX_H__