utils: add helper functions useful for allocating a ring buffer
[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 #ifdef __APPLE__
61
62 #include <mach/clock_types.h>
63 #define CLOCK_REALTIME  CALENDAR_CLOCK
64 #define CLOCK_MONOTONIC SYSTEM_CLOCK
65
66 int clock_gettime(int type, struct timespec *tv);
67
68 #endif
69
70 #ifdef __GNUC__
71 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
72 #else
73 #define _GNUC_MIN_VER(maj, min) 0
74 #endif
75
76 #if defined(__linux__) || defined(__CYGWIN__)
77 #include <byteswap.h>
78 #include <endian.h>
79
80 #elif defined(__APPLE__)
81 #include <machine/endian.h>
82 #include <machine/byte_order.h>
83 #define bswap_32(x) OSSwapInt32(x)
84 #define bswap_64(x) OSSwapInt64(x)
85 #elif defined(__FreeBSD__)
86 #include <sys/endian.h>
87 #define bswap_32(x) bswap32(x)
88 #define bswap_64(x) bswap64(x)
89 #else
90 #include <machine/endian.h>
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 static inline uint16_t __u_bswap16(uint16_t val)
106 {
107         return ((val >> 8) & 0xffu) | ((val & 0xffu) << 8);
108 }
109
110 #if _GNUC_MIN_VER(4, 2)
111 #define __u_bswap32(x) __builtin_bswap32(x)
112 #define __u_bswap64(x) __builtin_bswap64(x)
113 #else
114 #define __u_bswap32(x) bswap_32(x)
115 #define __u_bswap64(x) bswap_64(x)
116 #endif
117
118 #if __BYTE_ORDER == __LITTLE_ENDIAN
119
120 #define cpu_to_be64(x) __u_bswap64(x)
121 #define cpu_to_be32(x) __u_bswap32(x)
122 #define cpu_to_be16(x) __u_bswap16((uint16_t) (x))
123
124 #define be64_to_cpu(x) __u_bswap64(x)
125 #define be32_to_cpu(x) __u_bswap32(x)
126 #define be16_to_cpu(x) __u_bswap16((uint16_t) (x))
127
128 #define cpu_to_le64(x) (x)
129 #define cpu_to_le32(x) (x)
130 #define cpu_to_le16(x) (x)
131
132 #define le64_to_cpu(x) (x)
133 #define le32_to_cpu(x) (x)
134 #define le16_to_cpu(x) (x)
135
136 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
137
138 #define cpu_to_le64(x) __u_bswap64(x)
139 #define cpu_to_le32(x) __u_bswap32(x)
140 #define cpu_to_le16(x) __u_bswap16((uint16_t) (x))
141
142 #define le64_to_cpu(x) __u_bswap64(x)
143 #define le32_to_cpu(x) __u_bswap32(x)
144 #define le16_to_cpu(x) __u_bswap16((uint16_t) (x))
145
146 #define cpu_to_be64(x) (x)
147 #define cpu_to_be32(x) (x)
148 #define cpu_to_be16(x) (x)
149
150 #define be64_to_cpu(x) (x)
151 #define be32_to_cpu(x) (x)
152 #define be16_to_cpu(x) (x)
153
154 #endif
155
156 #ifndef __packed
157 #define __packed __attribute__((packed))
158 #endif
159
160 #ifndef __constructor
161 #define __constructor __attribute__((constructor))
162 #endif
163
164 #ifndef __destructor
165 #define __destructor __attribute__((destructor))
166 #endif
167
168 #ifndef __hidden
169 #define __hidden __attribute__((visibility("hidden")))
170 #endif
171
172 #ifndef BITS_PER_LONG
173 #define BITS_PER_LONG (8 * sizeof(unsigned long))
174 #endif
175
176 #define BITFIELD_SIZE(_n) (((_n) + (BITS_PER_LONG - 1)) / BITS_PER_LONG)
177
178 static inline void bitfield_set(unsigned long *bits, int bit)
179 {
180         bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG));
181 }
182
183 static inline bool bitfield_test(unsigned long *bits, int bit)
184 {
185         return !!(bits[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG)));
186 }
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