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>
27 * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
29 * allocate a block of memory big enough to hold multiple aligned objects.
30 * the pointer to the full object (starting with the first chunk) is returned,
31 * all other pointers are stored in the locations behind extra addr arguments.
32 * the last argument needs to be a NULL pointer
35 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
37 void *__calloc_a(size_t len, ...);
40 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
43 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
46 extern int __BUILD_BUG_ON_CONDITION_FAILED;
47 #define BUILD_BUG_ON(condition) \
49 __BUILD_BUG_ON(condition); \
51 __BUILD_BUG_ON_CONDITION_FAILED = 1; \
54 #define BUILD_BUG_ON __BUILD_BUG_ON
59 #define CLOCK_REALTIME 0
60 #define CLOCK_MONOTONIC 1
62 void clock_gettime(int type, struct timespec *tv);
66 #if defined(__linux__) || defined(__CYGWIN__)
70 #elif defined(__APPLE__)
71 #include <machine/endian.h>
72 #include <machine/byte_order.h>
73 #define bswap_16(x) OSSwapInt16(x)
74 #define bswap_32(x) OSSwapInt32(x)
75 #define bswap_64(x) OSSwapInt64(x)
76 #elif defined(__FreeBSD__)
77 #include <sys/endian.h>
78 #define bswap_16(x) bswap16(x)
79 #define bswap_32(x) bswap32(x)
80 #define bswap_64(x) bswap64(x)
82 #include <machine/endian.h>
83 #define bswap_16(x) swap16(x)
84 #define bswap_32(x) swap32(x)
85 #define bswap_64(x) swap64(x)
89 #define __BYTE_ORDER BYTE_ORDER
92 #define __BIG_ENDIAN BIG_ENDIAN
94 #ifndef __LITTLE_ENDIAN
95 #define __LITTLE_ENDIAN LITTLE_ENDIAN
98 #if __BYTE_ORDER == __LITTLE_ENDIAN
100 #define cpu_to_be64(x) bswap_64(x)
101 #define cpu_to_be32(x) bswap_32(x)
102 #define cpu_to_be16(x) bswap_16(x)
104 #define be64_to_cpu(x) bswap_64(x)
105 #define be32_to_cpu(x) bswap_32(x)
106 #define be16_to_cpu(x) bswap_16(x)
110 #define cpu_to_be64(x) (x)
111 #define cpu_to_be32(x) (x)
112 #define cpu_to_be16(x) (x)
114 #define be64_to_cpu(x) (x)
115 #define be32_to_cpu(x) (x)
116 #define be16_to_cpu(x) (x)
121 #define __packed __attribute__((packed))