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