diff options
| author | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 13:04:32 +0300 |
|---|---|---|
| committer | Berke Güzel <wenekar1@gmail.com> | 2026-10-15 13:04:32 +0300 |
| commit | bdaedce69b17b654bd0060579dd9b9ff120009b5 (patch) | |
| tree | 0e9cd8597652263a96c48ba678fb0b66e1fc131b /src/compat.h | |
| parent | ba75c8b0a1bba03ad92c53375432f122ddb4cb98 (diff) | |
| parent | dcebf9c7f954a55350b68d0ead1935612c4e2e05 (diff) | |
Merge branch 'wt/rename' into api-hardening
# Conflicts:
# README.md
Diffstat (limited to 'src/compat.h')
| -rw-r--r-- | src/compat.h | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/src/compat.h b/src/compat.h index 6c3302f..0b4559e 100644 --- a/src/compat.h +++ b/src/compat.h @@ -1,10 +1,10 @@ -#ifndef TERM_COMPAT_H -#define TERM_COMPAT_H +#ifndef WSAP_VTCORE_COMPAT_H +#define WSAP_VTCORE_COMPAT_H /* * Compiler and libc differences, kept in one place. GCC and Clang use their * builtins. MSVC uses its intrinsics. Any other compiler, and any build with - * TERM_NO_COMPILER_BUILTINS, uses plain C. Plain C gives the same results, + * WSAP_VTCORE_NO_COMPILER_BUILTINS, uses plain C. Plain C gives the same results, * only slower. */ @@ -28,68 +28,68 @@ #endif #if defined(__GNUC__) || defined(__clang__) -#define TERM_COMPILER_GNU 1 +#define WSAP_VTCORE_COMPILER_GNU 1 #elif defined(_MSC_VER) -#define TERM_COMPILER_MSVC 1 +#define WSAP_VTCORE_COMPILER_MSVC 1 #include <intrin.h> #endif -#if !defined(TERM_NO_COMPILER_BUILTINS) && defined(TERM_COMPILER_GNU) -#define TERM_HAVE_BUILTINS 1 -#elif !defined(TERM_NO_COMPILER_BUILTINS) && defined(TERM_COMPILER_MSVC) -#define TERM_HAVE_MSVC_BITOPS 1 +#if !defined(WSAP_VTCORE_NO_COMPILER_BUILTINS) && defined(WSAP_VTCORE_COMPILER_GNU) +#define WSAP_VTCORE_HAVE_BUILTINS 1 +#elif !defined(WSAP_VTCORE_NO_COMPILER_BUILTINS) && defined(WSAP_VTCORE_COMPILER_MSVC) +#define WSAP_VTCORE_HAVE_MSVC_BITOPS 1 #endif -#if defined(TERM_COMPILER_GNU) -#define TERM_HOT __attribute__((hot)) -#define TERM_NOINLINE __attribute__((noinline)) -#define TERM_ALWAYS_INLINE __attribute__((always_inline)) -#define TERM_MAYBE_UNUSED __attribute__((unused)) -#elif defined(TERM_COMPILER_MSVC) -#define TERM_HOT -#define TERM_NOINLINE __declspec(noinline) -#define TERM_ALWAYS_INLINE __forceinline -#define TERM_MAYBE_UNUSED +#if defined(WSAP_VTCORE_COMPILER_GNU) +#define WSAP_VTCORE_HOT __attribute__((hot)) +#define WSAP_VTCORE_NOINLINE __attribute__((noinline)) +#define WSAP_VTCORE_ALWAYS_INLINE __attribute__((always_inline)) +#define WSAP_VTCORE_MAYBE_UNUSED __attribute__((unused)) +#elif defined(WSAP_VTCORE_COMPILER_MSVC) +#define WSAP_VTCORE_HOT +#define WSAP_VTCORE_NOINLINE __declspec(noinline) +#define WSAP_VTCORE_ALWAYS_INLINE __forceinline +#define WSAP_VTCORE_MAYBE_UNUSED #else -#define TERM_HOT -#define TERM_NOINLINE -#define TERM_ALWAYS_INLINE -#define TERM_MAYBE_UNUSED +#define WSAP_VTCORE_HOT +#define WSAP_VTCORE_NOINLINE +#define WSAP_VTCORE_ALWAYS_INLINE +#define WSAP_VTCORE_MAYBE_UNUSED #endif -#if defined(TERM_HAVE_BUILTINS) -#define TERM_EXPECT(x, v) __builtin_expect((x), (v)) +#if defined(WSAP_VTCORE_HAVE_BUILTINS) +#define WSAP_VTCORE_EXPECT(x, v) __builtin_expect((x), (v)) #else -#define TERM_EXPECT(x, v) (x) +#define WSAP_VTCORE_EXPECT(x, v) (x) #endif -/* term_ctz32(0) is 32, term_clz32(0) is 32, term_ctz64(0) is 64, and - * term_clz64(0) is 64 on every path. */ -#if defined(TERM_HAVE_BUILTINS) +/* wsap_vtcore_ctz32(0) is 32, wsap_vtcore_clz32(0) is 32, wsap_vtcore_ctz64(0) is 64, and + * wsap_vtcore_clz64(0) is 64 on every path. */ +#if defined(WSAP_VTCORE_HAVE_BUILTINS) -static inline int term_ctz32(uint32_t x) { return x ? __builtin_ctz(x) : 32; } -static inline int term_clz32(uint32_t x) { return x ? __builtin_clz(x) : 32; } -static inline int term_popcount32(uint32_t x) { return __builtin_popcount(x); } -static inline int term_ctz64(uint64_t x) { return x ? __builtin_ctzll(x) : 64; } -static inline int term_clz64(uint64_t x) { return x ? __builtin_clzll(x) : 64; } -static inline int term_popcount64(uint64_t x) { return __builtin_popcountll(x); } +static inline int wsap_vtcore_ctz32(uint32_t x) { return x ? __builtin_ctz(x) : 32; } +static inline int wsap_vtcore_clz32(uint32_t x) { return x ? __builtin_clz(x) : 32; } +static inline int wsap_vtcore_popcount32(uint32_t x) { return __builtin_popcount(x); } +static inline int wsap_vtcore_ctz64(uint64_t x) { return x ? __builtin_ctzll(x) : 64; } +static inline int wsap_vtcore_clz64(uint64_t x) { return x ? __builtin_clzll(x) : 64; } +static inline int wsap_vtcore_popcount64(uint64_t x) { return __builtin_popcountll(x); } #else -#if defined(TERM_HAVE_MSVC_BITOPS) -static inline int term_ctz32(uint32_t x) +#if defined(WSAP_VTCORE_HAVE_MSVC_BITOPS) +static inline int wsap_vtcore_ctz32(uint32_t x) { unsigned long i; return _BitScanForward(&i, x) ? (int)i : 32; } -static inline int term_clz32(uint32_t x) +static inline int wsap_vtcore_clz32(uint32_t x) { unsigned long i; return _BitScanReverse(&i, x) ? 31 - (int)i : 32; } -static inline int term_popcount32(uint32_t x) { return (int)__popcnt(x); } +static inline int wsap_vtcore_popcount32(uint32_t x) { return (int)__popcnt(x); } #else -static inline int term_ctz32(uint32_t x) +static inline int wsap_vtcore_ctz32(uint32_t x) { if (!x) return 32; @@ -99,7 +99,7 @@ static inline int term_ctz32(uint32_t x) }; return (int)de_bruijn[((uint32_t)((x & -x) * 0x077CB531U)) >> 27]; } -static inline int term_clz32(uint32_t x) +static inline int wsap_vtcore_clz32(uint32_t x) { if (!x) return 32; @@ -111,7 +111,7 @@ static inline int term_clz32(uint32_t x) if (!(x & 0x80000000u)) { n += 1; } return n; } -static inline int term_popcount32(uint32_t x) +static inline int wsap_vtcore_popcount32(uint32_t x) { x = x - ((x >> 1) & 0x55555555u); x = (x & 0x33333333u) + ((x >> 2) & 0x33333333u); @@ -120,75 +120,75 @@ static inline int term_popcount32(uint32_t x) #endif /* Without builtins, the 64-bit forms are built from the 32-bit ones. */ -static inline int term_ctz64(uint64_t x) +static inline int wsap_vtcore_ctz64(uint64_t x) { uint32_t lo = (uint32_t)x; - return lo ? term_ctz32(lo) : 32 + term_ctz32((uint32_t)(x >> 32)); + return lo ? wsap_vtcore_ctz32(lo) : 32 + wsap_vtcore_ctz32((uint32_t)(x >> 32)); } -static inline int term_clz64(uint64_t x) +static inline int wsap_vtcore_clz64(uint64_t x) { uint32_t hi = (uint32_t)(x >> 32); - return hi ? term_clz32(hi) : 32 + term_clz32((uint32_t)x); + return hi ? wsap_vtcore_clz32(hi) : 32 + wsap_vtcore_clz32((uint32_t)x); } -static inline int term_popcount64(uint64_t x) +static inline int wsap_vtcore_popcount64(uint64_t x) { - return term_popcount32((uint32_t)x) + term_popcount32((uint32_t)(x >> 32)); + return wsap_vtcore_popcount32((uint32_t)x) + wsap_vtcore_popcount32((uint32_t)(x >> 32)); } #endif /* Smallest power of two >= x, branchlessly via CLZ. Returns UINT32_MAX on overflow. */ -static inline uint32_t term_next_pow2_32(uint32_t x) +static inline uint32_t wsap_vtcore_next_pow2_32(uint32_t x) { uint32_t v = x <= 1 ? 1 : x - 1; - int lz = term_clz32(v); + int lz = wsap_vtcore_clz32(v); uint32_t res = UINT32_C(1) << ((32 - lz) & 31); return (x <= 1) ? 1 : (lz == 0 ? UINT32_MAX : res); } /* Smallest power of two >= x, branchlessly via CLZ. Returns UINT64_MAX on overflow. */ -static inline uint64_t term_next_pow2_64(uint64_t x) +static inline uint64_t wsap_vtcore_next_pow2_64(uint64_t x) { uint64_t v = x <= 1 ? 1 : x - 1; - int lz = term_clz64(v); + int lz = wsap_vtcore_clz64(v); uint64_t res = UINT64_C(1) << ((64 - lz) & 63); return (x <= 1) ? 1 : (lz == 0 ? UINT64_MAX : res); } /* Grows cap by doubling until >= need, branchlessly. * If cap >= need, returns cap. Otherwise returns next power of two >= need. */ -static inline uint32_t term_grow_cap32(uint32_t cap, uint32_t need) +static inline uint32_t wsap_vtcore_grow_cap32(uint32_t cap, uint32_t need) { - return cap >= need ? cap : term_next_pow2_32(need); + return cap >= need ? cap : wsap_vtcore_next_pow2_32(need); } -static inline uint64_t term_grow_cap64(uint64_t cap, uint64_t need) +static inline uint64_t wsap_vtcore_grow_cap64(uint64_t cap, uint64_t need) { - return cap >= need ? cap : term_next_pow2_64(need); + return cap >= need ? cap : wsap_vtcore_next_pow2_64(need); } -static inline size_t term_grow_cap(size_t cap, size_t need) +static inline size_t wsap_vtcore_grow_cap(size_t cap, size_t need) { #if SIZE_MAX > UINT32_MAX - return (size_t)term_grow_cap64(cap, need); + return (size_t)wsap_vtcore_grow_cap64(cap, need); #else - return (size_t)term_grow_cap32((uint32_t)cap, (uint32_t)need); + return (size_t)wsap_vtcore_grow_cap32((uint32_t)cap, (uint32_t)need); #endif } /* A spin-wait hint. Compilers without one get a no-op. */ -static inline void term_cpu_relax(void) +static inline void wsap_vtcore_cpu_relax(void) { -#if defined(TERM_COMPILER_GNU) -#if defined(TERM_HAVE_BUILTINS) && defined(__x86_64__) +#if defined(WSAP_VTCORE_COMPILER_GNU) +#if defined(WSAP_VTCORE_HAVE_BUILTINS) && defined(__x86_64__) __builtin_ia32_pause(); #elif defined(__aarch64__) __asm__ __volatile__("yield"); #endif -#elif defined(TERM_COMPILER_MSVC) +#elif defined(WSAP_VTCORE_COMPILER_MSVC) #if defined(_M_X64) || defined(_M_IX86) _mm_pause(); #elif defined(_M_ARM64) @@ -199,11 +199,11 @@ static inline void term_cpu_relax(void) /* The writer lock. POSIX uses a mutex with priority inheritance where the * platform has it, so a waiter lifts the thread that holds the lock. Windows - * uses an SRWLOCK. Neither nests, so term_write_begin() counts re-entry. */ + * uses an SRWLOCK. Neither nests, so wsap_vtcore_write_begin() counts re-entry. */ #if defined(_WIN32) -typedef SRWLOCK term_mutex; +typedef SRWLOCK wsap_vtcore_mutex; #else -typedef pthread_mutex_t term_mutex; +typedef pthread_mutex_t wsap_vtcore_mutex; /* Strict C11 can hide PTHREAD_PRIO_INHERIT in pthread.h, as zig's aarch64 * glibc headers do. Every POSIX system that has the option uses 1 for it. */ #if !defined(PTHREAD_PRIO_INHERIT) @@ -212,7 +212,7 @@ typedef pthread_mutex_t term_mutex; #endif /* Returns false when the lock cannot be made. */ -static inline bool term_mutex_init(term_mutex *m) +static inline bool wsap_vtcore_mutex_init(wsap_vtcore_mutex *m) { #if defined(_WIN32) InitializeSRWLock(m); @@ -231,7 +231,7 @@ static inline bool term_mutex_init(term_mutex *m) } /* A normal mutex fails only when it is broken, so the result is ignored. */ -static inline void term_mutex_lock(term_mutex *m) +static inline void wsap_vtcore_mutex_lock(wsap_vtcore_mutex *m) { #if defined(_WIN32) AcquireSRWLockExclusive(m); @@ -240,7 +240,7 @@ static inline void term_mutex_lock(term_mutex *m) #endif } -static inline void term_mutex_unlock(term_mutex *m) +static inline void wsap_vtcore_mutex_unlock(wsap_vtcore_mutex *m) { #if defined(_WIN32) ReleaseSRWLockExclusive(m); @@ -250,7 +250,7 @@ static inline void term_mutex_unlock(term_mutex *m) } /* An SRWLOCK has no destroy. */ -static inline void term_mutex_destroy(term_mutex *m) +static inline void wsap_vtcore_mutex_destroy(wsap_vtcore_mutex *m) { #if defined(_WIN32) (void)m; @@ -260,7 +260,7 @@ static inline void term_mutex_destroy(term_mutex *m) } /* strdup is POSIX, so the core copies strings itself. */ -static inline char *term_strdup(const char *s) +static inline char *wsap_vtcore_strdup(const char *s) { size_t n = strlen(s) + 1; char *copy = malloc(n); @@ -270,7 +270,7 @@ static inline char *term_strdup(const char *s) } /* ASCII-only prefix test, as strncasecmp() does in the C locale. */ -static inline bool term_has_prefix_ci(const char *s, const char *prefix) +static inline bool wsap_vtcore_has_prefix_ci(const char *s, const char *prefix) { for (; *prefix; prefix++, s++) { unsigned char a = (unsigned char)*s; @@ -285,9 +285,9 @@ static inline bool term_has_prefix_ci(const char *s, const char *prefix) /* Allocates size bytes that start on an align-byte boundary. align must be a * power of two. The raw pointer is stored just before the block, so - * term_aligned_free() can release it. C11 aligned_alloc() and POSIX + * wsap_vtcore_aligned_free() can release it. C11 aligned_alloc() and POSIX * posix_memalign() are not on every toolchain. */ -static inline void *term_aligned_alloc(size_t align, size_t size) +static inline void *wsap_vtcore_aligned_alloc(size_t align, size_t size) { if (size > SIZE_MAX - align - sizeof(void *)) return NULL; @@ -299,7 +299,7 @@ static inline void *term_aligned_alloc(size_t align, size_t size) return (void *)p; } -static inline void term_aligned_free(void *p) +static inline void wsap_vtcore_aligned_free(void *p) { if (p) free(((void **)p)[-1]); |
