From 29775f086663e3226b5f359d3218a36859cbebab Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 6 Apr 2019 17:07:11 -0700 Subject: [PATCH] ircd::util: Add hton()/ntoh() in-place swap overloads. --- include/ircd/util/bswap.h | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/include/ircd/util/bswap.h b/include/ircd/util/bswap.h index 28db8f357..1b0afbcbe 100644 --- a/include/ircd/util/bswap.h +++ b/include/ircd/util/bswap.h @@ -17,6 +17,8 @@ namespace ircd::util template T bswap(T); // Host <-> network endian + template T &hton(T *const &); + template T &ntoh(T *const &); template T hton(T); template T ntoh(T); @@ -85,21 +87,37 @@ template T ircd::util::hton(T a) { - #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return bswap(&a); - #else - return a; - #endif + return hton(&a); } template T ircd::util::ntoh(T a) +{ + return ntoh(&a); +} + +template +T & +ircd::util::hton(T *const &a) { #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - return bswap(&a); + return bswap(a); #else - return a; + assert(a); + return *a; + #endif +} + +template +T & +ircd::util::ntoh(T *const &a) +{ + #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ + return bswap(a); + #else + assert(a); + return *a; #endif }