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 | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 6 | * Copyright (c) 2021 - 2023 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 | 3341d1d | 2023-03-24 11:33:09 +0100 | [diff] [blame] | 18 | #define _GNU_SOURCE /* pthread_rwlock_t */ |
| 19 | |
roman | 068eb40 | 2023-11-02 15:27:04 +0100 | [diff] [blame] | 20 | #cmakedefine HAVE_CRYPT_H |
| 21 | |
| 22 | #ifdef HAVE_CRYPT_H |
| 23 | # include <crypt.h> |
| 24 | #endif |
| 25 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 26 | #include <alloca.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 27 | #include <limits.h> |
| 28 | #include <pthread.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 29 | #include <stdarg.h> |
| 30 | #include <stdio.h> |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 31 | #include <sys/types.h> |
| 32 | #include <time.h> |
roman | 068eb40 | 2023-11-02 15:27:04 +0100 | [diff] [blame] | 33 | #include <unistd.h> |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 34 | |
| 35 | #ifndef __WORDSIZE |
| 36 | # if defined __x86_64__ && !defined __ILP32__ |
| 37 | # define __WORDSIZE 64 |
| 38 | # else |
| 39 | # define __WORDSIZE 32 |
| 40 | # endif |
| 41 | #endif |
| 42 | |
| 43 | #ifndef __INT64_C |
| 44 | # if __WORDSIZE == 64 |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 45 | # define __INT64_C(c) c ## L |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 46 | # define __UINT64_C(c) c ## UL |
| 47 | # else |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 48 | # define __INT64_C(c) c ## LL |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 49 | # define __UINT64_C(c) c ## ULL |
| 50 | # endif |
| 51 | #endif |
| 52 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 53 | #if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang) |
| 54 | # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) |
| 55 | # define _PACKED __attribute__((__packed__)) |
| 56 | #else |
| 57 | # define UNUSED(x) UNUSED_ ## x |
| 58 | # define _PACKED |
| 59 | #endif |
| 60 | |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 61 | #define COMPAT_CLOCK_ID @COMPAT_CLOCK_ID@ |
| 62 | #cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 63 | #cmakedefine HAVE_PTHREAD_MUTEX_CLOCKLOCK |
Michal Vasko | 62ba134 | 2023-10-17 08:54:26 +0200 | [diff] [blame] | 64 | #cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 65 | #cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK |
Michal Vasko | 62ba134 | 2023-10-17 08:54:26 +0200 | [diff] [blame] | 66 | #cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 67 | #cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK |
| 68 | #cmakedefine HAVE_PTHREAD_COND_CLOCKWAIT |
| 69 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 70 | #cmakedefine HAVE_VDPRINTF |
| 71 | #cmakedefine HAVE_ASPRINTF |
| 72 | #cmakedefine HAVE_VASPRINTF |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 73 | #cmakedefine HAVE_GETLINE |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 74 | #cmakedefine HAVE_STRNDUP |
| 75 | #cmakedefine HAVE_STRNSTR |
| 76 | #cmakedefine HAVE_STRDUPA |
| 77 | #cmakedefine HAVE_STRCHRNUL |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 78 | #cmakedefine HAVE_GET_CURRENT_DIR_NAME |
roman | 8b1a6c3 | 2023-10-26 13:35:22 +0200 | [diff] [blame] | 79 | #cmakedefine HAVE_CRYPT_R |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 80 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 81 | #ifndef bswap64 |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 82 | #define bswap64(val) \ |
| 83 | ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \ |
| 84 | (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \ |
| 85 | (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \ |
| 86 | (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) ) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 87 | #endif |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 88 | |
| 89 | #undef le64toh |
| 90 | #undef htole64 |
| 91 | |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 92 | #cmakedefine IS_BIG_ENDIAN |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 93 | |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 94 | #ifdef IS_BIG_ENDIAN |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 95 | # define le64toh(x) bswap64(x) |
| 96 | # define htole64(x) bswap64(x) |
Michal Vasko | 652eaa6 | 2020-04-21 14:11:21 +0200 | [diff] [blame] | 97 | #else |
| 98 | # define le64toh(x) (x) |
| 99 | # define htole64(x) (x) |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 100 | #endif |
| 101 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 102 | #cmakedefine HAVE_STDATOMIC |
| 103 | |
| 104 | #ifdef HAVE_STDATOMIC |
| 105 | # include <stdatomic.h> |
| 106 | |
| 107 | # define ATOMIC_T atomic_uint_fast32_t |
| 108 | # define ATOMIC_T_MAX UINT_FAST32_MAX |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 109 | # define ATOMIC64_T atomic_uint_fast64_t |
| 110 | # define ATOMIC64_T_MAX UINT_FAST64_MAX |
| 111 | |
| 112 | # define ATOMIC_PTR_T atomic_uintptr_t |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 113 | |
| 114 | # define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed) |
| 115 | # define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed) |
| 116 | # define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed) |
| 117 | # define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed) |
| 118 | # define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed) |
| 119 | # define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed) |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 120 | # define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \ |
| 121 | result = atomic_compare_exchange_strong_explicit(&(var), &(exp), des, memory_order_relaxed, memory_order_relaxed) |
| 122 | |
| 123 | # define ATOMIC_PTR_STORE_RELAXED(var, x) atomic_store_explicit(&(var), (uintptr_t)(x), memory_order_relaxed) |
| 124 | # define ATOMIC_PTR_LOAD_RELAXED(var) ((void *)atomic_load_explicit(&(var), memory_order_relaxed)) |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 125 | #else |
| 126 | # include <stdint.h> |
| 127 | |
| 128 | # define ATOMIC_T uint32_t |
| 129 | # define ATOMIC_T_MAX UINT32_MAX |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 130 | # define ATOMIC64_T uint64_t |
| 131 | # define ATOMIC64_T_MAX UINT64_MAX |
| 132 | |
| 133 | # define ATOMIC_PTR_T void * |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 134 | |
| 135 | # define ATOMIC_STORE_RELAXED(var, x) ((var) = (x)) |
| 136 | # define ATOMIC_LOAD_RELAXED(var) (var) |
| 137 | # define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1) |
| 138 | # define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x) |
| 139 | # define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1) |
| 140 | # define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x) |
Michal Vasko | d8a7419 | 2023-02-06 15:51:50 +0100 | [diff] [blame] | 141 | # define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \ |
| 142 | { \ |
| 143 | ATOMIC_T __old = __sync_val_compare_and_swap(&(var), exp, des); \ |
| 144 | result = ATOMIC_LOAD_RELAXED(__old) == ATOMIC_LOAD_RELAXED(exp) ? 1 : 0; \ |
| 145 | ATOMIC_STORE_RELAXED(exp, ATOMIC_LOAD_RELAXED(__old)); \ |
| 146 | } |
| 147 | |
| 148 | # define ATOMIC_PTR_STORE_RELAXED(var, x) ((var) = (x)) |
| 149 | # define ATOMIC_PTR_LOAD_RELAXED(var) (var) |
| 150 | #endif |
| 151 | |
| 152 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 153 | int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime); |
| 154 | #endif |
| 155 | |
| 156 | #ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK |
| 157 | int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime); |
| 158 | #endif |
| 159 | |
| 160 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK |
| 161 | int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime); |
| 162 | #endif |
| 163 | |
| 164 | #ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK |
| 165 | int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime); |
| 166 | #endif |
| 167 | |
| 168 | #ifndef HAVE_PTHREAD_COND_CLOCKWAIT |
| 169 | int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime); |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 170 | #endif |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 171 | |
| 172 | #ifndef HAVE_VDPRINTF |
| 173 | int vdprintf(int fd, const char *format, va_list ap); |
| 174 | #endif |
| 175 | |
| 176 | #ifndef HAVE_ASPRINTF |
| 177 | int asprintf(char **strp, const char *fmt, ...); |
| 178 | #endif |
| 179 | |
| 180 | #ifndef HAVE_VASPRINTF |
| 181 | int vasprintf(char **strp, const char *fmt, va_list ap); |
| 182 | #endif |
| 183 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 184 | #ifndef HAVE_GETLINE |
| 185 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 186 | #endif |
| 187 | |
Michal Vasko | 7a20d2e | 2021-05-19 16:40:23 +0200 | [diff] [blame] | 188 | #ifndef HAVE_STRNDUP |
| 189 | char *strndup(const char *s, size_t n); |
| 190 | #endif |
| 191 | |
| 192 | #ifndef HAVE_STRNSTR |
| 193 | char *strnstr(const char *s, const char *find, size_t slen); |
| 194 | #endif |
| 195 | |
| 196 | #ifndef HAVE_STRDUPA |
| 197 | #define strdupa(s) ( \ |
| 198 | { \ |
| 199 | char *buf; \ |
| 200 | size_t len = strlen(s); \ |
| 201 | buf = alloca(len + 1); \ |
| 202 | buf[len] = '\0'; \ |
| 203 | (char *)memcpy(buf, s, len); \ |
| 204 | }) |
| 205 | #endif |
| 206 | |
| 207 | #ifndef HAVE_STRCHRNUL |
| 208 | char *strchrnul(const char *s, int c); |
| 209 | #endif |
| 210 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 211 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 212 | char *get_current_dir_name(void); |
| 213 | #endif |
| 214 | |
roman | 8b1a6c3 | 2023-10-26 13:35:22 +0200 | [diff] [blame] | 215 | #ifndef HAVE_CRYPT_R |
| 216 | char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data); |
| 217 | #endif |
| 218 | |
Michal Vasko | 9e8ac26 | 2020-04-07 13:06:45 +0200 | [diff] [blame] | 219 | #endif /* _COMPAT_H_ */ |