source: trunk/debian/packages/libubox/trunk/utils.h @ 857

Last change on this file since 857 was 857, checked in by amain, 4 years ago

libubox: initial import / part 4

File size: 5.0 KB
Line 
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 <time.h>
27
28/*
29 * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
30 *
31 * allocate a block of memory big enough to hold multiple aligned objects.
32 * the pointer to the full object (starting with the first chunk) is returned,
33 * all other pointers are stored in the locations behind extra addr arguments.
34 * the last argument needs to be a NULL pointer
35 */
36
37#define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
38
39void *__calloc_a(size_t len, ...);
40
41#ifndef ARRAY_SIZE
42#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
43#endif
44
45#define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
46
47#ifdef __OPTIMIZE__
48extern int __BUILD_BUG_ON_CONDITION_FAILED;
49#define BUILD_BUG_ON(condition)                                 \
50        do {                                                    \
51                __BUILD_BUG_ON(condition);                      \
52                if (condition)                                  \
53                        __BUILD_BUG_ON_CONDITION_FAILED = 1;    \
54        } while(0)
55#else
56#define BUILD_BUG_ON __BUILD_BUG_ON
57#endif
58
59#ifdef __APPLE__
60
61#include <mach/clock_types.h>
62#define CLOCK_REALTIME  CALENDAR_CLOCK
63#define CLOCK_MONOTONIC SYSTEM_CLOCK
64
65int clock_gettime(int type, struct timespec *tv);
66
67#endif
68
69#ifdef __GNUC__
70#define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
71#else
72#define _GNUC_MIN_VER(maj, min) 0
73#endif
74
75#if defined(__linux__) || defined(__CYGWIN__)
76#include <byteswap.h>
77#include <endian.h>
78
79#elif defined(__APPLE__)
80#include <machine/endian.h>
81#include <machine/byte_order.h>
82#define bswap_32(x) OSSwapInt32(x)
83#define bswap_64(x) OSSwapInt64(x)
84#elif defined(__FreeBSD__)
85#include <sys/endian.h>
86#define bswap_32(x) bswap32(x)
87#define bswap_64(x) bswap64(x)
88#else
89#include <machine/endian.h>
90#define bswap_32(x) swap32(x)
91#define bswap_64(x) swap64(x)
92#endif
93
94#ifndef __BYTE_ORDER
95#define __BYTE_ORDER BYTE_ORDER
96#endif
97#ifndef __BIG_ENDIAN
98#define __BIG_ENDIAN BIG_ENDIAN
99#endif
100#ifndef __LITTLE_ENDIAN
101#define __LITTLE_ENDIAN LITTLE_ENDIAN
102#endif
103
104static inline uint16_t __u_bswap16(uint16_t val)
105{
106        return ((val >> 8) & 0xffu) | ((val & 0xffu) << 8);
107}
108
109#if _GNUC_MIN_VER(4, 2)
110#define __u_bswap32(x) __builtin_bswap32(x)
111#define __u_bswap64(x) __builtin_bswap64(x)
112#else
113#define __u_bswap32(x) bswap_32(x)
114#define __u_bswap64(x) bswap_64(x)
115#endif
116
117#if __BYTE_ORDER == __LITTLE_ENDIAN
118
119#define cpu_to_be64(x) __u_bswap64(x)
120#define cpu_to_be32(x) __u_bswap32(x)
121#define cpu_to_be16(x) __u_bswap16((uint16_t) (x))
122
123#define be64_to_cpu(x) __u_bswap64(x)
124#define be32_to_cpu(x) __u_bswap32(x)
125#define be16_to_cpu(x) __u_bswap16((uint16_t) (x))
126
127#define cpu_to_le64(x) (x)
128#define cpu_to_le32(x) (x)
129#define cpu_to_le16(x) (x)
130
131#define le64_to_cpu(x) (x)
132#define le32_to_cpu(x) (x)
133#define le16_to_cpu(x) (x)
134
135#else /* __BYTE_ORDER == __LITTLE_ENDIAN */
136
137#define cpu_to_le64(x) __u_bswap64(x)
138#define cpu_to_le32(x) __u_bswap32(x)
139#define cpu_to_le16(x) __u_bswap16((uint16_t) (x))
140
141#define le64_to_cpu(x) __u_bswap64(x)
142#define le32_to_cpu(x) __u_bswap32(x)
143#define le16_to_cpu(x) __u_bswap16((uint16_t) (x))
144
145#define cpu_to_be64(x) (x)
146#define cpu_to_be32(x) (x)
147#define cpu_to_be16(x) (x)
148
149#define be64_to_cpu(x) (x)
150#define be32_to_cpu(x) (x)
151#define be16_to_cpu(x) (x)
152
153#endif
154
155#ifndef __packed
156#define __packed __attribute__((packed))
157#endif
158
159#ifndef __constructor
160#define __constructor __attribute__((constructor))
161#endif
162
163#ifndef __destructor
164#define __destructor __attribute__((destructor))
165#endif
166
167#ifndef __hidden
168#define __hidden __attribute__((visibility("hidden")))
169#endif
170
171#ifndef BITS_PER_LONG
172#define BITS_PER_LONG (8 * sizeof(unsigned long))
173#endif
174
175#define BITFIELD_SIZE(_n) (((_n) + (BITS_PER_LONG - 1)) / BITS_PER_LONG)
176
177static inline void bitfield_set(unsigned long *bits, int bit)
178{
179        bits[bit / BITS_PER_LONG] |= (1UL << (bit % BITS_PER_LONG));
180}
181
182static inline bool bitfield_test(unsigned long *bits, int bit)
183{
184        return !!(bits[bit / BITS_PER_LONG] & (1UL << (bit % BITS_PER_LONG)));
185}
186
187int b64_encode(const void *src, size_t src_len,
188               void *dest, size_t dest_len);
189
190int b64_decode(const void *src, void *dest, size_t dest_len);
191
192#define B64_ENCODE_LEN(_len)    ((((_len) + 2) / 3) * 4 + 1)
193#define B64_DECODE_LEN(_len)    (((_len) / 4) * 3 + 1)
194
195#endif
Note: See TracBrowser for help on using the repository browser.