Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Kitware
GitHub Repository: Kitware/CMake
Path: blob/master/Utilities/cmlibrhash/librhash/util.h
3150 views
1
/* util.h */
2
#ifndef UTIL_H
3
#define UTIL_H
4
5
#include <stdlib.h> /* for aligned_alloc and __GLIBC__ version macros */
6
7
#ifdef __cplusplus
8
extern "C" {
9
#endif
10
11
/* compile-time assert */
12
#define RHASH_ASSERT(cond) (void)sizeof(char[1 - 2 * !(cond)])
13
14
#if (defined(__GNUC__) && __GNUC__ >= 4 && (__GNUC__ > 4 || __GNUC_MINOR__ >= 1) \
15
&& defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)) \
16
|| (defined(__INTEL_COMPILER) && !defined(_WIN32))
17
/* atomic operations are defined by ICC and GCC >= 4.1, but by the later one supposedly not for ARM */
18
/* note: ICC on ia64 platform possibly require ia64intrin.h, need testing */
19
# define atomic_compare_and_swap(ptr, oldval, newval) __sync_val_compare_and_swap(ptr, oldval, newval)
20
#elif defined(_MSC_VER)
21
# include <windows.h>
22
# define atomic_compare_and_swap(ptr, oldval, newval) InterlockedCompareExchange(ptr, newval, oldval)
23
#elif defined(__sun)
24
# include <atomic.h>
25
# define atomic_compare_and_swap(ptr, oldval, newval) atomic_cas_32(ptr, oldval, newval)
26
#else
27
/* pray that it will work */
28
# define atomic_compare_and_swap(ptr, oldval, newval) { if (*(ptr) == (oldval)) *(ptr) = (newval); }
29
# define NO_ATOMIC_BUILTINS
30
#endif
31
32
/* alignment macros */
33
#define DEFAULT_ALIGNMENT 64
34
#define ALIGN_SIZE_BY(size, align) (((size) + ((align) - 1)) & ~((align) - 1))
35
#define IS_SIZE_ALIGNED_BY(size, align) (((size) & ((align) - 1)) == 0)
36
#define IS_PTR_ALIGNED_BY(ptr, align) IS_SIZE_ALIGNED_BY((uintptr_t)(ptr), (align))
37
38
/* define rhash_aligned_alloc() and rhash_aligned_free() */
39
#if !defined(NO_WIN32_ALIGNED_ALLOC) && defined(_WIN32)
40
41
# define HAS_WIN32_ALIGNED_ALLOC
42
# include <malloc.h>
43
# define rhash_aligned_alloc(alignment, size) _aligned_malloc((size), (alignment))
44
# define rhash_aligned_free(ptr) _aligned_free(ptr)
45
46
#elif !defined(NO_STDC_ALIGNED_ALLOC) && (__STDC_VERSION__ >= 201112L || defined(_ISOC11_SOURCE)) \
47
&& !(defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ < 16))) \
48
&& !(defined(__ibmxl__) && defined(__clang__) && defined(__linux__)) \
49
&& !defined(__APPLE__) && !defined(__HAIKU__) && !defined(__sun) \
50
&& (!defined(__ANDROID_API__) || __ANDROID_API__ >= 28)
51
52
# define HAS_STDC_ALIGNED_ALLOC
53
# define rhash_aligned_alloc(alignment, size) aligned_alloc((alignment), ALIGN_SIZE_BY(size, alignment))
54
# define rhash_aligned_free(ptr) free(ptr)
55
56
#else /* defined(_WIN32) ... */
57
58
# include "ustd.h" /* for _POSIX_VERSION macro */
59
60
# if !defined(NO_POSIX_ALIGNED_ALLOC) && (_POSIX_VERSION >= 200112L || _XOPEN_SOURCE >= 600)
61
62
# define HAS_POSIX_ALIGNED_ALLOC
63
# define rhash_aligned_alloc(alignment, size) rhash_px_aalloc((alignment), ALIGN_SIZE_BY(size, sizeof(void*)))
64
# define rhash_aligned_free(ptr) free(ptr)
65
void* rhash_px_aalloc(size_t size, size_t alignment);
66
67
# else
68
69
# define HAS_GENERIC_ALIGNED_ALLOC
70
void* rhash_aligned_alloc(size_t alignment, size_t size);
71
void rhash_aligned_free(void* ptr);
72
73
# endif /* !defined(NO_POSIX_ALIGNED_ALLOC) ... */
74
#endif /* defined(_WIN32) ... */
75
76
#ifdef __cplusplus
77
} /* extern "C" */
78
#endif /* __cplusplus */
79
80
#endif /* UTIL_H */
81
82