]>
Commit | Line | Data |
---|---|---|
b1ab9ed8 A |
1 | #ifndef __UTILITIES__ |
2 | #define __UTILITIES__ | |
3 | ||
4 | #ifdef __cplusplus | |
5 | extern "C" { | |
6 | #endif | |
7 | ||
8 | ||
9 | #include <CoreFoundation/CoreFoundation.h> | |
10 | ||
11 | ||
12 | void MyDispatchAsync(dispatch_queue_t queue, void(^block)(void)); | |
13 | dispatch_queue_t MyDispatchQueueCreate(const char* name, dispatch_queue_attr_t attr); | |
14 | ||
15 | CFErrorRef CreateGenericErrorRef(CFStringRef domain, int errorCode, const char* format, ...); | |
16 | CFErrorRef CreateSecTransformErrorRef(int errorCode, const char* format, ...); | |
17 | CFErrorRef CreateSecTransformErrorRefWithCFType(int errorCode, CFTypeRef errorMsg); | |
18 | ||
19 | CFTypeRef DebugRetain(const void* owner, CFTypeRef type); | |
20 | void DebugRelease(const void* owner, CFTypeRef type); | |
21 | ||
22 | void transforms_bug(size_t line, long val) __attribute__((__noinline__)); | |
23 | ||
24 | // Borrowed form libdispatch, fastpath's x should normally be true; slowpath's x should normally be false. | |
25 | // The compiler will generate correct code in either case, but it is faster if you hint right. If you | |
26 | // hint wrong it could be slower then no hints. | |
27 | #define fastpath(x) ((typeof(x))__builtin_expect((long)(x), ~0l)) | |
28 | #define slowpath(x) ((typeof(x))__builtin_expect((long)(x), 0l)) | |
29 | ||
30 | /* | |
31 | * Borrowed (with minor changes) from libdispatch. | |
32 | * | |
33 | * For reporting bugs or impedance mismatches between SecTransform and external subsystems, | |
34 | * and for internal consistency failures. These do NOT abort(), and are always compiled | |
35 | * into the product. | |
36 | * | |
37 | * Libdispatch wraps all system-calls with assume() macros, and we ought to do the same | |
38 | * (but don't yet). | |
39 | */ | |
40 | #define transforms_assume(e) ({ \ | |
41 | typeof(e) _e = fastpath(e); /* always eval 'e' */ \ | |
42 | if (!_e) { \ | |
43 | if (__builtin_constant_p(e)) { \ | |
44 | char __compile_time_assert__[(e) ? 1 : -1]; \ | |
45 | (void)__compile_time_assert__; \ | |
46 | } \ | |
47 | transforms_bug(__LINE__, (long)_e); \ | |
48 | } \ | |
49 | _e; \ | |
50 | }) | |
51 | ||
52 | /* A lot of API return zero upon success and not-zero on fail. Let's capture and log the non-zero value */ | |
53 | #define transforms_assume_zero(e) ({ \ | |
54 | typeof(e) _e = slowpath(e); /* always eval 'e' */ \ | |
55 | if (_e) { \ | |
56 | if (__builtin_constant_p(e)) { \ | |
57 | char __compile_time_assert__[(e) ? -1 : 1]; \ | |
58 | (void)__compile_time_assert__; \ | |
59 | } \ | |
60 | transforms_bug(__LINE__, (long)_e); \ | |
61 | } \ | |
62 | _e; \ | |
63 | }) | |
64 | ||
65 | #ifdef __cplusplus | |
66 | }; | |
67 | #endif | |
68 | ||
69 | #endif |