blob: 3baa4893a15d58e3ca9e0a2d661c95933a18be25 [file] [log] [blame]
Michal Vasko5aa44c02020-06-29 11:47:02 +02001/**
2 * @file compat.h
3 * @author Michal Vasko <mvasko@cesnet.cz>
4 * @brief compatibility functions header
5 *
Michal Vaskof17bb2a2023-02-10 11:34:55 +01006 * Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
Michal Vasko5aa44c02020-06-29 11:47:02 +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
Jan Kundráta3d248e2021-12-14 18:05:26 +010018#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 Krejciad97c5f2020-06-30 09:19:28 +020024#include <limits.h>
Michal Vaskod304a082021-05-19 16:36:10 +020025#include <pthread.h>
Christian Hopps6f326212021-03-23 12:37:29 -040026#include <stdarg.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020027#include <stdio.h>
28#include <sys/types.h>
Michal Vaskod304a082021-05-19 16:36:10 +020029#include <time.h>
Jan Kundrát178ff712021-12-09 23:51:15 +010030#include <unistd.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020031
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 Vaskoe29489b2020-08-24 10:43:54 +020042# define __INT64_C(c) c ## L
Michal Vasko5aa44c02020-06-29 11:47:02 +020043# define __UINT64_C(c) c ## UL
44# else
Michal Vaskoe29489b2020-08-24 10:43:54 +020045# define __INT64_C(c) c ## LL
Michal Vasko5aa44c02020-06-29 11:47:02 +020046# define __UINT64_C(c) c ## ULL
47# endif
48#endif
49
Jan Kundrát68eeada2021-12-09 02:48:26 +010050#if defined (__GNUC__) || defined (__llvm__)
Michal Vaskoc5a22832020-08-20 13:21:33 +020051# 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 Vasko5aa44c02020-06-29 11:47:02 +020058#cmakedefine HAVE_VDPRINTF
59#cmakedefine HAVE_ASPRINTF
60#cmakedefine HAVE_VASPRINTF
Michal Vaskod304a082021-05-19 16:36:10 +020061#cmakedefine HAVE_GETLINE
Michal Vasko5aa44c02020-06-29 11:47:02 +020062#cmakedefine HAVE_STRNDUP
63#cmakedefine HAVE_STRNSTR
Michal Vaskod304a082021-05-19 16:36:10 +020064#cmakedefine HAVE_STRDUPA
65#cmakedefine HAVE_STRCHRNUL
Michal Vasko5aa44c02020-06-29 11:47:02 +020066#cmakedefine HAVE_GET_CURRENT_DIR_NAME
Michal Vaskod304a082021-05-19 16:36:10 +020067#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
Jan Kundrát147a72c2021-12-10 16:13:57 +010068#cmakedefine HAVE_REALPATH
Jan Kundrát91907632021-12-14 16:18:16 +010069#cmakedefine HAVE_LOCALTIME_R
Jan Kundráta9caaf72022-02-14 18:29:43 +010070#cmakedefine HAVE_GMTIME_R
Michal Vasko762fbcf2023-02-10 11:34:06 +010071#cmakedefine HAVE_TIMEGM
Jan Kundráta5a7e292021-12-10 17:37:46 +010072#cmakedefine HAVE_STRPTIME
Jan Kundrátf1960dc2021-12-12 03:12:23 +010073#cmakedefine HAVE_MMAP
Jan Kundrát6568f342021-12-11 20:47:42 +010074#cmakedefine HAVE_STRCASECMP
Jan Kundrátc8805302021-12-11 23:40:26 +010075#cmakedefine HAVE_SETENV
Michal Vasko5aa44c02020-06-29 11:47:02 +020076
Christian Hopps6f326212021-03-23 12:37:29 -040077#ifndef bswap64
Michal Vasko5aa44c02020-06-29 11:47:02 +020078#define bswap64(val) \
79 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
80 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
81 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
82 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
Christian Hopps6f326212021-03-23 12:37:29 -040083#endif
Michal Vasko5aa44c02020-06-29 11:47:02 +020084
85#undef le64toh
86#undef htole64
87
88#cmakedefine IS_BIG_ENDIAN
89
90#ifdef IS_BIG_ENDIAN
91# define le64toh(x) bswap64(x)
92# define htole64(x) bswap64(x)
93#else
94# define le64toh(x) (x)
95# define htole64(x) (x)
96#endif
97
Michal Vaskod304a082021-05-19 16:36:10 +020098#cmakedefine HAVE_STDATOMIC
99
100#ifdef HAVE_STDATOMIC
101# include <stdatomic.h>
102
103# define ATOMIC_T atomic_uint_fast32_t
104# define ATOMIC_T_MAX UINT_FAST32_MAX
105
106# define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed)
107# define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed)
108# define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed)
109# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
110# define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed)
111# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
112#else
113# include <stdint.h>
114
115# define ATOMIC_T uint32_t
116# define ATOMIC_T_MAX UINT32_MAX
117
118# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
119# define ATOMIC_LOAD_RELAXED(var) (var)
Jan Kundrát12cd9aa2021-12-10 01:50:26 +0100120# ifndef _WIN32
121# define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1)
122# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
123# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
124# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
125# else
126# include <windows.h>
127# define ATOMIC_INC_RELAXED(var) InterlockedExchangeAdd(&(var), 1)
128# define ATOMIC_ADD_RELAXED(var, x) InterlockedExchangeAdd(&(var), x)
129# define ATOMIC_DEC_RELAXED(var) InterlockedExchangeAdd(&(var), -1)
130# define ATOMIC_SUB_RELAXED(var, x) InterlockedExchangeAdd(&(var), -(x))
131# endif
Michal Vaskod304a082021-05-19 16:36:10 +0200132#endif
133
Michal Vasko5aa44c02020-06-29 11:47:02 +0200134#ifndef HAVE_VDPRINTF
135int vdprintf(int fd, const char *format, va_list ap);
136#endif
137
138#ifndef HAVE_ASPRINTF
139int asprintf(char **strp, const char *fmt, ...);
140#endif
141
142#ifndef HAVE_VASPRINTF
143int vasprintf(char **strp, const char *fmt, va_list ap);
144#endif
145
Michal Vaskod304a082021-05-19 16:36:10 +0200146#ifndef HAVE_GETLINE
147ssize_t getline(char **lineptr, size_t *n, FILE *stream);
148#endif
149
Michal Vasko5aa44c02020-06-29 11:47:02 +0200150#ifndef HAVE_STRNDUP
151char *strndup(const char *s, size_t n);
152#endif
153
154#ifndef HAVE_STRNSTR
155char *strnstr(const char *s, const char *find, size_t slen);
156#endif
157
Michal Vaskod304a082021-05-19 16:36:10 +0200158#ifndef HAVE_STRDUPA
159#define strdupa(s) ( \
160{ \
161 char *buf; \
162 size_t len = strlen(s); \
163 buf = alloca(len + 1); \
164 buf[len] = '\0'; \
165 (char *)memcpy(buf, s, len); \
166})
167#endif
168
169#ifndef HAVE_STRCHRNUL
170char *strchrnul(const char *s, int c);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200171#endif
172
173#ifndef HAVE_GET_CURRENT_DIR_NAME
174char *get_current_dir_name(void);
175#endif
176
Michal Vaskod304a082021-05-19 16:36:10 +0200177#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
178int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
179#endif
180
Jan Kundrát147a72c2021-12-10 16:13:57 +0100181#ifndef HAVE_REALPATH
182char *realpath(const char *path, char *resolved_path);
183#endif
184
Jan Kundrát91907632021-12-14 16:18:16 +0100185#ifndef HAVE_LOCALTIME_R
186struct tm *localtime_r(const time_t *timep, struct tm *result);
187#endif
188
Michal Vaskof17bb2a2023-02-10 11:34:55 +0100189#ifndef HAVE_GMTIME_R
190struct tm *gmtime_r(const time_t *timep, struct tm *result);
191#endif
192
Michal Vasko762fbcf2023-02-10 11:34:06 +0100193#ifndef HAVE_TIMEGM
194# if defined (_WIN32)
195# define timegm _mkgmtime
196# else
197# error No timegm() implementation for this platform is available.
198# endif
199#endif
200
Jan Kundráta5a7e292021-12-10 17:37:46 +0100201#ifndef HAVE_STRPTIME
202char *strptime(const char *s, const char *format, struct tm *tm);
203#endif
204
Jan Kundrát4e830702021-12-10 17:40:27 +0100205#if defined (_WIN32)
206# define strtok_r strtok_s
207#endif
208
Jan Kundrátc8805302021-12-11 23:40:26 +0100209#ifndef HAVE_SETENV
210int setenv(const char *name, const char *value, int overwrite);
211#endif
212
Michal Vasko5aa44c02020-06-29 11:47:02 +0200213#endif /* _COMPAT_H_ */