utils: move endian swap helpers and __packed definition to utils.h
[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 <time.h>
25
26 /*
27  * calloc_a(size_t len, [void **addr, size_t len,...], NULL)
28  *
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
33  */
34
35 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL)
36
37 void *__calloc_a(size_t len, ...);
38
39 #ifndef ARRAY_SIZE
40 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
41 #endif
42
43 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
44
45 #ifdef __OPTIMIZE__
46 extern int __BUILD_BUG_ON_CONDITION_FAILED;
47 #define BUILD_BUG_ON(condition)                                 \
48         do {                                                    \
49                 __BUILD_BUG_ON(condition);                      \
50                 if (condition)                                  \
51                         __BUILD_BUG_ON_CONDITION_FAILED = 1;    \
52         } while(0)
53 #else
54 #define BUILD_BUG_ON __BUILD_BUG_ON
55 #endif
56
57 #ifdef __APPLE__
58
59 #define CLOCK_REALTIME  0
60 #define CLOCK_MONOTONIC 1
61
62 void clock_gettime(int type, struct timespec *tv);
63
64 #endif
65
66 #if defined(__linux__) || defined(__CYGWIN__)
67 #include <byteswap.h>
68 #include <endian.h>
69
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)
81 #else
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)
86 #endif
87
88 #ifndef __BYTE_ORDER
89 #define __BYTE_ORDER BYTE_ORDER
90 #endif
91 #ifndef __BIG_ENDIAN
92 #define __BIG_ENDIAN BIG_ENDIAN
93 #endif
94 #ifndef __LITTLE_ENDIAN
95 #define __LITTLE_ENDIAN LITTLE_ENDIAN
96 #endif
97
98 #if __BYTE_ORDER == __LITTLE_ENDIAN
99
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)
103
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)
107
108 #else
109
110 #define cpu_to_be64(x) (x)
111 #define cpu_to_be32(x) (x)
112 #define cpu_to_be16(x) (x)
113
114 #define be64_to_cpu(x) (x)
115 #define be32_to_cpu(x) (x)
116 #define be16_to_cpu(x) (x)
117
118 #endif
119
120 #ifndef __packed
121 #define __packed __attribute__((packed))
122 #endif
123
124 #endif