Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @file compat.h |
| 3 | * @author Michal Vasko <mvasko@cesnet.cz> |
| 4 | * @brief compatibility functions header |
| 5 | * |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 6 | * Copyright (c) 2021 CESNET, z.s.p.o. |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +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 | |
Jan Kundrát | c998948 | 2021-12-14 18:05:26 +0100 | [diff] [blame] | 18 | #ifdef _WIN32 |
| 19 | /* headers are broken on Windows, which means that some of them simply *have* to come first */ |
| 20 | # include <winsock2.h> |
| 21 | # include <ws2tcpip.h> |
| 22 | #endif |
| 23 | |
Radek Krejci | ad97c5f | 2020-06-30 09:19:28 +0200 | [diff] [blame] | 24 | #include <limits.h> |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 25 | #include <pthread.h> |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 26 | #include <stdarg.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <sys/types.h> |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 29 | #include <time.h> |
Jan Kundrát | 099b712 | 2021-12-09 23:51:15 +0100 | [diff] [blame] | 30 | #include <unistd.h> |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 31 | |
| 32 | #ifndef __WORDSIZE |
| 33 | # if defined __x86_64__ && !defined __ILP32__ |
| 34 | # define __WORDSIZE 64 |
| 35 | # else |
| 36 | # define __WORDSIZE 32 |
| 37 | # endif |
| 38 | #endif |
| 39 | |
| 40 | #ifndef __INT64_C |
| 41 | # if __WORDSIZE == 64 |
Michal Vasko | e29489b | 2020-08-24 10:43:54 +0200 | [diff] [blame] | 42 | # define __INT64_C(c) c ## L |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 43 | # define __UINT64_C(c) c ## UL |
| 44 | # else |
Michal Vasko | e29489b | 2020-08-24 10:43:54 +0200 | [diff] [blame] | 45 | # define __INT64_C(c) c ## LL |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 46 | # define __UINT64_C(c) c ## ULL |
| 47 | # endif |
| 48 | #endif |
| 49 | |
Jan Kundrát | 654d9ff | 2021-12-09 02:48:26 +0100 | [diff] [blame] | 50 | #if defined (__GNUC__) || defined (__llvm__) |
Michal Vasko | c5a2283 | 2020-08-20 13:21:33 +0200 | [diff] [blame] | 51 | # define UNUSED(x) UNUSED_ ## x __attribute__((__unused__)) |
| 52 | # define _PACKED __attribute__((__packed__)) |
| 53 | #else |
| 54 | # define UNUSED(x) UNUSED_ ## x |
| 55 | # define _PACKED |
| 56 | #endif |
| 57 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 58 | #cmakedefine HAVE_VDPRINTF |
| 59 | #cmakedefine HAVE_ASPRINTF |
| 60 | #cmakedefine HAVE_VASPRINTF |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 61 | #cmakedefine HAVE_GETLINE |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 62 | #cmakedefine HAVE_STRNDUP |
| 63 | #cmakedefine HAVE_STRNSTR |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 64 | #cmakedefine HAVE_STRDUPA |
| 65 | #cmakedefine HAVE_STRCHRNUL |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 66 | #cmakedefine HAVE_GET_CURRENT_DIR_NAME |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 67 | #cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK |
Jan Kundrát | aac59c2 | 2021-12-09 23:25:15 +0100 | [diff] [blame] | 68 | #cmakedefine HAVE_TM_GMTOFF |
| 69 | #cmakedefine HAVE_TIME_H_TIMEZONE |
Jan Kundrát | a5b3b99 | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 70 | #cmakedefine HAVE_REALPATH |
Jan Kundrát | 447970d | 2021-12-14 16:18:16 +0100 | [diff] [blame] | 71 | #cmakedefine HAVE_LOCALTIME_R |
Jan Kundrát | 39e07cb | 2022-02-14 18:29:43 +0100 | [diff] [blame] | 72 | #cmakedefine HAVE_GMTIME_R |
Jan Kundrát | 7f8fee0 | 2021-12-10 17:37:46 +0100 | [diff] [blame] | 73 | #cmakedefine HAVE_STRPTIME |
Jan Kundrát | 111ce78 | 2021-12-12 03:12:23 +0100 | [diff] [blame] | 74 | #cmakedefine HAVE_MMAP |
Jan Kundrát | 55428a5 | 2021-12-11 19:37:33 +0100 | [diff] [blame] | 75 | #cmakedefine HAVE_DIRNAME |
Jan Kundrát | 5ea2265 | 2021-12-11 20:47:42 +0100 | [diff] [blame] | 76 | #cmakedefine HAVE_STRCASECMP |
Jan Kundrát | 06b507b | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 77 | #cmakedefine HAVE_SETENV |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 78 | |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 79 | #ifndef bswap64 |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 80 | #define bswap64(val) \ |
| 81 | ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \ |
| 82 | (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \ |
| 83 | (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \ |
| 84 | (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) ) |
Christian Hopps | 6f32621 | 2021-03-23 12:37:29 -0400 | [diff] [blame] | 85 | #endif |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 86 | |
| 87 | #undef le64toh |
| 88 | #undef htole64 |
| 89 | |
| 90 | #cmakedefine IS_BIG_ENDIAN |
| 91 | |
| 92 | #ifdef IS_BIG_ENDIAN |
| 93 | # define le64toh(x) bswap64(x) |
| 94 | # define htole64(x) bswap64(x) |
| 95 | #else |
| 96 | # define le64toh(x) (x) |
| 97 | # define htole64(x) (x) |
| 98 | #endif |
| 99 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 100 | #cmakedefine HAVE_STDATOMIC |
| 101 | |
| 102 | #ifdef HAVE_STDATOMIC |
| 103 | # include <stdatomic.h> |
| 104 | |
| 105 | # define ATOMIC_T atomic_uint_fast32_t |
| 106 | # define ATOMIC_T_MAX UINT_FAST32_MAX |
| 107 | |
| 108 | # define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed) |
| 109 | # define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed) |
| 110 | # define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed) |
| 111 | # define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed) |
| 112 | # define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed) |
| 113 | # define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed) |
| 114 | #else |
| 115 | # include <stdint.h> |
| 116 | |
| 117 | # define ATOMIC_T uint32_t |
| 118 | # define ATOMIC_T_MAX UINT32_MAX |
| 119 | |
| 120 | # define ATOMIC_STORE_RELAXED(var, x) ((var) = (x)) |
| 121 | # define ATOMIC_LOAD_RELAXED(var) (var) |
Jan Kundrát | 8e63fe6 | 2021-12-10 01:50:26 +0100 | [diff] [blame] | 122 | # ifndef _WIN32 |
| 123 | # define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1) |
| 124 | # define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x) |
| 125 | # define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1) |
| 126 | # define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x) |
| 127 | # else |
| 128 | # include <windows.h> |
| 129 | # define ATOMIC_INC_RELAXED(var) InterlockedExchangeAdd(&(var), 1) |
| 130 | # define ATOMIC_ADD_RELAXED(var, x) InterlockedExchangeAdd(&(var), x) |
| 131 | # define ATOMIC_DEC_RELAXED(var) InterlockedExchangeAdd(&(var), -1) |
| 132 | # define ATOMIC_SUB_RELAXED(var, x) InterlockedExchangeAdd(&(var), -(x)) |
| 133 | # endif |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 134 | #endif |
| 135 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 136 | #ifndef HAVE_VDPRINTF |
| 137 | int vdprintf(int fd, const char *format, va_list ap); |
| 138 | #endif |
| 139 | |
| 140 | #ifndef HAVE_ASPRINTF |
| 141 | int asprintf(char **strp, const char *fmt, ...); |
| 142 | #endif |
| 143 | |
| 144 | #ifndef HAVE_VASPRINTF |
| 145 | int vasprintf(char **strp, const char *fmt, va_list ap); |
| 146 | #endif |
| 147 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 148 | #ifndef HAVE_GETLINE |
| 149 | ssize_t getline(char **lineptr, size_t *n, FILE *stream); |
| 150 | #endif |
| 151 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 152 | #ifndef HAVE_STRNDUP |
| 153 | char *strndup(const char *s, size_t n); |
| 154 | #endif |
| 155 | |
| 156 | #ifndef HAVE_STRNSTR |
| 157 | char *strnstr(const char *s, const char *find, size_t slen); |
| 158 | #endif |
| 159 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 160 | #ifndef HAVE_STRDUPA |
| 161 | #define strdupa(s) ( \ |
| 162 | { \ |
| 163 | char *buf; \ |
| 164 | size_t len = strlen(s); \ |
| 165 | buf = alloca(len + 1); \ |
| 166 | buf[len] = '\0'; \ |
| 167 | (char *)memcpy(buf, s, len); \ |
| 168 | }) |
| 169 | #endif |
| 170 | |
| 171 | #ifndef HAVE_STRCHRNUL |
| 172 | char *strchrnul(const char *s, int c); |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 173 | #endif |
| 174 | |
| 175 | #ifndef HAVE_GET_CURRENT_DIR_NAME |
| 176 | char *get_current_dir_name(void); |
| 177 | #endif |
| 178 | |
Michal Vasko | d304a08 | 2021-05-19 16:36:10 +0200 | [diff] [blame] | 179 | #ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK |
| 180 | int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime); |
| 181 | #endif |
| 182 | |
Jan Kundrát | a5b3b99 | 2021-12-10 16:13:57 +0100 | [diff] [blame] | 183 | #ifndef HAVE_REALPATH |
| 184 | char *realpath(const char *path, char *resolved_path); |
| 185 | #endif |
| 186 | |
Jan Kundrát | 447970d | 2021-12-14 16:18:16 +0100 | [diff] [blame] | 187 | #ifndef HAVE_LOCALTIME_R |
| 188 | struct tm *localtime_r(const time_t *timep, struct tm *result); |
| 189 | #endif |
| 190 | |
Jan Kundrát | 7f8fee0 | 2021-12-10 17:37:46 +0100 | [diff] [blame] | 191 | #ifndef HAVE_STRPTIME |
| 192 | char *strptime(const char *s, const char *format, struct tm *tm); |
| 193 | #endif |
| 194 | |
Jan Kundrát | a78fb6a | 2021-12-10 17:40:27 +0100 | [diff] [blame] | 195 | #if defined (_WIN32) |
| 196 | # define strtok_r strtok_s |
| 197 | #endif |
| 198 | |
Jan Kundrát | 06b507b | 2021-12-11 23:40:26 +0100 | [diff] [blame] | 199 | #ifndef HAVE_SETENV |
| 200 | int setenv(const char *name, const char *value, int overwrite); |
| 201 | #endif |
| 202 | |
Michal Vasko | 5aa44c0 | 2020-06-29 11:47:02 +0200 | [diff] [blame] | 203 | #endif /* _COMPAT_H_ */ |