blob: 7c641f71f7ac3092789b67f5b05c6d8a56302e70 [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 Vaskod304a082021-05-19 16:36:10 +02006 * Copyright (c) 2021 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
Radek Krejciad97c5f2020-06-30 09:19:28 +020018#include <limits.h>
Michal Vaskod304a082021-05-19 16:36:10 +020019#include <pthread.h>
Christian Hopps6f326212021-03-23 12:37:29 -040020#include <stdarg.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020021#include <stdio.h>
22#include <sys/types.h>
Michal Vaskod304a082021-05-19 16:36:10 +020023#include <time.h>
Jan Kundrát178ff712021-12-09 23:51:15 +010024#include <unistd.h>
Michal Vasko5aa44c02020-06-29 11:47:02 +020025
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 Vaskoe29489b2020-08-24 10:43:54 +020036# define __INT64_C(c) c ## L
Michal Vasko5aa44c02020-06-29 11:47:02 +020037# define __UINT64_C(c) c ## UL
38# else
Michal Vaskoe29489b2020-08-24 10:43:54 +020039# define __INT64_C(c) c ## LL
Michal Vasko5aa44c02020-06-29 11:47:02 +020040# define __UINT64_C(c) c ## ULL
41# endif
42#endif
43
Jan Kundrát68eeada2021-12-09 02:48:26 +010044#if defined (__GNUC__) || defined (__llvm__)
Michal Vaskoc5a22832020-08-20 13:21:33 +020045# 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 Vasko5aa44c02020-06-29 11:47:02 +020052#cmakedefine HAVE_VDPRINTF
53#cmakedefine HAVE_ASPRINTF
54#cmakedefine HAVE_VASPRINTF
Michal Vaskod304a082021-05-19 16:36:10 +020055#cmakedefine HAVE_GETLINE
Michal Vasko5aa44c02020-06-29 11:47:02 +020056#cmakedefine HAVE_STRNDUP
57#cmakedefine HAVE_STRNSTR
Michal Vaskod304a082021-05-19 16:36:10 +020058#cmakedefine HAVE_STRDUPA
59#cmakedefine HAVE_STRCHRNUL
Michal Vasko5aa44c02020-06-29 11:47:02 +020060#cmakedefine HAVE_GET_CURRENT_DIR_NAME
Michal Vaskod304a082021-05-19 16:36:10 +020061#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
Jan Kundráte182a272021-12-09 23:25:15 +010062#cmakedefine HAVE_TM_GMTOFF
63#cmakedefine HAVE_TIME_H_TIMEZONE
Jan Kundrát147a72c2021-12-10 16:13:57 +010064#cmakedefine HAVE_REALPATH
Jan Kundrát91907632021-12-14 16:18:16 +010065#cmakedefine HAVE_LOCALTIME_R
Jan Kundráta5a7e292021-12-10 17:37:46 +010066#cmakedefine HAVE_STRPTIME
Jan Kundrátf1960dc2021-12-12 03:12:23 +010067#cmakedefine HAVE_MMAP
Jan Kundrát406e96e2021-12-11 19:37:33 +010068#cmakedefine HAVE_DIRNAME
Jan Kundrát6568f342021-12-11 20:47:42 +010069#cmakedefine HAVE_STRCASECMP
Michal Vasko5aa44c02020-06-29 11:47:02 +020070
Christian Hopps6f326212021-03-23 12:37:29 -040071#ifndef bswap64
Michal Vasko5aa44c02020-06-29 11:47:02 +020072#define bswap64(val) \
73 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
74 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
75 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
76 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
Christian Hopps6f326212021-03-23 12:37:29 -040077#endif
Michal Vasko5aa44c02020-06-29 11:47:02 +020078
79#undef le64toh
80#undef htole64
81
82#cmakedefine IS_BIG_ENDIAN
83
84#ifdef IS_BIG_ENDIAN
85# define le64toh(x) bswap64(x)
86# define htole64(x) bswap64(x)
87#else
88# define le64toh(x) (x)
89# define htole64(x) (x)
90#endif
91
Michal Vaskod304a082021-05-19 16:36:10 +020092#cmakedefine HAVE_STDATOMIC
93
94#ifdef HAVE_STDATOMIC
95# include <stdatomic.h>
96
97# define ATOMIC_T atomic_uint_fast32_t
98# define ATOMIC_T_MAX UINT_FAST32_MAX
99
100# define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed)
101# define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed)
102# define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed)
103# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
104# define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed)
105# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
106#else
107# include <stdint.h>
108
109# define ATOMIC_T uint32_t
110# define ATOMIC_T_MAX UINT32_MAX
111
112# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
113# define ATOMIC_LOAD_RELAXED(var) (var)
Jan Kundrát12cd9aa2021-12-10 01:50:26 +0100114# ifndef _WIN32
115# define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1)
116# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
117# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
118# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
119# else
120# include <windows.h>
121# define ATOMIC_INC_RELAXED(var) InterlockedExchangeAdd(&(var), 1)
122# define ATOMIC_ADD_RELAXED(var, x) InterlockedExchangeAdd(&(var), x)
123# define ATOMIC_DEC_RELAXED(var) InterlockedExchangeAdd(&(var), -1)
124# define ATOMIC_SUB_RELAXED(var, x) InterlockedExchangeAdd(&(var), -(x))
125# endif
Michal Vaskod304a082021-05-19 16:36:10 +0200126#endif
127
Michal Vasko5aa44c02020-06-29 11:47:02 +0200128#ifndef HAVE_VDPRINTF
129int vdprintf(int fd, const char *format, va_list ap);
130#endif
131
132#ifndef HAVE_ASPRINTF
133int asprintf(char **strp, const char *fmt, ...);
134#endif
135
136#ifndef HAVE_VASPRINTF
137int vasprintf(char **strp, const char *fmt, va_list ap);
138#endif
139
Michal Vaskod304a082021-05-19 16:36:10 +0200140#ifndef HAVE_GETLINE
141ssize_t getline(char **lineptr, size_t *n, FILE *stream);
142#endif
143
Michal Vasko5aa44c02020-06-29 11:47:02 +0200144#ifndef HAVE_STRNDUP
145char *strndup(const char *s, size_t n);
146#endif
147
148#ifndef HAVE_STRNSTR
149char *strnstr(const char *s, const char *find, size_t slen);
150#endif
151
Michal Vaskod304a082021-05-19 16:36:10 +0200152#ifndef HAVE_STRDUPA
153#define strdupa(s) ( \
154{ \
155 char *buf; \
156 size_t len = strlen(s); \
157 buf = alloca(len + 1); \
158 buf[len] = '\0'; \
159 (char *)memcpy(buf, s, len); \
160})
161#endif
162
163#ifndef HAVE_STRCHRNUL
164char *strchrnul(const char *s, int c);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200165#endif
166
167#ifndef HAVE_GET_CURRENT_DIR_NAME
168char *get_current_dir_name(void);
169#endif
170
Michal Vaskod304a082021-05-19 16:36:10 +0200171#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
172int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
173#endif
174
Jan Kundrát147a72c2021-12-10 16:13:57 +0100175#ifndef HAVE_REALPATH
176char *realpath(const char *path, char *resolved_path);
177#endif
178
Jan Kundrát91907632021-12-14 16:18:16 +0100179#ifndef HAVE_LOCALTIME_R
180struct tm *localtime_r(const time_t *timep, struct tm *result);
181#endif
182
Jan Kundráta5a7e292021-12-10 17:37:46 +0100183#ifndef HAVE_STRPTIME
184char *strptime(const char *s, const char *format, struct tm *tm);
185#endif
186
Jan Kundrát4e830702021-12-10 17:40:27 +0100187#if defined (_WIN32)
188# define strtok_r strtok_s
189#endif
190
Michal Vasko5aa44c02020-06-29 11:47:02 +0200191#endif /* _COMPAT_H_ */