blob: f11615230691ebcd40eb2deac3b1d728872b8cde [file] [log] [blame]
Michal Vasko9e8ac262020-04-07 13:06:45 +02001/**
2 * @file compat.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief compatibility functions header
5 *
Michal Vaskod8a74192023-02-06 15:51:50 +01006 * Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
Michal Vasko9e8ac262020-04-07 13:06:45 +02007 *
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 Vasko3341d1d2023-03-24 11:33:09 +010018#define _GNU_SOURCE /* pthread_rwlock_t */
19
Michal Vaskod8a74192023-02-06 15:51:50 +010020#include <alloca.h>
roman8b1a6c32023-10-26 13:35:22 +020021#include <crypt.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020022#include <limits.h>
23#include <pthread.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020024#include <stdarg.h>
25#include <stdio.h>
Michal Vasko7a20d2e2021-05-19 16:40:23 +020026#include <sys/types.h>
27#include <time.h>
Michal Vasko9e8ac262020-04-07 13:06:45 +020028
29#ifndef __WORDSIZE
30# if defined __x86_64__ && !defined __ILP32__
31# define __WORDSIZE 64
32# else
33# define __WORDSIZE 32
34# endif
35#endif
36
37#ifndef __INT64_C
38# if __WORDSIZE == 64
Michal Vasko7a20d2e2021-05-19 16:40:23 +020039# define __INT64_C(c) c ## L
Michal Vasko9e8ac262020-04-07 13:06:45 +020040# define __UINT64_C(c) c ## UL
41# else
Michal Vasko7a20d2e2021-05-19 16:40:23 +020042# define __INT64_C(c) c ## LL
Michal Vasko9e8ac262020-04-07 13:06:45 +020043# define __UINT64_C(c) c ## ULL
44# endif
45#endif
46
Michal Vasko7a20d2e2021-05-19 16:40:23 +020047#if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang)
48# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
49# define _PACKED __attribute__((__packed__))
50#else
51# define UNUSED(x) UNUSED_ ## x
52# define _PACKED
53#endif
54
Michal Vaskod8a74192023-02-06 15:51:50 +010055#define COMPAT_CLOCK_ID @COMPAT_CLOCK_ID@
56#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
57#cmakedefine HAVE_PTHREAD_MUTEX_CLOCKLOCK
Michal Vasko62ba1342023-10-17 08:54:26 +020058#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDRDLOCK
Michal Vaskod8a74192023-02-06 15:51:50 +010059#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
Michal Vasko62ba1342023-10-17 08:54:26 +020060#cmakedefine HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
Michal Vaskod8a74192023-02-06 15:51:50 +010061#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
62#cmakedefine HAVE_PTHREAD_COND_CLOCKWAIT
63
Michal Vasko9e8ac262020-04-07 13:06:45 +020064#cmakedefine HAVE_VDPRINTF
65#cmakedefine HAVE_ASPRINTF
66#cmakedefine HAVE_VASPRINTF
Michal Vasko9e8ac262020-04-07 13:06:45 +020067#cmakedefine HAVE_GETLINE
Michal Vasko7a20d2e2021-05-19 16:40:23 +020068#cmakedefine HAVE_STRNDUP
69#cmakedefine HAVE_STRNSTR
70#cmakedefine HAVE_STRDUPA
71#cmakedefine HAVE_STRCHRNUL
Michal Vasko9e8ac262020-04-07 13:06:45 +020072#cmakedefine HAVE_GET_CURRENT_DIR_NAME
roman8b1a6c32023-10-26 13:35:22 +020073#cmakedefine HAVE_CRYPT_R
Michal Vasko9e8ac262020-04-07 13:06:45 +020074
Michal Vasko7a20d2e2021-05-19 16:40:23 +020075#ifndef bswap64
Michal Vasko9e8ac262020-04-07 13:06:45 +020076#define bswap64(val) \
77 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
78 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
79 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
80 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
Michal Vasko7a20d2e2021-05-19 16:40:23 +020081#endif
Michal Vasko9e8ac262020-04-07 13:06:45 +020082
83#undef le64toh
84#undef htole64
85
Michal Vasko652eaa62020-04-21 14:11:21 +020086#cmakedefine IS_BIG_ENDIAN
Michal Vasko9e8ac262020-04-07 13:06:45 +020087
Michal Vasko652eaa62020-04-21 14:11:21 +020088#ifdef IS_BIG_ENDIAN
Michal Vasko9e8ac262020-04-07 13:06:45 +020089# define le64toh(x) bswap64(x)
90# define htole64(x) bswap64(x)
Michal Vasko652eaa62020-04-21 14:11:21 +020091#else
92# define le64toh(x) (x)
93# define htole64(x) (x)
Michal Vasko9e8ac262020-04-07 13:06:45 +020094#endif
95
Michal Vasko7a20d2e2021-05-19 16:40:23 +020096#cmakedefine HAVE_STDATOMIC
97
98#ifdef HAVE_STDATOMIC
99# include <stdatomic.h>
100
101# define ATOMIC_T atomic_uint_fast32_t
102# define ATOMIC_T_MAX UINT_FAST32_MAX
Michal Vaskod8a74192023-02-06 15:51:50 +0100103# define ATOMIC64_T atomic_uint_fast64_t
104# define ATOMIC64_T_MAX UINT_FAST64_MAX
105
106# define ATOMIC_PTR_T atomic_uintptr_t
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200107
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)
Michal Vaskod8a74192023-02-06 15:51:50 +0100114# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
115 result = atomic_compare_exchange_strong_explicit(&(var), &(exp), des, memory_order_relaxed, memory_order_relaxed)
116
117# define ATOMIC_PTR_STORE_RELAXED(var, x) atomic_store_explicit(&(var), (uintptr_t)(x), memory_order_relaxed)
118# define ATOMIC_PTR_LOAD_RELAXED(var) ((void *)atomic_load_explicit(&(var), memory_order_relaxed))
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200119#else
120# include <stdint.h>
121
122# define ATOMIC_T uint32_t
123# define ATOMIC_T_MAX UINT32_MAX
Michal Vaskod8a74192023-02-06 15:51:50 +0100124# define ATOMIC64_T uint64_t
125# define ATOMIC64_T_MAX UINT64_MAX
126
127# define ATOMIC_PTR_T void *
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200128
129# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
130# define ATOMIC_LOAD_RELAXED(var) (var)
131# define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1)
132# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
133# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
134# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
Michal Vaskod8a74192023-02-06 15:51:50 +0100135# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
136 { \
137 ATOMIC_T __old = __sync_val_compare_and_swap(&(var), exp, des); \
138 result = ATOMIC_LOAD_RELAXED(__old) == ATOMIC_LOAD_RELAXED(exp) ? 1 : 0; \
139 ATOMIC_STORE_RELAXED(exp, ATOMIC_LOAD_RELAXED(__old)); \
140 }
141
142# define ATOMIC_PTR_STORE_RELAXED(var, x) ((var) = (x))
143# define ATOMIC_PTR_LOAD_RELAXED(var) (var)
144#endif
145
146#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
147int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
148#endif
149
150#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
151int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
152#endif
153
154#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
155int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
156#endif
157
158#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
159int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
160#endif
161
162#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
163int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200164#endif
Michal Vasko9e8ac262020-04-07 13:06:45 +0200165
166#ifndef HAVE_VDPRINTF
167int vdprintf(int fd, const char *format, va_list ap);
168#endif
169
170#ifndef HAVE_ASPRINTF
171int asprintf(char **strp, const char *fmt, ...);
172#endif
173
174#ifndef HAVE_VASPRINTF
175int vasprintf(char **strp, const char *fmt, va_list ap);
176#endif
177
Michal Vasko9e8ac262020-04-07 13:06:45 +0200178#ifndef HAVE_GETLINE
179ssize_t getline(char **lineptr, size_t *n, FILE *stream);
180#endif
181
Michal Vasko7a20d2e2021-05-19 16:40:23 +0200182#ifndef HAVE_STRNDUP
183char *strndup(const char *s, size_t n);
184#endif
185
186#ifndef HAVE_STRNSTR
187char *strnstr(const char *s, const char *find, size_t slen);
188#endif
189
190#ifndef HAVE_STRDUPA
191#define strdupa(s) ( \
192{ \
193 char *buf; \
194 size_t len = strlen(s); \
195 buf = alloca(len + 1); \
196 buf[len] = '\0'; \
197 (char *)memcpy(buf, s, len); \
198})
199#endif
200
201#ifndef HAVE_STRCHRNUL
202char *strchrnul(const char *s, int c);
203#endif
204
Michal Vasko9e8ac262020-04-07 13:06:45 +0200205#ifndef HAVE_GET_CURRENT_DIR_NAME
206char *get_current_dir_name(void);
207#endif
208
roman8b1a6c32023-10-26 13:35:22 +0200209#ifndef HAVE_CRYPT_R
210char *crypt_r(const char *phrase, const char *setting, struct crypt_data *data);
211#endif
212
Michal Vasko9e8ac262020-04-07 13:06:45 +0200213#endif /* _COMPAT_H_ */