util: SWAP macro implementation for older MSVC versions
Some checks are pending
macOS-CI / macOS-CI (dri) (push) Waiting to run
macOS-CI / macOS-CI (xlib) (push) Waiting to run

Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Acked-by: Alyssa Rosenzweig <alyssa@rosenzweig.io>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/36682>
This commit is contained in:
Aleksi Sapon 2025-08-08 12:12:33 -04:00 committed by Marge Bot
parent fe42a3d0eb
commit 3bd0badd3a

View file

@ -31,6 +31,7 @@
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#ifdef _GAMING_XBOX
#define strdup _strdup
@ -528,11 +529,23 @@ typedef int lock_cap_t;
/*
* SWAP - swap value of @a and @b
*/
#if !defined(_MSC_VER) || _MSC_VER >= 1939 /* MSVC 17.9 or later for __typeof__ */
#define SWAP(a, b) \
do { \
__typeof__(a) __tmp = (a); \
(a) = (b); \
(b) = __tmp; \
} while (0)
#else
#define SWAP(a, b) \
do { \
/* NOLINTBEGIN(bugprone-sizeof-expression) */ \
char __tmp[sizeof(a) == sizeof(b) ? (ptrdiff_t)sizeof(a) : -1]; \
memcpy(__tmp, &(b), sizeof(a)); \
memcpy(&(b), &(a), sizeof(a)); \
memcpy(&(a), __tmp, sizeof(a)); \
/* NOLINTEND(bugprone-sizeof-expression) */ \
} while (0)
#endif
#endif /* UTIL_MACROS_H */