]>
Commit | Line | Data |
---|---|---|
ee9c93b9 JF |
1 | /* Minimal - the simplest thing that could possibly work |
2 | * Copyright (C) 2007 Jay Freeman (saurik) | |
3 | */ | |
4 | ||
5 | /* | |
6 | * Redistribution and use in source and binary | |
7 | * forms, with or without modification, are permitted | |
8 | * provided that the following conditions are met: | |
9 | * | |
10 | * 1. Redistributions of source code must retain the | |
11 | * above copyright notice, this list of conditions | |
12 | * and the following disclaimer. | |
13 | * 2. Redistributions in binary form must reproduce the | |
14 | * above copyright notice, this list of conditions | |
15 | * and the following disclaimer in the documentation | |
16 | * and/or other materials provided with the | |
17 | * distribution. | |
18 | * 3. The name of the author may not be used to endorse | |
19 | * or promote products derived from this software | |
20 | * without specific prior written permission. | |
21 | * | |
22 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' | |
23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | |
24 | * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
25 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE | |
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
28 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
29 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
32 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR | |
33 | * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN | |
34 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
35 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
36 | */ | |
37 | ||
64c5d5aa JF |
38 | #ifndef MINIMAL_STDLIB_H |
39 | #define MINIMAL_STDLIB_H | |
40 | ||
a5a48954 JF |
41 | #ifdef __i386__ |
42 | #define _breakpoint() \ | |
43 | __asm__ ("int $0x03") | |
44 | #else | |
45 | #define _breakpoint() | |
46 | #endif | |
47 | ||
48 | #ifdef __cplusplus | |
92794285 JF |
49 | #define _assert___(line) \ |
50 | #line | |
51 | #define _assert__(line) \ | |
52 | _assert___(line) | |
a5a48954 | 53 | #define _assert_(e) \ |
92794285 | 54 | throw __FILE__ "(" _assert__(__LINE__) "): _assert(" e ")" |
a5a48954 JF |
55 | #else |
56 | #define _assert_(e) \ | |
57 | exit(1) | |
58 | #endif | |
59 | ||
d4987f7d JF |
60 | #define _assert(expr) \ |
61 | do if (!(expr)) { \ | |
92794285 | 62 | fprintf(stderr, "%s(%u): _assert(%s); errno=%u\n", __FILE__, __LINE__, #expr, errno); \ |
a5a48954 JF |
63 | _breakpoint(); \ |
64 | _assert_(#expr); \ | |
d4987f7d JF |
65 | } while (false) |
66 | ||
d28d515c | 67 | #define _syscall(expr) ({ \ |
66b708b1 JF |
68 | __typeof__(expr) _value; \ |
69 | do if ((long) (_value = (expr)) != -1) \ | |
d4987f7d JF |
70 | break; \ |
71 | else switch (errno) { \ | |
72 | case EINTR: \ | |
73 | continue; \ | |
74 | default: \ | |
75 | _assert(false); \ | |
d28d515c | 76 | } while (true); \ |
66b708b1 | 77 | _value; \ |
d28d515c JF |
78 | }) |
79 | ||
80 | #define _aprcall(expr) \ | |
81 | do { \ | |
acfdbc10 JF |
82 | apr_status_t _aprstatus((expr)); \ |
83 | _assert(_aprstatus == APR_SUCCESS); \ | |
d28d515c | 84 | } while (false) |
d4987f7d JF |
85 | |
86 | #define _forever \ | |
87 | for (;;) | |
88 | ||
89 | #define _trace() \ | |
9f1a211e | 90 | fprintf(stderr, "_trace(%s:%u): %s\n", __FILE__, __LINE__, __FUNCTION__) |
d4987f7d | 91 | |
3ed54889 JF |
92 | #define _not(type) \ |
93 | ((type) ~ (type) 0) | |
94 | ||
1a8261e0 JF |
95 | #define _finline \ |
96 | inline __attribute__((always_inline)) | |
64c5d5aa | 97 | #define _disused \ |
2b559b76 JF |
98 | __attribute__((unused)) |
99 | ||
a5a48954 JF |
100 | #define _label__(x) _label ## x |
101 | #define _label_(y) _label__(y) | |
102 | #define _label _label_(__LINE__) | |
103 | ||
104 | #define _packed \ | |
105 | __attribute__((packed)) | |
106 | ||
107 | #ifdef __cplusplus | |
108 | ||
109 | template <typename Type_> | |
110 | struct Iterator_ { | |
111 | typedef typename Type_::const_iterator Result; | |
112 | }; | |
113 | ||
114 | #define _foreach(item, list) \ | |
115 | for (bool _stop(true); _stop; ) \ | |
116 | for (const __typeof__(list) &_list = (list); _stop; _stop = false) \ | |
df8f0a21 | 117 | for (Iterator_<__typeof__(list)>::Result _item = _list.begin(); _item != _list.end(); ++_item) \ |
e327d27b | 118 | for (bool _suck(true); _suck; _suck = false) \ |
df8f0a21 | 119 | for (const __typeof__(*_item) &item = *_item; _suck; _suck = false) |
a5a48954 JF |
120 | |
121 | #endif | |
122 | ||
20363569 | 123 | #include <errno.h> |
d4987f7d JF |
124 | #include <stdio.h> |
125 | #include <stdbool.h> | |
126 | #include <stdlib.h> | |
127 | #include <stdint.h> | |
64c5d5aa JF |
128 | |
129 | #endif/*MINIMAL_STDLIB_H*/ |