compat FEATURE compat updated
diff --git a/compat/compat.c b/compat/compat.c
index 70ebece..71e8176 100644
--- a/compat/compat.c
+++ b/compat/compat.c
@@ -3,7 +3,7 @@
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief compatibility functions
*
- * Copyright (c) 2020 CESNET, z.s.p.o.
+ * Copyright (c) 2021 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.
@@ -11,26 +11,29 @@
*
* https://opensource.org/licenses/BSD-3-Clause
*/
-#define _POSIX_C_SOURCE 1 /* fdopen, _POSIX_PATH_MAX */
+#define _POSIX_C_SOURCE 200809L /* fdopen, _POSIX_PATH_MAX, strdup */
#define _ISOC99_SOURCE /* vsnprintf */
-#define _XOPEN_SOURCE 500 /* strdup */
+
+#include "compat.h"
#include <errno.h>
+#include <inttypes.h>
#include <limits.h>
-#include <string.h>
+#include <pthread.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
+#include <sys/time.h>
+#include <time.h>
#include <unistd.h>
-#include "compat.h"
-
#ifndef HAVE_VDPRINTF
int
vdprintf(int fd, const char *format, va_list ap)
{
FILE *stream;
- int count;
+ int count = 0;
stream = fdopen(dup(fd), "a+");
if (stream) {
@@ -39,6 +42,7 @@
}
return count;
}
+
#endif
#ifndef HAVE_ASPRINTF
@@ -53,6 +57,7 @@
va_end(ap);
return ret;
}
+
#endif
#ifndef HAVE_VASPRINTF
@@ -60,36 +65,19 @@
vasprintf(char **strp, const char *fmt, va_list ap)
{
va_list ap2;
+
va_copy(ap2, ap);
int l = vsnprintf(0, 0, fmt, ap2);
+
va_end(ap2);
- if (l < 0 || !(*strp = malloc(l + 1U))) {
+ if ((l < 0) || !(*strp = malloc(l + 1U))) {
return -1;
}
return vsnprintf(*strp, l + 1U, fmt, ap);
}
-#endif
-#ifndef HAVE_STRNDUP
-char *
-strndup(const char *s, size_t n)
-{
- char *buf;
- size_t len = 0;
-
- /* strnlen */
- for (; (len < n) && (s[len] != '\0'); ++len);
-
- if (!(buf = malloc(len + 1U))) {
- return NULL;
- }
-
- memcpy(buf, s, len);
- buf[len] = '\0';
- return buf;
-}
#endif
#ifndef HAVE_GETLINE
@@ -98,7 +86,7 @@
{
static char line[256];
char *ptr;
- unsigned int len;
+ ssize_t len;
if (!lineptr || !n) {
errno = EINVAL;
@@ -132,6 +120,64 @@
strcpy(*lineptr, line);
return len;
}
+
+#endif
+
+#ifndef HAVE_STRNDUP
+char *
+strndup(const char *s, size_t n)
+{
+ char *buf;
+ size_t len = 0;
+
+ /* strnlen */
+ for ( ; (len < n) && (s[len] != '\0'); ++len) {}
+
+ if (!(buf = malloc(len + 1U))) {
+ return NULL;
+ }
+
+ memcpy(buf, s, len);
+ buf[len] = '\0';
+ return buf;
+}
+
+#endif
+
+#ifndef HAVE_STRNSTR
+char *
+strnstr(const char *s, const char *find, size_t slen)
+{
+ char c, sc;
+ size_t len;
+
+ if ((c = *find++) != '\0') {
+ len = strlen(find);
+ do {
+ do {
+ if ((slen-- < 1) || ((sc = *s++) == '\0')) {
+ return NULL;
+ }
+ } while (sc != c);
+ if (len > slen) {
+ return NULL;
+ }
+ } while (strncmp(s, find, len));
+ s--;
+ }
+ return (char *)s;
+}
+
+#endif
+
+#ifndef HAVE_STRCHRNUL
+char *
+strchrnul(const char *s, int c)
+{
+ char *p = strchr(s, c);
+ return p ? p : (char *)s + strlen(s);
+}
+
#endif
#ifndef HAVE_GET_CURRENT_DIR_NAME
@@ -150,4 +196,54 @@
return retval;
}
+
+#endif
+
+#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
+int
+pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime)
+{
+ int64_t nsec_diff;
+ int32_t diff;
+ struct timespec cur, dur;
+ int rc;
+
+ /* try to acquire the lock and, if we fail, sleep for 5ms. */
+ while ((rc = pthread_mutex_trylock(mutex)) == EBUSY) {
+ /* get real time */
+#ifdef CLOCK_REALTIME
+ clock_gettime(CLOCK_REALTIME, &cur);
+#else
+ struct timeval tv;
+
+ gettimeofday(&tv, NULL);
+ cur.tv_sec = (time_t)tv.tv_sec;
+ cur.tv_nsec = 1000L * (long)tv.tv_usec;
+#endif
+
+ /* get time diff */
+ nsec_diff = 0;
+ nsec_diff += (((int64_t)abstime->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
+ nsec_diff += ((int64_t)abstime->tv_nsec) - ((int64_t)cur.tv_nsec);
+ diff = (nsec_diff ? nsec_diff / 1000000L : 0);
+
+ if (diff < 1) {
+ /* timeout */
+ break;
+ } else if (diff < 5) {
+ /* sleep until timeout */
+ dur.tv_sec = 0;
+ dur.tv_nsec = (long)diff * 1000000;
+ } else {
+ /* sleep 5 ms */
+ dur.tv_sec = 0;
+ dur.tv_nsec = 5000000;
+ }
+
+ nanosleep(&dur, NULL);
+ }
+
+ return rc;
+}
+
#endif
diff --git a/compat/compat.h.in b/compat/compat.h.in
index 73024f4..acc5fc7 100644
--- a/compat/compat.h.in
+++ b/compat/compat.h.in
@@ -3,7 +3,7 @@
* @author Michal Vasko <mvasko@cesnet.cz>
* @brief compatibility functions header
*
- * Copyright (c) 2020 CESNET, z.s.p.o.
+ * Copyright (c) 2021 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.
@@ -15,9 +15,12 @@
#ifndef _COMPAT_H_
#define _COMPAT_H_
-#include <sys/types.h>
+#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__
@@ -29,26 +32,40 @@
#ifndef __INT64_C
# if __WORDSIZE == 64
-# define __INT64_C(c) c ## L
+# define __INT64_C(c) c ## L
# define __UINT64_C(c) c ## UL
# else
-# define __INT64_C(c) c ## LL
+# define __INT64_C(c) c ## LL
# define __UINT64_C(c) c ## ULL
# endif
#endif
+#if (@CMAKE_C_COMPILER_ID@ == GNU) || (@CMAKE_C_COMPILER_ID@ == Clang)
+# define UNUSED(x) UNUSED_ ## x __attribute__((__unused__))
+# define _PACKED __attribute__((__packed__))
+#else
+# define UNUSED(x) UNUSED_ ## x
+# define _PACKED
+#endif
+
#cmakedefine HAVE_VDPRINTF
#cmakedefine HAVE_ASPRINTF
#cmakedefine HAVE_VASPRINTF
-#cmakedefine HAVE_STRNDUP
#cmakedefine HAVE_GETLINE
+#cmakedefine HAVE_STRNDUP
+#cmakedefine HAVE_STRNSTR
+#cmakedefine HAVE_STRDUPA
+#cmakedefine HAVE_STRCHRNUL
#cmakedefine HAVE_GET_CURRENT_DIR_NAME
+#cmakedefine HAVE_PTHREAD_MUTEX_TIMEDLOCK
+#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
@@ -63,9 +80,33 @@
# define htole64(x) (x)
#endif
-#ifndef MAP_ANONYMOUS
-#define MAP_ANONYMOUS MAP_ANON
-#endif /* !MAP_ANONYMOUS */
+#cmakedefine HAVE_STDATOMIC
+
+#ifdef HAVE_STDATOMIC
+# include <stdatomic.h>
+
+# define ATOMIC_T atomic_uint_fast32_t
+# define ATOMIC_T_MAX UINT_FAST32_MAX
+
+# 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_INC_RELAXED(var) atomic_fetch_add_explicit(&(var), 1, memory_order_relaxed)
+# define ATOMIC_ADD_RELAXED(var, x) atomic_fetch_add_explicit(&(var), x, memory_order_relaxed)
+# define ATOMIC_DEC_RELAXED(var) atomic_fetch_sub_explicit(&(var), 1, memory_order_relaxed)
+# define ATOMIC_SUB_RELAXED(var, x) atomic_fetch_sub_explicit(&(var), x, memory_order_relaxed)
+#else
+# include <stdint.h>
+
+# define ATOMIC_T uint32_t
+# define ATOMIC_T_MAX UINT32_MAX
+
+# define ATOMIC_STORE_RELAXED(var, x) ((var) = (x))
+# define ATOMIC_LOAD_RELAXED(var) (var)
+# define ATOMIC_INC_RELAXED(var) __sync_fetch_and_add(&(var), 1)
+# define ATOMIC_ADD_RELAXED(var, x) __sync_fetch_and_add(&(var), x)
+# define ATOMIC_DEC_RELAXED(var) __sync_fetch_and_sub(&(var), 1)
+# define ATOMIC_SUB_RELAXED(var, x) __sync_fetch_and_sub(&(var), x)
+#endif
#ifndef HAVE_VDPRINTF
int vdprintf(int fd, const char *format, va_list ap);
@@ -79,16 +120,39 @@
int vasprintf(char **strp, const char *fmt, va_list ap);
#endif
-#ifndef HAVE_STRNDUP
-char *strndup(const char *s, size_t n);
-#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
+#ifndef HAVE_PTHREAD_MUTEX_TIMEDLOCK
+int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *abstime);
+#endif
+
#endif /* _COMPAT_H_ */