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