blob: 8ff7c89a1cdc91238261aad6bbd7aa575deace73 [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>
Michal Vasko5aa44c02020-06-29 11:47:02 +020024
25#ifndef __WORDSIZE
26# if defined __x86_64__ && !defined __ILP32__
27# define __WORDSIZE 64
28# else
29# define __WORDSIZE 32
30# endif
31#endif
32
33#ifndef __INT64_C
34# if __WORDSIZE == 64
Michal Vaskoe29489b2020-08-24 10:43:54 +020035# define __INT64_C(c) c ## L
Michal Vasko5aa44c02020-06-29 11:47:02 +020036# define __UINT64_C(c) c ## UL
37# else
Michal Vaskoe29489b2020-08-24 10:43:54 +020038# define __INT64_C(c) c ## LL
Michal Vasko5aa44c02020-06-29 11:47:02 +020039# define __UINT64_C(c) c ## ULL
40# endif
41#endif
42
Jan Kundrát654d9ff2021-12-09 02:48:26 +010043#if defined (__GNUC__) || defined (__llvm__)
Michal Vaskoc5a22832020-08-20 13:21:33 +020044# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
45# define _PACKED __attribute__((__packed__))
46#else
47# define UNUSED(x) UNUSED_ ## x
48# define _PACKED
49#endif
50
Michal Vasko5aa44c02020-06-29 11:47:02 +020051#cmakedefine HAVE_VDPRINTF
52#cmakedefine HAVE_ASPRINTF
53#cmakedefine HAVE_VASPRINTF
Michal Vaskod304a082021-05-19 16:36:10 +020054#cmakedefine HAVE_GETLINE
Michal Vasko5aa44c02020-06-29 11:47:02 +020055#cmakedefine HAVE_STRNDUP
56#cmakedefine HAVE_STRNSTR
Michal Vaskod304a082021-05-19 16:36:10 +020057#cmakedefine HAVE_STRDUPA
58#cmakedefine HAVE_STRCHRNUL
Michal Vasko5aa44c02020-06-29 11:47:02 +020059#cmakedefine HAVE_GET_CURRENT_DIR_NAME
Michal Vaskod304a082021-05-19 16:36:10 +020060#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
Jan Kundrátaac59c22021-12-09 23:25:15 +010061#cmakedefine HAVE_TM_GMTOFF
62#cmakedefine HAVE_TIME_H_TIMEZONE
Michal Vasko5aa44c02020-06-29 11:47:02 +020063
Christian Hopps6f326212021-03-23 12:37:29 -040064#ifndef bswap64
Michal Vasko5aa44c02020-06-29 11:47:02 +020065#define bswap64(val) \
66 ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
67 (((val) >> 24) & 0x0000000000FF0000) | (((val) >> 8) & 0x00000000FF000000) | \
68 (((val) << 8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
69 (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
Christian Hopps6f326212021-03-23 12:37:29 -040070#endif
Michal Vasko5aa44c02020-06-29 11:47:02 +020071
72#undef le64toh
73#undef htole64
74
75#cmakedefine IS_BIG_ENDIAN
76
77#ifdef IS_BIG_ENDIAN
78# define le64toh(x) bswap64(x)
79# define htole64(x) bswap64(x)
80#else
81# define le64toh(x) (x)
82# define htole64(x) (x)
83#endif
84
Michal Vaskod304a082021-05-19 16:36:10 +020085#cmakedefine HAVE_STDATOMIC
86
87#ifdef HAVE_STDATOMIC
88# include <stdatomic.h>
89
90# define ATOMIC_T atomic_uint_fast32_t
91# define ATOMIC_T_MAX UINT_FAST32_MAX
92
93# define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed)
94# define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed)
95# define ATOMIC_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed)
96# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
97# define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed)
98# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
99#else
100# include <stdint.h>
101
102# define ATOMIC_T uint32_t
103# define ATOMIC_T_MAX UINT32_MAX
104
105# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
106# define ATOMIC_LOAD_RELAXED(var) (var)
107# define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1)
108# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
109# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
110# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
111#endif
112
Michal Vasko5aa44c02020-06-29 11:47:02 +0200113#ifndef HAVE_VDPRINTF
114int vdprintf(int fd, const char *format, va_list ap);
115#endif
116
117#ifndef HAVE_ASPRINTF
118int asprintf(char **strp, const char *fmt, ...);
119#endif
120
121#ifndef HAVE_VASPRINTF
122int vasprintf(char **strp, const char *fmt, va_list ap);
123#endif
124
Michal Vaskod304a082021-05-19 16:36:10 +0200125#ifndef HAVE_GETLINE
126ssize_t getline(char **lineptr, size_t *n, FILE *stream);
127#endif
128
Michal Vasko5aa44c02020-06-29 11:47:02 +0200129#ifndef HAVE_STRNDUP
130char *strndup(const char *s, size_t n);
131#endif
132
133#ifndef HAVE_STRNSTR
134char *strnstr(const char *s, const char *find, size_t slen);
135#endif
136
Michal Vaskod304a082021-05-19 16:36:10 +0200137#ifndef HAVE_STRDUPA
138#define strdupa(s) ( \
139{ \
140 char *buf; \
141 size_t len = strlen(s); \
142 buf = alloca(len + 1); \
143 buf[len] = '\0'; \
144 (char *)memcpy(buf, s, len); \
145})
146#endif
147
148#ifndef HAVE_STRCHRNUL
149char *strchrnul(const char *s, int c);
Michal Vasko5aa44c02020-06-29 11:47:02 +0200150#endif
151
152#ifndef HAVE_GET_CURRENT_DIR_NAME
153char *get_current_dir_name(void);
154#endif
155
Michal Vaskod304a082021-05-19 16:36:10 +0200156#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
157int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
158#endif
159
Michal Vasko5aa44c02020-06-29 11:47:02 +0200160#endif /* _COMPAT_H_ */