Description: <short summary of the patch>
 TODO: Put a short summary on the line above and replace this paragraph
 with a longer explanation of this change. Complete the meta-information
 with other relevant fields (see below for details). To make it easier, the
 information below has been extracted from the changelog. Adjust it or drop
 it.
 .
 vmemcache (0.8-3) unstable; urgency=medium
 .
   * Hard-code -latomic.
   * Avoid some Linuxisms.
   * Handle missing PATH_MAX.
   * Don't assume hard-coded sizes of pthread types.
Author: Adam Borowski <kilobyte@angband.pl>

---
The information above should follow the Patch Tagging Guidelines, please
checkout http://dep.debian.net/deps/dep3/ to learn about the format. Here
are templates for supplementary fields that you might want to add:

Origin: <vendor|upstream|other>, <url of original patch>
Bug: <url in upstream bugtracker>
Bug-Debian: https://bugs.debian.org/<bugnumber>
Bug-Ubuntu: https://launchpad.net/bugs/<bugnumber>
Forwarded: <no|not-needed|url proving that it has been forwarded>
Reviewed-By: <name and email of someone who approved the patch>
Last-Update: 2019-05-02

--- vmemcache-0.8.orig/benchmarks/bench_simul.c
+++ vmemcache-0.8/benchmarks/bench_simul.c
@@ -737,7 +737,11 @@ main(int argc, const char **argv)
 	}
 
 	lotta_zeroes = mmap(NULL, max_size, PROT_READ,
-		MAP_PRIVATE | MAP_ANONYMOUS | MAP_POPULATE, -1, 0);
+		MAP_PRIVATE | MAP_ANONYMOUS
+#ifdef MAP_POPULATE
+		| MAP_POPULATE
+#endif
+		, -1, 0);
 	if (!lotta_zeroes) {
 		UT_FATAL("couldn't grab a zero buffer: mmap failed: %s",
 			strerror(errno));
--- vmemcache-0.8.orig/src/file.h
+++ vmemcache-0.8/src/file.h
@@ -52,6 +52,10 @@ extern "C" {
 #define NAME_MAX _MAX_FNAME
 #endif
 
+#ifndef PATH_MAX
+#define PATH_MAX 4096
+#endif
+
 struct file_info {
 	char filename[NAME_MAX + 1];
 	int is_dir;
--- vmemcache-0.8.orig/src/os_thread.h
+++ vmemcache-0.8/src/os_thread.h
@@ -40,54 +40,25 @@
 
 #include <stdint.h>
 #include <time.h>
+#include <pthread.h>
+#include <semaphore.h>
 
 #ifdef __cplusplus
 extern "C" {
 #endif
 
-typedef union {
-	long long align;
-	char padding[44]; /* linux: 40 windows: 44 */
-} os_mutex_t;
-
-typedef union {
-	long long align;
-	char padding[56]; /* linux: 56 windows: 13 */
-} os_rwlock_t;
-
-typedef union {
-	long long align;
-	char padding[48]; /* linux: 48 windows: 12 */
-} os_cond_t;
-
-typedef union {
-	long long align;
-	char padding[32]; /* linux: 8 windows: 32 */
-} os_thread_t;
-
-typedef union {
-	long long align;  /* linux: long windows: 8 FreeBSD: 12 */
-	char padding[16]; /* 16 to be safe */
-} os_once_t;
+typedef pthread_mutex_t os_mutex_t;
+typedef pthread_rwlock_t os_rwlock_t;
+typedef pthread_cond_t os_cond_t;
+typedef pthread_t os_thread_t;
+typedef pthread_once_t os_once_t;
 
-#define OS_ONCE_INIT { .padding = {0} }
+#define OS_ONCE_INIT {0}
 
 typedef unsigned os_tls_key_t;
-
-typedef union {
-	long long align;
-	char padding[56];  /* linux: 56 windows: 8 */
-} os_semaphore_t;
-
-typedef union {
-	long long align;
-	char padding[56];  /* linux: 56 windows: 8 */
-} os_thread_attr_t;
-
-typedef union {
-	long long align;
-	char padding[512];
-} os_cpu_set_t;
+typedef sem_t os_semaphore_t;
+typedef pthread_attr_t os_thread_attr_t;
+typedef cpu_set_t os_cpu_set_t;
 
 #ifdef __FreeBSD__
 #define cpu_set_t cpuset_t
--- vmemcache-0.8.orig/src/out.c
+++ vmemcache-0.8/src/out.c
@@ -44,6 +44,7 @@
 #include <string.h>
 #include <errno.h>
 
+#include "file.h"
 #include "out.h"
 #include "os.h"
 #include "os_thread.h"
--- vmemcache-0.8.orig/src/util.h
+++ vmemcache-0.8/src/util.h
@@ -251,14 +251,14 @@ typedef enum {
 #define util_bool_compare_and_swap64 __sync_bool_compare_and_swap
 #define util_val_compare_and_swap32 __sync_val_compare_and_swap
 #define util_val_compare_and_swap64 __sync_val_compare_and_swap
-#define util_fetch_and_add32 __sync_fetch_and_add
-#define util_fetch_and_add64 __sync_fetch_and_add
-#define util_fetch_and_sub32 __sync_fetch_and_sub
-#define util_fetch_and_sub64 __sync_fetch_and_sub
-#define util_fetch_and_and32 __sync_fetch_and_and
-#define util_fetch_and_and64 __sync_fetch_and_and
-#define util_fetch_and_or32 __sync_fetch_and_or
-#define util_fetch_and_or64 __sync_fetch_and_or
+#define util_fetch_and_add32(ptr,val) __atomic_fetch_add(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_add64(ptr,val) __atomic_fetch_add(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_sub32(ptr,val) __atomic_fetch_sub(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_sub64(ptr,val) __atomic_fetch_sub(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_and32(ptr,val) __atomic_fetch_and(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_and64(ptr,val) __atomic_fetch_and(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_or32(ptr,val) __atomic_fetch_or(ptr, val, __ATOMIC_SEQ_CST)
+#define util_fetch_and_or64(ptr,val) __atomic_fetch_or(ptr, val, __ATOMIC_SEQ_CST)
 #define util_synchronize __sync_synchronize
 #define util_popcount(value) ((unsigned char)__builtin_popcount(value))
 #define util_popcount64(value) ((unsigned char)__builtin_popcountll(value))
--- vmemcache-0.8.orig/src/vmemcache.c
+++ vmemcache-0.8/src/vmemcache.c
@@ -120,7 +120,7 @@ vmemcache_set_size(VMEMcache *cache, siz
 		return -1;
 	}
 
-	if (size >= (1ULL << 56)) {
+	if (size >= 1ULL << ((sizeof(void *) > 4) ? 56 : 31)) {
 		ERR("implausible large size %zu", size);
 		errno = EINVAL;
 		return -1;
--- vmemcache-0.8.orig/src/vmemcache_heap.c
+++ vmemcache-0.8/src/vmemcache_heap.c
@@ -43,7 +43,7 @@
 #define IS_FREE 0
 
 /* flag: this extent is allocated */
-#define FLAG_ALLOCATED ((uint64_t)1 << 63)
+#define FLAG_ALLOCATED ((sizeof(void *) > 4) ? (1ULL << 63) : (1UL << 31))
 
 /* mask of all flags */
 #define MASK_FLAGS (~FLAG_ALLOCATED)
--- vmemcache-0.8.orig/tests/vmemcache_test_basic.c
+++ vmemcache-0.8/tests/vmemcache_test_basic.c
@@ -322,9 +322,12 @@ test_new_delete(const char *dir, const c
 			"vmemcache_new did not fail with a file instead of a directory");
 
 #define ERR_MSG_1 "open: Not a directory"
-	if (strcmp(vmemcache_errormsg(), ERR_MSG_1))
+#define ERR_MSG_1bsd "mkstemp: Not a directory"
+	if (strcmp(vmemcache_errormsg(), ERR_MSG_1)
+		&& strcmp(vmemcache_errormsg(), ERR_MSG_1bsd)) {
 		UT_FATAL("wrong error message: '%s' (should be '"ERR_MSG_1"')",
 			vmemcache_errormsg());
+	}
 	vmemcache_delete(cache);
 
 	/* TEST #11 - NULL directory path */
@@ -347,7 +350,7 @@ test_new_delete(const char *dir, const c
 	vmemcache_set_size(cache, VMEMCACHE_MIN_POOL);
 	vmemcache_set_extent_size(cache, VMEMCACHE_MIN_EXTENT);
 	vmemcache_set_eviction_policy(cache, repl_p);
-	char nonexistent[PATH_MAX];
+	char nonexistent[4096];
 	strcpy(nonexistent, dir);
 	strcat(nonexistent, "/nonexistent_dir");
 	if (!vmemcache_add(cache, nonexistent))
--- vmemcache-0.8.orig/tests/vmemcache_test_utilization.c
+++ vmemcache-0.8/tests/vmemcache_test_utilization.c
@@ -58,7 +58,7 @@ typedef struct {
 	size_t pool_size;
 	size_t extent_size;
 	size_t val_max;
-	char dir[PATH_MAX];
+	char dir[4096];
 	long seconds;
 } test_params;
 
--- vmemcache-0.8.orig/benchmarks/benchmark_time.c
+++ vmemcache-0.8/benchmarks/benchmark_time.c
@@ -60,8 +60,8 @@ void
 benchmark_time_diff(benchmark_time_t *d, benchmark_time_t *t1,
 		    benchmark_time_t *t2)
 {
-	long long nsecs = (t2->tv_sec - t1->tv_sec) * NSECPSEC + t2->tv_nsec -
-		t1->tv_nsec;
+	long long nsecs = ((long long)t2->tv_sec - t1->tv_sec) * NSECPSEC
+		+ t2->tv_nsec - t1->tv_nsec;
 	assert(nsecs >= 0);
 	d->tv_sec = nsecs / NSECPSEC;
 	d->tv_nsec = nsecs % NSECPSEC;
