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/stat.h> 23 #include <sys/types.h> 24 #include <sys/time.h> 25 #include <stdint.h> 26 #include <stdbool.h> 27 #include <unistd.h> 28 #include <time.h> 29 30 /* 31 * calloc_a(size_t len, [void **addr, size_t len,...], NULL) 32 * 33 * allocate a block of memory big enough to hold multiple aligned objects. 34 * the pointer to the full object (starting with the first chunk) is returned, 35 * all other pointers are stored in the locations behind extra addr arguments. 36 * the last argument needs to be a NULL pointer 37 */ 38 39 #define calloc_a(len, ...) __calloc_a(len, ##__VA_ARGS__, NULL) 40 41 void *__calloc_a(size_t len, ...); 42 43 #ifndef ARRAY_SIZE 44 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) 45 #endif 46 47 #define __BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) 48 49 #ifdef __OPTIMIZE__ 50 extern int __BUILD_BUG_ON_CONDITION_FAILED; 51 #define BUILD_BUG_ON(condition) \ 52 do { \ 53 __BUILD_BUG_ON(condition); \ 54 if (condition) \ 55 __BUILD_BUG_ON_CONDITION_FAILED = 1; \ 56 } while(0) 57 #else 58 #define BUILD_BUG_ON __BUILD_BUG_ON 59 #endif 60 61 #if defined(__APPLE__) && !defined(CLOCK_MONOTONIC) 62 #define LIBUBOX_COMPAT_CLOCK_GETTIME 63 64 #include <mach/clock_types.h> 65 #define CLOCK_REALTIME CALENDAR_CLOCK 66 #define CLOCK_MONOTONIC SYSTEM_CLOCK 67 68 int clock_gettime(int type, struct timespec *tv); 69 70 #endif 71 72 #ifdef __GNUC__ 73 #define _GNUC_MIN_VER(maj, min) (((__GNUC__ << 8) + __GNUC_MINOR__) >= (((maj) << 8) + (min))) 74 #else 75 #define _GNUC_MIN_VER(maj, min) 0 76 #endif 77 78 #if defined(__linux__) || defined(__CYGWIN__) 79 #include <byteswap.h> 80 #include <endian.h> 81 82 #elif defined(__APPLE__) 83 #include <machine/endian.h> 84 #include <machine/byte_order.h> 85 #elif defined(__FreeBSD__) 86 #include <sys/endian.h> 87 #else 88 #include <machine/endian.h> 89 #endif 90 91 #ifndef __BYTE_ORDER 92 #define __BYTE_ORDER BYTE_ORDER 93 #endif 94 #ifndef __BIG_ENDIAN 95 #define __BIG_ENDIAN BIG_ENDIAN 96 #endif 97 #ifndef __LITTLE_ENDIAN 98 #define __LITTLE_ENDIAN LITTLE_ENDIAN 99 #endif 100 101 #define __constant_swap16(x) ((uint16_t)( \ 102 (((uint16_t)(x) & (uint16_t)0x00ffU) << 8) | \ 103 (((uint16_t)(x) & (uint16_t)0xff00U) >> 8))) 104 105 #define __constant_swap32(x) ((uint32_t)( \ 106 (((uint32_t)(x) & (uint32_t)0x000000ffUL) << 24) | \ 107 (((uint32_t)(x) & (uint32_t)0x0000ff00UL) << 8) | \ 108 (((uint32_t)(x) & (uint32_t)0x00ff0000UL) >> 8) | \ 109 (((uint32_t)(x) & (uint32_t)0xff000000UL) >> 24))) 110 111 #define __constant_swap64(x) ((uint64_t)( \ 112 (((uint64_t)(x) & (uint64_t)0x00000000000000ffULL) << 56) | \ 113 (((uint64_t)(x) & (uint64_t)0x000000000000ff00ULL) << 40) | \ 114 (((uint64_t)(x) & (uint64_t)0x0000000000ff0000ULL) << 24) | \ 115 (((uint64_t)(x) & (uint64_t)0x00000000ff000000ULL) << 8) | \ 116 (((uint64_t)(x) & (uint64_t)0x000000ff00000000ULL) >> 8) | \ 117 (((uint64_t)(x) & (uint64_t)0x0000ff0000000000ULL) >> 24) | \ 118 (((uint64_t)(x) & (uint64_t)0x00ff000000000000ULL) >> 40) | \ 119 (((uint64_t)(x) & (uint64_t)0xff00000000000000ULL) >> 56))) 120 121 /* 122 * This returns a constant expression while determining if an argument is 123 * a constant expression, most importantly without evaluating the argument. 124 */ 125 #define __is_constant(x) \ 126 (sizeof(int) == sizeof(*(1 ? ((void*)((long)(x) * 0l)) : (int*)1))) 127 128 #define __eval_once(func, x) \ 129 ({ __typeof__(x) __x = x; func(__x); }) 130 131 #ifdef __cplusplus 132 /* 133 * g++ does not support __builtin_choose_expr, so always use __eval_once. 134 * Unfortunately this means that the byte order functions can't be used 135 * as a constant expression anymore 136 */ 137 #define __eval_safe(func, x) __eval_once(func, x) 138 #else 139 #define __eval_safe(func, x) \ 140 __builtin_choose_expr(__is_constant(x), \ 141 func(x), __eval_once(func, x)) 142 #endif 143 144 #if __BYTE_ORDER == __LITTLE_ENDIAN 145 146 #define const_cpu_to_be64(x) __constant_swap64(x) 147 #define const_cpu_to_be32(x) __constant_swap32(x) 148 #define const_cpu_to_be16(x) __constant_swap16(x) 149 150 #define const_be64_to_cpu(x) __constant_swap64(x) 151 #define const_be32_to_cpu(x) __constant_swap32(x) 152 #define const_be16_to_cpu(x) __constant_swap16(x) 153 154 #define const_cpu_to_le64(x) (x) 155 #define const_cpu_to_le32(x) (x) 156 #define const_cpu_to_le16(x) (x) 157 158 #define const_le64_to_cpu(x) (x) 159 #define const_le32_to_cpu(x) (x) 160 #define const_le16_to_cpu(x) (x) 161 162 #define cpu_to_be64(x) __eval_safe(__constant_swap64, x) 163 #define cpu_to_be32(x) __eval_safe(__constant_swap32, x) 164 #define cpu_to_be16(x) __eval_safe(__constant_swap16, x) 165 166 #define be64_to_cpu(x) __eval_safe(__constant_swap64, x) 167 #define be32_to_cpu(x) __eval_safe(__constant_swap32, x) 168 #define be16_to_cpu(x) __eval_safe(__constant_swap16, x) 169 170 #define cpu_to_le64(x) (x) 171 #define cpu_to_le32(x) (x) 172 #define cpu_to_le16(x) (x) 173 174 #define le64_to_cpu(x) (x) 175 #define le32_to_cpu(x) (x) 176 #define le16_to_cpu(x) (x) 177 178 #else /* __BYTE_ORDER == __LITTLE_ENDIAN */ 179 180 #define const_cpu_to_le64(x) __constant_swap64(x) 181 #define const_cpu_to_le32(x) __constant_swap32(x) 182 #define const_cpu_to_le16(x) __constant_swap16(x) 183 184 #define const_le64_to_cpu(x) __constant_swap64(x) 185 #define const_le32_to_cpu(x) __constant_swap32(x) 186 #define const_le16_to_cpu(x) __constant_swap16(x) 187 188 #define const_cpu_to_be64(x) (x) 189 #define const_cpu_to_be32(x) (x) 190 #define const_cpu_to_be16(x) (x) 191 192 #define const_be64_to_cpu(x) (x) 193 #define const_be32_to_cpu(x) (x) 194 #define const_be16_to_cpu(x) (x) 195 196 #define cpu_to_le64(x) __eval_safe(__constant_swap64, x) 197 #define cpu_to_le32(x) __eval_safe(__constant_swap32, x) 198 #define cpu_to_le16(x) __eval_safe(__constant_swap16, x) 199 200 #define le64_to_cpu(x) __eval_safe(__constant_swap64, x) 201 #define le32_to_cpu(x) __eval_safe(__constant_swap32, x) 202 #define le16_to_cpu(x) __eval_safe(__constant_swap16, x) 203 204 #define cpu_to_be64(x) (x) 205 #define cpu_to_be32(x) (x) 206 #define cpu_to_be16(x) (x) 207 208 #define be64_to_cpu(x) (x) 209 #define be32_to_cpu(x) (x) 210 #define be16_to_cpu(x) (x) 211 212 #endif 213 214 #ifndef __packed 215 #define __packed __attribute__((packed)) 216 #endif 217 218 #ifndef __constructor 219 #define __constructor __attribute__((constructor)) 220 #endif 221 222 #ifndef __destructor 223 #define __destructor __attribute__((destructor)) 224 #endif 225 226 #ifndef __hidden 227 #define __hidden __attribute__((visibility("hidden"))) 228 #endif 229 230 #ifndef __has_attribute 231 # define __has_attribute(x) 0 232 #endif 233 234 #ifndef fallthrough 235 # if __has_attribute(__fallthrough__) 236 # define fallthrough __attribute__((__fallthrough__)) 237 # else 238 # define fallthrough do {} while (0) /* fallthrough */ 239 # endif 240 #endif 241 242 int b64_encode(const void *src, size_t src_len, 243 void *dest, size_t dest_len); 244 245 int b64_decode(const void *src, void *dest, size_t dest_len); 246 247 #define B64_ENCODE_LEN(_len) ((((_len) + 2) / 3) * 4 + 1) 248 #define B64_DECODE_LEN(_len) (((_len) / 4) * 3 + 1) 249 250 static inline unsigned int cbuf_order(unsigned int x) 251 { 252 return 32 - __builtin_clz(x - 1); 253 } 254 255 static inline unsigned long cbuf_size(int order) 256 { 257 unsigned long page_size = sysconf(_SC_PAGESIZE); 258 unsigned long ret = 1ULL << order; 259 260 if (ret < page_size) 261 ret = page_size; 262 263 return ret; 264 } 265 266 void *cbuf_alloc(unsigned int order); 267 void cbuf_free(void *ptr, unsigned int order); 268 int mkdir_p(char *dir, mode_t mask); 269 270 #endif 271
This page was automatically generated by LXR 0.3.1. • OpenWrt