Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file compat.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief compatibility functions header |
| 5 | * |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 7 | * |
| 8 | * This source code is licensed under BSD 3-Clause License (the "License"). |
| 9 | * You may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * https://opensource.org/licenses/BSD-3-Clause |
| 13 | */ |
| 14 | |
| 15 | #ifndef _COMPAT_H_ |
| 16 | #define _COMPAT_H_ |
| 17 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 18 | #include <limits.h> |
| 19 | #include <pthread.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 20 | #include <stdarg.h> |
| 21 | #include <stdio.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 22 | #include <sys/types.h> |
| 23 | #include <time.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 24 | |
| 25 | #ifndef __WORDSIZE |
| 26 | # if defined __x86_64__ && !defined __ILP32__ |
| 27 | # define __WORDSIZE 64 |
| 28 | # else |
| 29 | # define __WORDSIZE 32 |
| 30 | # endif |
| 31 | #endif |
| 32 | |
| 33 | #ifndef __INT64_C |
| 34 | # if __WORDSIZE == 64 |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 35 | # define __INT64_C(c) c ## L |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 36 | # define __UINT64_C(c) c ## UL |
| 37 | # else |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 38 | # define __INT64_C(c) c ## LL |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 39 | # define __UINT64_C(c) c ## ULL |
| 40 | # endif |
| 41 | #endif |
| 42 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 43 | #if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang) |
| 44 | # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) |
| 45 | # define _PACKED __attribute__((__packed__)) |
| 46 | #else |
| 47 | # define UNUSED(x) UNUSED_ ## x |
| 48 | # define _PACKED |
| 49 | #endif |
| 50 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 51 | #cmakedefine HAVE_VDPRINTF |
| 52 | #cmakedefine HAVE_ASPRINTF |
| 53 | #cmakedefine HAVE_VASPRINTF |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 54 | #cmakedefine HAVE_GETLINE |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 55 | #cmakedefine HAVE_STRNDUP |
| 56 | #cmakedefine HAVE_STRNSTR |
| 57 | #cmakedefine HAVE_STRDUPA |
| 58 | #cmakedefine HAVE_STRCHRNUL |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 59 | #cmakedefine HAVE_GET_CURRENT_DIR_NAME |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 60 | #cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 61 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 62 | #ifndef bswap64 |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 63 | #define bswap64(val) \ |
| 64 | ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \ |
| 65 | (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \ |
| 66 | (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \ |
| 67 | (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) ) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 68 | #endif |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 69 | |
| 70 | #undef le64toh |
| 71 | #undef htole64 |
| 72 | |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 73 | #cmakedefine IS_BIG_ENDIAN |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 74 | |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 75 | #ifdef IS_BIG_ENDIAN |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 76 | # define le64toh(x) bswap64(x) |
| 77 | # define htole64(x) bswap64(x) |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 78 | #else |
| 79 | # define le64toh(x) (x) |
| 80 | # define htole64(x) (x) |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 81 | #endif |
| 82 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 83 | #cmakedefine HAVE_STDATOMIC |
| 84 | |
| 85 | #ifdef HAVE_STDATOMIC |
| 86 | # include <stdatomic.h> |
| 87 | |
| 88 | # define ATOMIC_T atomic_uint_fast32_t |
| 89 | # define ATOMIC_T_MAX UINT_FAST32_MAX |
| 90 | |
| 91 | # define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed) |
| 92 | # define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed) |
| 93 | # define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed) |
| 94 | # define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed) |
| 95 | # define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed) |
| 96 | # define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed) |
| 97 | #else |
| 98 | # include <stdint.h> |
| 99 | |
| 100 | # define ATOMIC_T uint32_t |
| 101 | # define ATOMIC_T_MAX UINT32_MAX |
| 102 | |
| 103 | # define ATOMIC_STORE_RELAXED(var, x) ((var) = (x)) |
| 104 | # define ATOMIC_LOAD_RELAXED(var) (var) |
| 105 | # define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1) |
| 106 | # define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x) |
| 107 | # define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1) |
| 108 | # define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x) |
| 109 | #endif |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 110 | |
| 111 | #ifndef HAVE_VDPRINTF |
| 112 | int vdprintf(int fd, const char *format, va_list ap); |
| 113 | #endif |
| 114 | |
| 115 | #ifndef HAVE_ASPRINTF |
| 116 | int asprintf(char **strp, const char *fmt, ...); |
| 117 | #endif |
| 118 | |
| 119 | #ifndef HAVE_VASPRINTF |
| 120 | int vasprintf(char **strp, const char *fmt, va_list ap); |
| 121 | #endif |
| 122 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 123 | #ifndef HAVE_GETLINE |
| 124 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 125 | #endif |
| 126 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 127 | #ifndef HAVE_STRNDUP |
| 128 | char *strndup(const char *s, size_t n); |
| 129 | #endif |
| 130 | |
| 131 | #ifndef HAVE_STRNSTR |
| 132 | char *strnstr(const char *s, const char *find, size_t slen); |
| 133 | #endif |
| 134 | |
| 135 | #ifndef HAVE_STRDUPA |
| 136 | #define strdupa(s) ( \ |
| 137 | { \ |
| 138 | char *buf; \ |
| 139 | size_t len = strlen(s); \ |
| 140 | buf = alloca(len + 1); \ |
| 141 | buf[len] = '\0'; \ |
| 142 | (char *)memcpy(buf, s, len); \ |
| 143 | }) |
| 144 | #endif |
| 145 | |
| 146 | #ifndef HAVE_STRCHRNUL |
| 147 | char *strchrnul(const char *s, int c); |
| 148 | #endif |
| 149 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 150 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 151 | char *get_current_dir_name(void); |
| 152 | #endif |
| 153 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 154 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 155 | int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime); |
| 156 | #endif |
| 157 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 158 | #endif /* _COMPAT_H_ */ |