From 02fcf3974ef7c37b6af57683d9c884d996495b68 Mon Sep 17 00:00:00 2001 From: Andrew Kaster Date: Sun, 27 Dec 2020 16:51:03 -0700 Subject: [PATCH] AK/Userland: Use AK/Endian.h for portable byte swapping in ntpquery Create macros for the byte swap operations one would expect to be in endian.h or byteswap.h in AK/Endian.h. It's likely a similar/different change will be needed for BSDs, but there's no github action for those added to the project yet. --- AK/Endian.h | 24 ++++++++++++++++++++++++ Userland/ntpquery.cpp | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/AK/Endian.h b/AK/Endian.h index 1c6e91e1da..f8b0a5fdc2 100644 --- a/AK/Endian.h +++ b/AK/Endian.h @@ -29,6 +29,30 @@ #include #include +#if defined(AK_OS_MACOS) +# include +# include + +# define htobe16(x) OSSwapHostToBigInt16(x) +# define htole16(x) OSSwapHostToLittleInt16(x) +# define be16toh(x) OSSwapBigToHostInt16(x) +# define le16toh(x) OSSwapLittleToHostInt16(x) + +# define htobe32(x) OSSwapHostToBigInt32(x) +# define htole32(x) OSSwapHostToLittleInt32(x) +# define be32toh(x) OSSwapBigToHostInt32(x) +# define le32toh(x) OSSwapLittleToHostInt32(x) + +# define htobe64(x) OSSwapHostToBigInt64(x) +# define htole64(x) OSSwapHostToLittleInt64(x) +# define be64toh(x) OSSwapBigToHostInt64(x) +# define le64toh(x) OSSwapLittleToHostInt64(x) + +# define __BIG_ENDIAN BIG_ENDIAN +# define __LITTLE_ENDIAN LITTLE_ENDIAN +# define __BYTE_ORDER BYTE_ORDER +#endif + namespace AK { template diff --git a/Userland/ntpquery.cpp b/Userland/ntpquery.cpp index 7a4350526b..31552998ac 100644 --- a/Userland/ntpquery.cpp +++ b/Userland/ntpquery.cpp @@ -26,10 +26,10 @@ #define _BSD_SOURCE #define _DEFAULT_SOURCE +#include #include #include #include -#include #include #include #include @@ -303,7 +303,7 @@ int main(int argc, char** argv) // When the system isn't under load, user-space t and packet_t are identical. If a shell with `yes` is running, it can be as high as 30ms in this program, // which gets user-space time immediately after the recvmsg() call. In programs that have an event loop reading from multiple sockets, it could be higher. - printf("Receive latency: %" PRId64 ".%06d s\n", kernel_to_userspace_latency.tv_sec, (int)kernel_to_userspace_latency.tv_usec); + printf("Receive latency: %" PRId64 ".%06d s\n", (i64)kernel_to_userspace_latency.tv_sec, (int)kernel_to_userspace_latency.tv_usec); } // Parts of the "Clock Filter" computations, https://tools.ietf.org/html/rfc5905#section-10