]> git.saurik.com Git - minimal.git/blob - stdlib.h
bc3461d8dded8e9a4aefae063360be77d23a1c8d
[minimal.git] / stdlib.h
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
38 #ifndef MINIMAL_STDLIB_H
39 #define MINIMAL_STDLIB_H
40
41 #ifdef __i386__
42 #define _breakpoint() \
43 __asm__ ("int $0x03")
44 #else
45 #define _breakpoint()
46 #endif
47
48 #ifdef __cplusplus
49 #define _assert_(e) \
50 throw e
51 #else
52 #define _assert_(e) \
53 exit(1)
54 #endif
55
56 #define _assert(expr) \
57 do if (!(expr)) { \
58 fprintf(stderr, "%s(%u): _assert(%u:%s)\n", __FILE__, __LINE__, errno, #expr); \
59 _breakpoint(); \
60 _assert_(#expr); \
61 } while (false)
62
63 #define _syscall(expr) ({ \
64 __typeof__(expr) value; \
65 do if ((long) (value = (expr)) != -1) \
66 break; \
67 else switch (errno) { \
68 case EINTR: \
69 continue; \
70 default: \
71 _assert(false); \
72 } while (true); \
73 value; \
74 })
75
76 #define _aprcall(expr) \
77 do { \
78 apr_status_t status((expr)); \
79 _assert(status == APR_SUCCESS); \
80 } while (false)
81
82 #define _forever \
83 for (;;)
84
85 #define _trace() \
86 fprintf(stderr, "_trace(%s:%u): %s\n", __FILE__, __LINE__, __FUNCTION__)
87
88 #define _not(type) \
89 ((type) ~ (type) 0)
90
91 #define _finline \
92 inline __attribute__((always_inline))
93 #define _disused \
94 __attribute__((unused))
95
96 #define _label__(x) _label ## x
97 #define _label_(y) _label__(y)
98 #define _label _label_(__LINE__)
99
100 #define _packed \
101 __attribute__((packed))
102
103 #ifdef __cplusplus
104
105 template <typename Type_>
106 struct Iterator_ {
107 typedef typename Type_::const_iterator Result;
108 };
109
110 #define _foreach(item, list) \
111 for (bool _stop(true); _stop; ) \
112 for (const __typeof__(list) &_list = (list); _stop; _stop = false) \
113 for (Iterator_<__typeof__(list)>::Result item = _list.begin(); item != _list.end(); ++item)
114
115 #endif
116
117 #include <errno.h>
118 #include <stdio.h>
119 #include <stdbool.h>
120 #include <stdlib.h>
121 #include <stdint.h>
122
123 #endif/*MINIMAL_STDLIB_H*/