]> git.saurik.com Git - apple/cf.git/blob - CoreFoundation_Prefix.h
CF-550.19.tar.gz
[apple/cf.git] / CoreFoundation_Prefix.h
1 /*
2 * Copyright (c) 2010 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-2009, 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 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_WINDOWS
43 #include <objc/objc.h>
44 #endif
45
46 #if DEPLOYMENT_TARGET_WINDOWS
47 #define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME)
48 #endif
49
50 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
51
52 #import <dispatch/dispatch.h>
53 #import <libkern/OSAtomic.h>
54 #import <pthread.h>
55
56 /* This macro creates 3 helper functions which are useful in dealing with libdispatch:
57 * __ PREFIX SyncDispatchIsSafe -- returns bool indicating whether calling dispatch_sync() would be safe from self-deadlock
58 * __ PREFIX Queue -- manages and returns a singleton serial queue
59 *
60 * Use the macro like this:
61 * DISPATCH_HELPER_FUNCTIONS(fh, NSFileHandle)
62 */
63
64 #define DISPATCH_HELPER_FUNCTIONS(PREFIX, QNAME) \
65 static Boolean __ ## PREFIX ## SyncDispatchIsSafe(dispatch_queue_t Q) { \
66 dispatch_queue_t C = dispatch_get_current_queue(); \
67 return (!C || Q != C) ? true : false; \
68 } \
69 \
70 static dispatch_queue_t __ ## PREFIX ## Queue(void) { \
71 static volatile dispatch_queue_t __ ## PREFIX ## dq = NULL; \
72 if (!__ ## PREFIX ## dq) { \
73 dispatch_queue_t dq = dispatch_queue_create(# QNAME, NULL); \
74 void * volatile *loc = (void * volatile *)&__ ## PREFIX ## dq; \
75 if (!OSAtomicCompareAndSwapPtrBarrier(NULL, dq, loc)) { \
76 dispatch_release(dq); \
77 } \
78 } \
79 return __ ## PREFIX ## dq; \
80 } \
81
82 #endif
83
84 #define LIBAUTO_STUB 1
85
86 #ifndef LIBAUTO_STUB
87
88 #if DEPLOYMENT_TARGET_MACOSX
89 #include <auto_zone.h>
90 #endif
91 #if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
92 #include <objc/objc-auto.h>
93 #endif
94
95 #endif // LIBAUTO_STUB
96
97 #if DEPLOYMENT_TARGET_WINDOWS
98 // Compatibility with boolean.h
99 #if defined(__x86_64__)
100 typedef unsigned int boolean_t;
101 #else
102 typedef int boolean_t;
103 #endif
104 #endif
105
106 #if DEPLOYMENT_TARGET_WINDOWS
107
108 #define MAXPATHLEN MAX_PATH
109 #undef MAX_PATH
110 #undef INVALID_HANDLE_VALUE
111
112 // Define MIN/MAX macros from NSObjCRuntime.h, which are only defined for GNUC
113 #if !defined(__GNUC__)
114
115 #if !defined(MIN)
116 #define MIN(A,B) ((A) < (B) ? (A) : (B))
117 #endif
118
119 #if !defined(MAX)
120 #define MAX(A,B) ((A) > (B) ? (A) : (B))
121 #endif
122
123 #if !defined(ABS)
124 #define ABS(A) ((A) < 0 ? (-(A)) : (A))
125 #endif
126
127 #endif
128
129 // Defined for source compatibility
130 #define ino_t _ino_t
131 #define off_t _off_t
132 #define mode_t uint16_t
133
134 // 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
135 #define __private_extern__ extern
136
137 #define __builtin_expect(P1,P2) P1
138
139 // These are replacements for POSIX calls on Windows, ensuring that the UTF8 parameters are converted to UTF16 before being passed to Windows
140 CF_EXPORT int _NS_stat(const char *name, struct _stat *st);
141 CF_EXPORT int _NS_mkdir(const char *name);
142 CF_EXPORT int _NS_rmdir(const char *name);
143 CF_EXPORT int _NS_chmod(const char *name, int mode);
144 CF_EXPORT int _NS_unlink(const char *name);
145 CF_EXPORT char *_NS_getcwd(char *dstbuf, size_t size); // Warning: this doesn't support dstbuf as null even though 'getcwd' does
146 CF_EXPORT char *_NS_getenv(const char *name);
147 CF_EXPORT int _NS_rename(const char *oldName, const char *newName);
148 CF_EXPORT int _NS_open(const char *name, int oflag, int pmode = 0);
149 CF_EXPORT int _NS_chdir(const char *name);
150 CF_EXPORT int _NS_mkstemp(char *name, int bufSize);
151 CF_EXPORT int _NS_access(const char *name, int amode);
152
153 #define BOOL WINDOWS_BOOL
154
155 #define WIN32_LEAN_AND_MEAN
156
157 #ifndef WINVER
158 #define WINVER 0x0501
159 #endif
160
161 #ifndef _WIN32_WINNT
162 #define _WIN32_WINNT 0x0501
163 #endif
164
165 // The order of these includes is important
166 #include <winsock2.h>
167 #include <windows.h>
168
169 #undef BOOL
170
171 #ifndef HAVE_STRUCT_TIMESPEC
172 #define HAVE_STRUCT_TIMESPEC 1
173 struct timespec {
174 long tv_sec;
175 long tv_nsec;
176 };
177 #endif /* HAVE_STRUCT_TIMESPEC */
178
179 #define __PRETTY_FUNCTION__ __FUNCTION__
180
181 #define malloc_default_zone() (void *)0
182 #define malloc_zone_from_ptr(a) (void *)0
183 #define malloc_zone_malloc(zone,size) malloc(size)
184 #define malloc_zone_calloc(zone,count,size) calloc(count,size)
185 #define bcopy(b1,b2,len) memmove(b2, b1, (size_t)(len))
186 typedef int malloc_zone_t;
187 typedef int uid_t;
188 typedef int gid_t;
189 #define geteuid() 0
190 #define getuid() 0
191 #define getegid() 0
192
193 #define fsync(a) _commit(a)
194 #define malloc_create_zone(a,b) 123
195 #define malloc_set_zone_name(zone,name)
196 #define malloc_zone_realloc(zone,ptr,size) realloc(ptr,size)
197 #define malloc_zone_free(zone,ptr) free(ptr)
198
199 // implemented in CFInternal.h
200 #define OSSpinLockLock(A) __CFSpinLock(A)
201 #define OSSpinLockUnlock(A) __CFSpinUnlock(A)
202
203 typedef int32_t OSSpinLock;
204
205 #define OS_SPINLOCK_INIT 0
206
207 #include <stdint.h>
208 #include <stdbool.h>
209 //#include <search.h>
210 #include <stdio.h>
211 #include <stdarg.h>
212 #include <malloc.h>
213 CF_INLINE size_t malloc_size(void *memblock) {
214 return _msize(memblock);
215 }
216
217 #define mach_absolute_time() ((uint64_t)(CFAbsoluteTimeGetCurrent() * 1000000000.0))
218
219 extern int pthread_main_np();
220
221 #define strtod_l(a,b,locale) strtod(a,b)
222 #define strtoul_l(a,b,c,locale) strtoul(a,b,c)
223 #define strtol_l(a,b,c,locale) strtol(a,b,c)
224
225 #define _fprintf_l(a,b,locale,...) fprintf(a, b, __VA_ARGS__)
226
227 #define strlcat(a,b,c) strncat(a,b,c)
228
229 #define issetugid() 0
230
231 // CF exports these useful atomic operation functions on Windows
232 CF_EXPORT bool OSAtomicCompareAndSwapPtr(void *oldp, void *newp, void *volatile *dst);
233 CF_EXPORT bool OSAtomicCompareAndSwapLong(long oldl, long newl, long volatile *dst);
234 CF_EXPORT bool OSAtomicCompareAndSwapPtrBarrier(void *oldp, void *newp, void *volatile *dst);
235
236 CF_EXPORT int32_t OSAtomicDecrement32Barrier(volatile int32_t *dst);
237 CF_EXPORT int32_t OSAtomicIncrement32Barrier(volatile int32_t *dst);
238 CF_EXPORT int32_t OSAtomicIncrement32(volatile int32_t *theValue);
239 CF_EXPORT int32_t OSAtomicDecrement32(volatile int32_t *theValue);
240
241 CF_EXPORT int32_t OSAtomicAdd32Barrier( int32_t theAmount, volatile int32_t *theValue );
242 CF_EXPORT bool OSAtomicCompareAndSwap32Barrier( int32_t oldValue, int32_t newValue, volatile int32_t *theValue );
243
244 /*
245 CF_EXPORT bool OSAtomicCompareAndSwap64( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
246 CF_EXPORT bool OSAtomicCompareAndSwap64Barrier( int64_t __oldValue, int64_t __newValue, volatile int64_t *__theValue );
247
248 CF_EXPORT int64_t OSAtomicAdd64( int64_t __theAmount, volatile int64_t *__theValue );
249 CF_EXPORT int64_t OSAtomicAdd64Barrier( int64_t __theAmount, volatile int64_t *__theValue );
250 */
251
252 #define sleep(x) Sleep(1000*x)
253 #define strlcpy strncpy
254
255 //#ifndef NTDDI_VERSION
256 //#define NTDDI_VERSION NTDDI_WINXP
257 //#endif
258
259 #include <io.h>
260 #include <fcntl.h>
261 #include <errno.h>
262
263 static __inline int flsl( long mask ) {
264 int idx = 0;
265 while (mask != 0) mask = (unsigned long)mask >> 1, idx++;
266 return idx;
267 }
268
269 static __inline int asprintf(char **ret, const char *format, ...) {
270 va_list args;
271 size_t sz = 1024;
272 *ret = (char *) malloc(sz * sizeof(char));
273 if (!*ret) return -1;
274 va_start(args, format);
275 int cnt = vsnprintf(*ret, sz, format, args);
276 va_end(args);
277 if (cnt < sz - 1) return cnt;
278 sz = cnt + 8;
279 char *oldret = *ret;
280 *ret = (char *) realloc(*ret, sz * sizeof(char));
281 if (!*ret && oldret) free(oldret);
282 if (!*ret) return -1;
283 va_start(args, format);
284 cnt = vsnprintf(*ret, sz, format, args);
285 va_end(args);
286 if (cnt < sz - 1) return cnt;
287 free(*ret);
288 *ret = NULL;
289 return -1;
290 }
291
292 #define __unused
293
294 #define __weak
295 #define __strong
296
297 #endif
298
299 #ifdef LIBAUTO_STUB
300
301 /* Stubs for functions in libauto. */
302
303 enum {OBJC_GENERATIONAL = (1 << 0)};
304
305 enum {
306 OBJC_RATIO_COLLECTION = (0 << 0),
307 OBJC_GENERATIONAL_COLLECTION = (1 << 0),
308 OBJC_FULL_COLLECTION = (2 << 0),
309 OBJC_EXHAUSTIVE_COLLECTION = (3 << 0),
310 OBJC_COLLECT_IF_NEEDED = (1 << 3),
311 OBJC_WAIT_UNTIL_DONE = (1 << 4),
312 };
313
314
315 enum { AUTO_TYPE_UNKNOWN = -1, AUTO_UNSCANNED = 1, AUTO_OBJECT = 2, AUTO_MEMORY_SCANNED = 0, AUTO_MEMORY_UNSCANNED = AUTO_UNSCANNED, AUTO_OBJECT_SCANNED = AUTO_OBJECT, AUTO_OBJECT_UNSCANNED = AUTO_OBJECT | AUTO_UNSCANNED };
316 typedef unsigned long auto_memory_type_t;
317 typedef struct _auto_zone_t auto_zone_t;
318 typedef struct auto_weak_callback_block {
319 struct auto_weak_callback_block *next;
320 void (*callback_function)(void *arg1, void *arg2);
321 void *arg1;
322 void *arg2;
323 } auto_weak_callback_block_t;
324
325 CF_INLINE void *objc_memmove_collectable(void *a, const void *b, size_t c) { return memmove(a, b, c); }
326
327 CF_INLINE auto_zone_t *auto_zone(void) { return 0; }
328 CF_INLINE void *auto_zone_allocate_object(void *zone, size_t size, auto_memory_type_t type, int rc, int clear) { return 0; }
329 CF_INLINE const void *auto_zone_base_pointer(void *zone, const void *ptr) { return 0; }
330 CF_INLINE void auto_zone_retain(void *zone, void *ptr) {}
331 CF_INLINE unsigned int auto_zone_release(void *zone, void *ptr) { return 0; }
332 CF_INLINE unsigned int auto_zone_retain_count(void *zone, const void *ptr) { return 0; }
333 CF_INLINE void auto_zone_set_unscanned(auto_zone_t *zone, void *ptr) {}
334 CF_INLINE void auto_zone_set_nofinalize(auto_zone_t *zone, void *ptr) {}
335 CF_INLINE int auto_zone_is_finalized(void *zone, const void *ptr) { return 0; }
336 CF_INLINE size_t auto_zone_size(void *zone, const void *ptr) { return 0; }
337 CF_INLINE void auto_register_weak_reference(void *zone, const void *referent, void **referrer, uintptr_t *counter, void **listHead, void **listElement) {}
338 CF_INLINE void auto_unregister_weak_reference(void *zone, const void *referent, void **referrer) {}
339 CF_INLINE void auto_zone_register_thread(void *zone) {}
340 CF_INLINE void auto_zone_unregister_thread(void *zone) {}
341 CF_INLINE int auto_zone_is_valid_pointer(void *zone, const void *ptr) { return 0; }
342 CF_INLINE BOOL objc_isAuto(id object) { return 0; }
343 CF_INLINE void* auto_read_weak_reference(void *zone, void **referrer) { void *result = *referrer; return result; }
344 CF_INLINE void auto_assign_weak_reference(void *zone, const void *referent, void **referrer, auto_weak_callback_block_t *block) { *referrer = (void *)referent; }
345 CF_INLINE auto_memory_type_t auto_zone_get_layout_type(void *zone, void *ptr) { return AUTO_UNSCANNED; }
346 CF_INLINE boolean_t auto_zone_set_write_barrier(auto_zone_t *zone, const void *dest, const void *new_value) { return false; }
347
348 CF_INLINE void objc_assertRegisteredThreadWithCollector(void) {}
349
350 // from objc-auto.h
351
352 static OBJC_INLINE BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation)
353 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
354
355 static OBJC_INLINE BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation)
356 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
357
358 static OBJC_INLINE BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation)
359 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
360
361 static OBJC_INLINE BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation)
362 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
363
364 static OBJC_INLINE BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation)
365 { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
366
367 static OBJC_INLINE BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation)
368 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
369
370 static OBJC_INLINE id objc_assign_strongCast(id val, id *dest)
371 { return (*dest = val); }
372
373 static OBJC_INLINE id objc_assign_global(id val, id *dest)
374 { return (*dest = val); }
375
376 static OBJC_INLINE id objc_assign_ivar(id val, id dest, ptrdiff_t offset)
377 { return (*(id*)((char *)dest+offset) = val); }
378
379 //static OBJC_INLINE void *objc_memmove_collectable(void *dst, const void *src, size_t size) { return memmove(dst, src, size); }
380
381 static OBJC_INLINE id objc_read_weak(id *location)
382 { return *location; }
383
384 static OBJC_INLINE id objc_assign_weak(id value, id *location)
385 { return (*location = value); }
386
387
388 static OBJC_INLINE void objc_finalizeOnMainThread(Class cls) { }
389 static OBJC_INLINE BOOL objc_is_finalized(void *ptr) { return NO; }
390 static OBJC_INLINE void objc_clear_stack(unsigned long options) { }
391
392 static OBJC_INLINE BOOL objc_collectingEnabled(void) { return NO; }
393 static OBJC_INLINE void objc_start_collector_thread(void) { }
394
395 static OBJC_INLINE void objc_collect(unsigned long options) { }
396
397 #endif
398
399 // Need to use the _O_BINARY flag on Windows to get the correct behavior
400 #if DEPLOYMENT_TARGET_WINDOWS
401 #define CF_OPENFLGS (_O_BINARY|_O_NOINHERIT)
402 #else
403 #define CF_OPENFLGS (0)
404 #endif
405
406
407 #if DEPLOYMENT_TARGET_WINDOWS && defined(__cplusplus)
408 } // extern "C"
409 #endif