[tor-commits] [tor/master] Move set/get_uint*() to inline functions in arch/bytes.h

nickm at torproject.org nickm at torproject.org
Wed Jun 27 21:22:39 UTC 2018


commit 4d81f5211b3c714edae6370bccc6873033c0092d
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Jun 27 15:00:11 2018 -0400

    Move set/get_uint*() to inline functions in arch/bytes.h
    
    Also move our ntohll/htonll functions.
---
 src/common/compat.c       |  65 --------------------
 src/common/compat.h       |  17 +-----
 src/common/util.c         |  21 -------
 src/common/util.h         |   3 -
 src/include.am            |   1 +
 src/lib/arch/.may_include |   2 +
 src/lib/arch/bytes.h      | 148 ++++++++++++++++++++++++++++++++++++++++++++++
 src/lib/arch/include.am   |   3 +
 8 files changed, 155 insertions(+), 105 deletions(-)

diff --git a/src/common/compat.c b/src/common/compat.c
index e26591776..033ffa6ae 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -227,71 +227,6 @@ tor_strtok_r_impl(char *str, const char *sep, char **lasts)
   return start;
 }
 
-/**
- * Read a 16-bit value beginning at <b>cp</b>.  Equivalent to
- * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
- * unaligned memory access.
- */
-uint16_t
-get_uint16(const void *cp)
-{
-  uint16_t v;
-  memcpy(&v,cp,2);
-  return v;
-}
-/**
- * Read a 32-bit value beginning at <b>cp</b>.  Equivalent to
- * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
- * unaligned memory access.
- */
-uint32_t
-get_uint32(const void *cp)
-{
-  uint32_t v;
-  memcpy(&v,cp,4);
-  return v;
-}
-/**
- * Read a 64-bit value beginning at <b>cp</b>.  Equivalent to
- * *(uint64_t*)(cp), but will not cause segfaults on platforms that forbid
- * unaligned memory access.
- */
-uint64_t
-get_uint64(const void *cp)
-{
-  uint64_t v;
-  memcpy(&v,cp,8);
-  return v;
-}
-
-/**
- * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
- * *(uint16_t*)(cp) = v, but will not cause segfaults on platforms that forbid
- * unaligned memory access. */
-void
-set_uint16(void *cp, uint16_t v)
-{
-  memcpy(cp,&v,2);
-}
-/**
- * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
- * *(uint32_t*)(cp) = v, but will not cause segfaults on platforms that forbid
- * unaligned memory access. */
-void
-set_uint32(void *cp, uint32_t v)
-{
-  memcpy(cp,&v,4);
-}
-/**
- * Set a 64-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
- * *(uint64_t*)(cp) = v, but will not cause segfaults on platforms that forbid
- * unaligned memory access. */
-void
-set_uint64(void *cp, uint64_t v)
-{
-  memcpy(cp,&v,8);
-}
-
 /** Represents a lockfile on which we hold the lock. */
 struct tor_lockfile_t {
   /** Name of the file */
diff --git a/src/common/compat.h b/src/common/compat.h
index 1379f95a7..2282e07be 100644
--- a/src/common/compat.h
+++ b/src/common/compat.h
@@ -46,6 +46,7 @@
 #endif
 
 #include "lib/cc/compat_compiler.h"
+#include "lib/arch/bytes.h"
 #include "common/compat_time.h"
 #include "lib/string/compat_ctype.h"
 #include "lib/string/compat_string.h"
@@ -152,22 +153,6 @@ typedef enum {
 /* ===== OS compatibility */
 MOCK_DECL(const char *, get_uname, (void));
 
-uint16_t get_uint16(const void *cp) ATTR_NONNULL((1));
-uint32_t get_uint32(const void *cp) ATTR_NONNULL((1));
-uint64_t get_uint64(const void *cp) ATTR_NONNULL((1));
-void set_uint16(void *cp, uint16_t v) ATTR_NONNULL((1));
-void set_uint32(void *cp, uint32_t v) ATTR_NONNULL((1));
-void set_uint64(void *cp, uint64_t v) ATTR_NONNULL((1));
-
-/* These uint8 variants are defined to make the code more uniform. */
-#define get_uint8(cp) (*(const uint8_t*)(cp))
-static void set_uint8(void *cp, uint8_t v);
-static inline void
-set_uint8(void *cp, uint8_t v)
-{
-  *(uint8_t*)cp = v;
-}
-
 #if !defined(HAVE_RLIM_T)
 typedef unsigned long rlim_t;
 #endif
diff --git a/src/common/util.c b/src/common/util.c
index 6233b8e4f..09d3b2679 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2848,24 +2848,3 @@ ENABLE_GCC_WARNING(double-promotion)
 ENABLE_GCC_WARNING(float-conversion)
 #endif
 }
-
-/** Return a uint64_t value from <b>a</b> in network byte order. */
-uint64_t
-tor_htonll(uint64_t a)
-{
-#ifdef WORDS_BIGENDIAN
-  /* Big endian. */
-  return a;
-#else /* WORDS_BIGENDIAN */
-  /* Little endian. The worst... */
-  return htonl((uint32_t)(a>>32)) |
-    (((uint64_t)htonl((uint32_t)a))<<32);
-#endif /* defined(WORDS_BIGENDIAN) */
-}
-
-/** Return a uint64_t value from <b>a</b> in host byte order. */
-uint64_t
-tor_ntohll(uint64_t a)
-{
-  return tor_htonll(a);
-}
diff --git a/src/common/util.h b/src/common/util.h
index 4f8d6395d..2b16fc457 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -39,9 +39,6 @@
 #include "lib/fs/files.h"
 #include "lib/fs/path.h"
 
-uint64_t tor_htonll(uint64_t a);
-uint64_t tor_ntohll(uint64_t a);
-
 void tor_log_mallinfo(int severity);
 
 /** Macro: yield a pointer to an enclosing structure given a pointer to
diff --git a/src/include.am b/src/include.am
index e5527b098..62234d468 100644
--- a/src/include.am
+++ b/src/include.am
@@ -1,4 +1,5 @@
 include src/ext/include.am
+include src/lib/arch/include.am
 include src/lib/err/include.am
 include src/lib/cc/include.am
 include src/lib/ctime/include.am
diff --git a/src/lib/arch/.may_include b/src/lib/arch/.may_include
new file mode 100644
index 000000000..4285c3dcb
--- /dev/null
+++ b/src/lib/arch/.may_include
@@ -0,0 +1,2 @@
+orconfig.h
+lib/cc/*.h
diff --git a/src/lib/arch/bytes.h b/src/lib/arch/bytes.h
new file mode 100644
index 000000000..dcd35ae4f
--- /dev/null
+++ b/src/lib/arch/bytes.h
@@ -0,0 +1,148 @@
+/* Copyright (c) 2003-2004, Roger Dingledine
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+#ifndef TOR_BYTES_H
+#define TOR_BYTES_H
+
+#include <stdlib.h>
+#include "lib/cc/torint.h"
+
+/* The uint8 variants are defined to make the code more uniform. */
+static inline uint8_t
+get_uint8(const void *cp)
+{
+  return *(const uint8_t*)(cp);
+}
+static inline void
+set_uint8(void *cp, uint8_t v)
+{
+  *(uint8_t*)cp = v;
+}
+
+/**
+ * Read a 16-bit value beginning at <b>cp</b>.  Equivalent to
+ * *(uint16_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
+static inline uint16_t
+get_uint16(const void *cp)
+{
+  uint16_t v;
+  memcpy(&v,cp,2);
+  return v;
+}
+/**
+ * Read a 32-bit value beginning at <b>cp</b>.  Equivalent to
+ * *(uint32_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
+static inline uint32_t
+get_uint32(const void *cp)
+{
+  uint32_t v;
+  memcpy(&v,cp,4);
+  return v;
+}
+/**
+ * Read a 64-bit value beginning at <b>cp</b>.  Equivalent to
+ * *(uint64_t*)(cp), but will not cause segfaults on platforms that forbid
+ * unaligned memory access.
+ */
+static inline uint64_t
+get_uint64(const void *cp)
+{
+  uint64_t v;
+  memcpy(&v,cp,8);
+  return v;
+}
+
+/**
+ * Set a 16-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
+ * *(uint16_t*)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
+static inline void
+set_uint16(void *cp, uint16_t v)
+{
+  memcpy(cp,&v,2);
+}
+/**
+ * Set a 32-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
+ * *(uint32_t*)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
+static inline void
+set_uint32(void *cp, uint32_t v)
+{
+  memcpy(cp,&v,4);
+}
+/**
+ * Set a 64-bit value beginning at <b>cp</b> to <b>v</b>. Equivalent to
+ * *(uint64_t*)(cp) = v, but will not cause segfaults on platforms that forbid
+ * unaligned memory access. */
+static inline void
+set_uint64(void *cp, uint64_t v)
+{
+  memcpy(cp,&v,8);
+}
+
+#ifdef WORDS_BIGENDIAN
+static inline uint32_t
+tor_htonl(uint32_t a)
+{
+  return a;
+}
+
+static inline uint32_t
+tor_ntohl(uint64_t a)
+{
+  return a;
+}
+
+static inline uint64_t
+tor_htonll(uint64_t a)
+{
+  return a;
+}
+
+static inline uint64_t
+tor_ntohll(uint64_t a)
+{
+  return a;
+}
+#else
+static inline uint32_t
+tor_htonl(uint32_t a)
+{
+  /* Our compilers will indeed recognize this as bswap. */
+  return
+    ((a & 0x000000ff) <<24) |
+    ((a & 0x0000ff00) << 8) |
+    ((a & 0x00ff0000) >> 8) |
+    ((a & 0xff000000) >>24);
+}
+
+static inline uint32_t
+tor_ntohl(uint32_t a)
+{
+  return tor_htonl(a);
+}
+
+/** Return a uint64_t value from <b>a</b> in network byte order. */
+static inline uint64_t
+tor_htonll(uint64_t a)
+{
+  /* Little endian. The worst... */
+  return tor_htonl((uint32_t)(a>>32)) |
+    (((uint64_t)tor_htonl((uint32_t)a))<<32);
+}
+
+/** Return a uint64_t value from <b>a</b> in host byte order. */
+static inline uint64_t
+tor_ntohll(uint64_t a)
+{
+  return tor_htonll(a);
+}
+#endif
+
+#endif
diff --git a/src/lib/arch/include.am b/src/lib/arch/include.am
new file mode 100644
index 000000000..f92ee9222
--- /dev/null
+++ b/src/lib/arch/include.am
@@ -0,0 +1,3 @@
+
+noinst_HEADERS += \
+	src/lib/arch/bytes.h





More information about the tor-commits mailing list