utils: ensure that byte-order conversion functions evaluate the argument only once
[project/libubox.git] / utils.h
1 /*
2  * utils - misc libubox utility functions
3  *
4  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18
19 #ifndef __LIBUBOX_UTILS_H
20 #define __LIBUBOX_UTILS_H
21
22 #include <sys/types.h>
23 #include <sys/time.h>
24 #include <stdint.h>
25 #include <stdbool.h>
26 #include <unistd.h>
27 #include <time.h>
28
29 /*
30  * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
31  *
32  * allocate a block of memory big enough to hold multiple aligned objects.
33  * the pointer to the full object (starting with the first chunk) is returned,
34  * all other pointers are stored in the locations behind extra addr arguments.
35  * the last argument needs to be a NULL pointer
36  */
37
38 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
39
40 void *__calloc_a(size_t len, ...);
41
42 #ifndef ARRAY_SIZE
43 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 #endif
45
46 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
47
48 #ifdef __OPTIMIZE__
49 extern int __BUILD_BUG_ON_CONDITION_FAILED;
50 #define BUILD_BUG_ON(condition)                                 \
51         do {                                                    \
52                 __BUILD_BUG_ON(condition);                      \
53                 if (condition)                                  \
54                         __BUILD_BUG_ON_CONDITION_FAILED = 1;    \
55         } while(0)
56 #else
57 #define BUILD_BUG_ON __BUILD_BUG_ON
58 #endif
59
60 #if defined(__APPLE__) && !defined(CLOCK_MONOTONIC)
61 #define LIBUBOX_COMPAT_CLOCK_GETTIME
62
63 #include <mach/clock_types.h>
64 #define CLOCK_REALTIME  CALENDAR_CLOCK
65 #define CLOCK_MONOTONIC SYSTEM_CLOCK
66
67 int clock_gettime(int type, struct timespec *tv);
68
69 #endif
70
71 #ifdef __GNUC__
72 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
73 #else
74 #define _GNUC_MIN_VER(maj, min) 0
75 #endif
76
77 #if defined(__linux__) || defined(__CYGWIN__)
78 #include <byteswap.h>
79 #include <endian.h>
80
81 #elif defined(__APPLE__)
82 #include <machine/endian.h>
83 #include <machine/byte_order.h>
84 #elif defined(__FreeBSD__)
85 #include <sys/endian.h>
86 #else
87 #include <machine/endian.h>
88 #endif
89
90 #ifndef __BYTE_ORDER
91 #define __BYTE_ORDER BYTE_ORDER
92 #endif
93 #ifndef __BIG_ENDIAN
94 #define __BIG_ENDIAN BIG_ENDIAN
95 #endif
96 #ifndef __LITTLE_ENDIAN
97 #define __LITTLE_ENDIAN LITTLE_ENDIAN
98 #endif
99
100 #define __constant_swap16(x) ((uint16_t)(                               \
101         (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) |                    \
102         (((uint16_t)(x) & (uint16_t)0xff00U) >> 8)))
103
104 #define __constant_swap32(x) ((uint32_t)(                               \
105         (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) |              \
106         (((uint32_t)(x) & (uint32_t)0x0000ff00UL) <<  8) |              \
107         (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >>  8) |              \
108         (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24)))
109
110 #define __constant_swap64(x) ((uint64_t)(                               \
111         (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) |     \
112         (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) |     \
113         (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) |     \
114         (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) <<  8) |     \
115         (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >>  8) |     \
116         (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) |     \
117         (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) |     \
118         (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56)))
119
120 /*
121  * This returns a constant expression while determining if an argument is
122  * a constant expression, most importantly without evaluating the argument.
123  */
124 #define __is_constant(x)                                                \
125         (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1)))
126
127 #define __eval_once(func, x)                                            \
128         ({ typeof(x) __x = x; func(__x); })
129
130 #define __eval_safe(func, x)                                            \
131         __builtin_choose_expr(__is_constant(x),                         \
132                               func(x), __eval_once(func, x))
133
134 #if __BYTE_ORDER == __LITTLE_ENDIAN
135
136 #define cpu_to_be64(x) __eval_safe(__constant_swap64, x)
137 #define cpu_to_be32(x) __eval_safe(__constant_swap32, x)
138 #define cpu_to_be16(x) __eval_safe(__constant_swap16, x)
139
140 #define be64_to_cpu(x) __eval_safe(__constant_swap64, x)
141 #define be32_to_cpu(x) __eval_safe(__constant_swap32, x)
142 #define be16_to_cpu(x) __eval_safe(__constant_swap16, x)
143
144 #define cpu_to_le64(x) (x)
145 #define cpu_to_le32(x) (x)
146 #define cpu_to_le16(x) (x)
147
148 #define le64_to_cpu(x) (x)
149 #define le32_to_cpu(x) (x)
150 #define le16_to_cpu(x) (x)
151
152 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
153
154 #define cpu_to_le64(x) __eval_safe(__constant_swap64, x)
155 #define cpu_to_le32(x) __eval_safe(__constant_swap32, x)
156 #define cpu_to_le16(x) __eval_safe(__constant_swap16, x)
157
158 #define le64_to_cpu(x) __eval_safe(__constant_swap64, x)
159 #define le32_to_cpu(x) __eval_safe(__constant_swap32, x)
160 #define le16_to_cpu(x) __eval_safe(__constant_swap16, x)
161
162 #define cpu_to_be64(x) (x)
163 #define cpu_to_be32(x) (x)
164 #define cpu_to_be16(x) (x)
165
166 #define be64_to_cpu(x) (x)
167 #define be32_to_cpu(x) (x)
168 #define be16_to_cpu(x) (x)
169
170 #endif
171
172 #ifndef __packed
173 #define __packed __attribute__((packed))
174 #endif
175
176 #ifndef __constructor
177 #define __constructor __attribute__((constructor))
178 #endif
179
180 #ifndef __destructor
181 #define __destructor __attribute__((destructor))
182 #endif
183
184 #ifndef __hidden
185 #define __hidden __attribute__((visibility("hidden")))
186 #endif
187
188 int b64_encode(const void *src, size_t src_len,
189                void *dest, size_t dest_len);
190
191 int b64_decode(const void *src, void *dest, size_t dest_len);
192
193 #define B64_ENCODE_LEN(_len)    ((((_len) + 2) / 3) * 4 + 1)
194 #define B64_DECODE_LEN(_len)    (((_len) / 4) * 3 + 1)
195
196 static inline unsigned int cbuf_order(unsigned int x)
197 {
198         return 32 - __builtin_clz(x - 1);
199 }
200
201 static inline unsigned long cbuf_size(int order)
202 {
203         unsigned long page_size = sysconf(_SC_PAGESIZE);
204         unsigned long ret = 1ULL << order;
205
206         if (ret < page_size)
207                 ret = page_size;
208
209         return ret;
210 }
211
212 void *cbuf_alloc(unsigned int order);
213 void cbuf_free(void *ptr, unsigned int order);
214
215 #endif