11783bc224f551ba27d4583a271dfc7460053804
[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 <time.h>
26
27 /*
28  * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
29  *
30  * allocate a block of memory big enough to hold multiple aligned objects.
31  * the pointer to the full object (starting with the first chunk) is returned,
32  * all other pointers are stored in the locations behind extra addr arguments.
33  * the last argument needs to be a NULL pointer
34  */
35
36 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
37
38 void *__calloc_a(size_t len, ...);
39
40 #ifndef ARRAY_SIZE
41 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
42 #endif
43
44 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
45
46 #ifdef __OPTIMIZE__
47 extern int __BUILD_BUG_ON_CONDITION_FAILED;
48 #define BUILD_BUG_ON(condition)                                 \
49         do {                                                    \
50                 __BUILD_BUG_ON(condition);                      \
51                 if (condition)                                  \
52                         __BUILD_BUG_ON_CONDITION_FAILED = 1;    \
53         } while(0)
54 #else
55 #define BUILD_BUG_ON __BUILD_BUG_ON
56 #endif
57
58 #ifdef __APPLE__
59
60 #define CLOCK_REALTIME  0
61 #define CLOCK_MONOTONIC 1
62
63 void clock_gettime(int type, struct timespec *tv);
64
65 #endif
66
67 #ifdef __GNUC__
68 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
69 #else
70 #define _GNUC_MIN_VER(maj, min) 0
71 #endif
72
73 #if defined(__linux__) || defined(__CYGWIN__)
74 #include <byteswap.h>
75 #include <endian.h>
76
77 #elif defined(__APPLE__)
78 #include <machine/endian.h>
79 #include <machine/byte_order.h>
80 #define bswap_16(x) OSSwapInt16(x)
81 #define bswap_32(x) OSSwapInt32(x)
82 #define bswap_64(x) OSSwapInt64(x)
83 #elif defined(__FreeBSD__)
84 #include <sys/endian.h>
85 #define bswap_16(x) bswap16(x)
86 #define bswap_32(x) bswap32(x)
87 #define bswap_64(x) bswap64(x)
88 #else
89 #include <machine/endian.h>
90 #define bswap_16(x) swap16(x)
91 #define bswap_32(x) swap32(x)
92 #define bswap_64(x) swap64(x)
93 #endif
94
95 #ifndef __BYTE_ORDER
96 #define __BYTE_ORDER BYTE_ORDER
97 #endif
98 #ifndef __BIG_ENDIAN
99 #define __BIG_ENDIAN BIG_ENDIAN
100 #endif
101 #ifndef __LITTLE_ENDIAN
102 #define __LITTLE_ENDIAN LITTLE_ENDIAN
103 #endif
104
105 #if __BYTE_ORDER == __LITTLE_ENDIAN
106
107 #if _GNUC_MIN_VER(4, 2)
108
109 #define __bswap_constant_16(x) ((uint16_t)((((x) >> 8) & 0xffu) | (((x) & 0xffu) << 8)))
110
111 #define cpu_to_be64(x) __builtin_bswap64(x)
112 #define cpu_to_be32(x) __builtin_bswap32(x)
113 #define cpu_to_be16(x) __bswap_constant_16((uint16_t) x)
114
115 #define be64_to_cpu(x) __builtin_bswap64(x)
116 #define be32_to_cpu(x) __builtin_bswap32(x)
117 #define be16_to_cpu(x) __bswap_constant_16((uint16_t) x)
118
119 #else /* __GNUC__ */
120
121 #define cpu_to_be64(x) bswap_64(x)
122 #define cpu_to_be32(x) bswap_32(x)
123 #define cpu_to_be16(x) bswap_16(x)
124
125 #define be64_to_cpu(x) bswap_64(x)
126 #define be32_to_cpu(x) bswap_32(x)
127 #define be16_to_cpu(x) bswap_16(x)
128
129 #endif /* __GNUC__ */
130
131 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
132
133 #define cpu_to_be64(x) (x)
134 #define cpu_to_be32(x) (x)
135 #define cpu_to_be16(x) (x)
136
137 #define be64_to_cpu(x) (x)
138 #define be32_to_cpu(x) (x)
139 #define be16_to_cpu(x) (x)
140
141 #endif
142
143 #ifndef __packed
144 #define __packed __attribute__((packed))
145 #endif
146
147 #endif