2 * utils - misc libubox utility functions
4 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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.
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.
19 #ifndef __LIBUBOX_UTILS_H
20 #define __LIBUBOX_UTILS_H
22 #include <sys/types.h>
28 * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
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
36 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
38 void *__calloc_a(size_t len, ...);
41 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
44 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
47 extern int __BUILD_BUG_ON_CONDITION_FAILED;
48 #define BUILD_BUG_ON(condition) \
50 __BUILD_BUG_ON(condition); \
52 __BUILD_BUG_ON_CONDITION_FAILED = 1; \
55 #define BUILD_BUG_ON __BUILD_BUG_ON
60 #define CLOCK_REALTIME 0
61 #define CLOCK_MONOTONIC 1
63 void clock_gettime(int type, struct timespec *tv);
68 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min)))
70 #define _GNUC_MIN_VER(maj, min) 0
73 #if defined(__linux__) || defined(__CYGWIN__)
77 #elif defined(__APPLE__)
78 #include <machine/endian.h>
79 #include <machine/byte_order.h>
80 #define bswap_32(x) OSSwapInt32(x)
81 #define bswap_64(x) OSSwapInt64(x)
82 #elif defined(__FreeBSD__)
83 #include <sys/endian.h>
84 #define bswap_32(x) bswap32(x)
85 #define bswap_64(x) bswap64(x)
87 #include <machine/endian.h>
88 #define bswap_32(x) swap32(x)
89 #define bswap_64(x) swap64(x)
93 #define __BYTE_ORDER BYTE_ORDER
96 #define __BIG_ENDIAN BIG_ENDIAN
98 #ifndef __LITTLE_ENDIAN
99 #define __LITTLE_ENDIAN LITTLE_ENDIAN
102 #define __u_bswap16(x) ({ uint16_t val = (x); ((uint16_t)(((val >> 8) & 0xffu) | ((val & 0xffu) << 8))); })
104 #if _GNUC_MIN_VER(4, 2)
105 #define __u_bswap32(x) __builtin_bswap32(x)
106 #define __u_bswap64(x) __builtin_bswap64(x)
108 #define __u_bswap32(x) bswap_32(x)
109 #define __u_bswap64(x) bswap_64(x)
112 #if __BYTE_ORDER == __LITTLE_ENDIAN
114 #define cpu_to_be64(x) __u_bswap64(x)
115 #define cpu_to_be32(x) __u_bswap32(x)
116 #define cpu_to_be16(x) __u_bswap16((uint16_t) (x))
118 #define be64_to_cpu(x) __u_bswap64(x)
119 #define be32_to_cpu(x) __u_bswap32(x)
120 #define be16_to_cpu(x) __u_bswap16((uint16_t) (x))
122 #define cpu_to_le64(x) (x)
123 #define cpu_to_le32(x) (x)
124 #define cpu_to_le16(x) (x)
126 #define le64_to_cpu(x) (x)
127 #define le32_to_cpu(x) (x)
128 #define le16_to_cpu(x) (x)
130 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */
132 #define cpu_to_le64(x) __u_bswap64(x)
133 #define cpu_to_le32(x) __u_bswap32(x)
134 #define cpu_to_le16(x) __u_bswap16((uint16_t) (x))
136 #define le64_to_cpu(x) __u_bswap64(x)
137 #define le32_to_cpu(x) __u_bswap32(x)
138 #define le16_to_cpu(x) __u_bswap16((uint16_t) (x))
140 #define cpu_to_be64(x) (x)
141 #define cpu_to_be32(x) (x)
142 #define cpu_to_be16(x) (x)
144 #define be64_to_cpu(x) (x)
145 #define be32_to_cpu(x) (x)
146 #define be16_to_cpu(x) (x)
151 #define __packed __attribute__((packed))