/**
 * @file compat.h
 * @author Michal Vasko <mvasko@cesnet.cz>
 * @brief compatibility functions header
 *
 * Copyright (c) 2021 - 2023 CESNET, z.s.p.o.
 *
 * This source code is licensed under BSD 3-Clause License (the "License").
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/BSD-3-Clause
 */

#ifndef _COMPAT_H_
#define _COMPAT_H_

#define _GNU_SOURCE /* pthread_rwlock_t */
#if !defined (_POSIX_C_SOURCE) && !defined (__FreeBSD__) /* hides functions */
# define _POSIX_C_SOURCE 199309L /* CLOCK_* */
#endif

#cmakedefine HAVE_ALLOCA_H
#ifdef HAVE_ALLOCA_H
# include <alloca.h>
#endif

#include <limits.h>
#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/types.h>
#include <time.h>

#ifndef __WORDSIZE
#  if defined __x86_64__ && !defined __ILP32__
#   define __WORDSIZE 64
#  else
#   define __WORDSIZE 32
#  endif
#endif

#ifndef __INT64_C
#  if __WORDSIZE == 64
#    define __INT64_C(c) c ## L
#    define __UINT64_C(c) c ## UL
#  else
#    define __INT64_C(c) c ## LL
#    define __UINT64_C(c) c ## ULL
#  endif
#endif

#if defined (__GNUC__) || defined (__llvm__)
# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
# define _PACKED __attribute__((__packed__))
#else
# define UNUSED(x) UNUSED_ ## x
# define _PACKED
#endif

#define COMPAT_CLOCK_ID @COMPAT_CLOCK_ID@
#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
#cmakedefine HAVE_PTHREAD_MUTEX_CLOCKLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
#cmakedefine HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
#cmakedefine HAVE_PTHREAD_COND_CLOCKWAIT

#cmakedefine HAVE_VDPRINTF
#cmakedefine HAVE_ASPRINTF
#cmakedefine HAVE_VASPRINTF
#cmakedefine HAVE_GETLINE
#cmakedefine HAVE_STRNDUP
#cmakedefine HAVE_STRNSTR
#cmakedefine HAVE_STRDUPA
#cmakedefine HAVE_STRCHRNUL
#cmakedefine HAVE_GET_CURRENT_DIR_NAME

#ifndef bswap64
#define bswap64(val) \
    ( (((val) >> 56) & 0x00000000000000FF) | (((val) >> 40) & 0x000000000000FF00) | \
    (((val) >> 24) & 0x0000000000FF0000) | (((val) >>  8) & 0x00000000FF000000) | \
    (((val) <<  8) & 0x000000FF00000000) | (((val) << 24) & 0x0000FF0000000000) | \
    (((val) << 40) & 0x00FF000000000000) | (((val) << 56) & 0xFF00000000000000) )
#endif

#undef le64toh
#undef htole64

#cmakedefine IS_BIG_ENDIAN

#ifdef IS_BIG_ENDIAN
# define le64toh(x) bswap64(x)
# define htole64(x) bswap64(x)
#else
# define le64toh(x) (x)
# define htole64(x) (x)
#endif

#if __STDC_VERSION__ >= 201112L
# define THREAD _Thread_local
#elif defined (__GNUC__)
# define THREAD __thread
#else
# error Cannot define thread_local
#endif

#cmakedefine HAVE_STDATOMIC

#ifdef HAVE_STDATOMIC
# include <stdatomic.h>

# define ATOMIC_T atomic_uint_fast32_t
# define ATOMIC_T_MAX UINT_FAST32_MAX
# define ATOMIC64_T atomic_uint_fast64_t
# define ATOMIC64_T_MAX UINT_FAST64_MAX

# define ATOMIC_PTR_T atomic_uintptr_t

# define ATOMIC_STORE_RELAXED(var, x) atomic_store_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_LOAD_RELAXED(var) atomic_load_explicit(&(var), memory_order_relaxed)
# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
# define ATOMIC_INC_RELAXED(var) ATOMIC_ADD_RELAXED(var, 1)
# define ATOMIC_DEC_RELAXED(var) ATOMIC_SUB_RELAXED(var, 1)
# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
        result = atomic_compare_exchange_strong_explicit(&(var), &(exp), des, memory_order_relaxed, memory_order_relaxed)

# define ATOMIC_PTR_STORE_RELAXED(var, x) atomic_store_explicit(&(var), (uintptr_t)(x), memory_order_relaxed)
# define ATOMIC_PTR_LOAD_RELAXED(var) ((void *)atomic_load_explicit(&(var), memory_order_relaxed))
#else
# include <stdint.h>

# define ATOMIC_T uint32_t
# define ATOMIC_T_MAX UINT32_MAX
# define ATOMIC64_T uint64_t
# define ATOMIC64_T_MAX UINT64_MAX

# define ATOMIC_PTR_T void *

# define ATOMIC_STORE_RELAXED(var, x) __atomic_store_n(&(var), x, __ATOMIC_RELAXED)
# define ATOMIC_LOAD_RELAXED(var) __atomic_load_n(&(var), __ATOMIC_RELAXED)
# define ATOMIC_ADD_RELAXED(var, x) __atomic_fetch_add(&(var), x, __ATOMIC_RELAXED)
# define ATOMIC_SUB_RELAXED(var, x) __atomic_fetch_sub(&(var), x, __ATOMIC_RELAXED)
# define ATOMIC_INC_RELAXED(var) ATOMIC_ADD_RELAXED(var, 1)
# define ATOMIC_DEC_RELAXED(var) ATOMIC_SUB_RELAXED(var, 1)
# define ATOMIC_COMPARE_EXCHANGE_RELAXED(var, exp, des, result) \
            result = __atomic_compare_exchange_n(&(var), &(exp), des, 0, __ATOMIC_RELAXED, __ATOMIC_RELAXED)

# define ATOMIC_PTR_STORE_RELAXED(var, x) ATOMIC_STORE_RELAXED(var, x)
# define ATOMIC_PTR_LOAD_RELAXED(var) ATOMIC_LOAD_RELAXED(var)
#endif

#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
#endif

#ifndef HAVE_PTHREAD_MUTEX_CLOCKLOCK
int pthread_mutex_clocklock(pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
#endif

#ifndef HAVE_PTHREAD_RWLOCK_CLOCKRDLOCK
int pthread_rwlock_clockrdlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
#endif

#ifndef HAVE_PTHREAD_RWLOCK_CLOCKWRLOCK
int pthread_rwlock_clockwrlock(pthread_rwlock_t *rwlock, clockid_t clockid, const struct timespec *abstime);
#endif

#ifndef HAVE_PTHREAD_COND_CLOCKWAIT
int pthread_cond_clockwait(pthread_cond_t *cond, pthread_mutex_t *mutex, clockid_t clockid, const struct timespec *abstime);
#endif

#ifndef HAVE_VDPRINTF
int vdprintf(int fd, const char *format, va_list ap);
#endif

#ifndef HAVE_ASPRINTF
int asprintf(char **strp, const char *fmt, ...);
#endif

#ifndef HAVE_VASPRINTF
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif

#ifndef HAVE_GETLINE
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
#endif

#ifndef HAVE_STRNDUP
char *strndup(const char *s, size_t n);
#endif

#ifndef HAVE_STRNSTR
char *strnstr(const char *s, const char *find, size_t slen);
#endif

#ifndef HAVE_STRDUPA
#define strdupa(s) (             \
{                                \
    char *buf;                   \
    size_t len = strlen(s);      \
    buf = alloca(len + 1);       \
    buf[len] = '\0';             \
    (char *)memcpy(buf, s, len); \
})
#endif

#ifndef HAVE_STRCHRNUL
char *strchrnul(const char *s, int c);
#endif

#ifndef HAVE_GET_CURRENT_DIR_NAME
char *get_current_dir_name(void);
#endif

#endif /* _COMPAT_H_ */
