[tor-commits] [obfsproxy/master] don't reinvent offsetof

nickm at torproject.org nickm at torproject.org
Thu Jul 14 21:28:54 UTC 2011


commit 17e8cb0097f31374e7b44e957b026f8ed11f5e0f
Author: Zack Weinberg <zackw at panix.com>
Date:   Thu Jul 14 13:23:58 2011 -0700

    don't reinvent offsetof
---
 src/network.c |    4 ++--
 src/util.h    |   12 +++---------
 2 files changed, 5 insertions(+), 11 deletions(-)

diff --git a/src/network.c b/src/network.c
index ea06bc2..6a8afa6 100644
--- a/src/network.c
+++ b/src/network.c
@@ -93,7 +93,7 @@ close_all_connections(void)
 {
   /** Traverse the dll and close all connections */
   while (conn_list.head) {
-    conn_t *conn = UPCAST(conn_t, dll_node, conn_list.head);
+    conn_t *conn = DOWNCAST(conn_t, dll_node, conn_list.head);
     conn_free(conn); /* removes it */
   }
   assert(!n_connections);
@@ -169,7 +169,7 @@ free_all_listeners(void)
 
   /* Iterate listener doubly linked list and free them all. */
   while (listener_list.head) {
-    listener_t *listener = UPCAST(listener_t, dll_node, listener_list.head);
+    listener_t *listener = DOWNCAST(listener_t, dll_node, listener_list.head);
     listener_free(listener);
   }
 
diff --git a/src/util.h b/src/util.h
index ff9df22..b526dbf 100644
--- a/src/util.h
+++ b/src/util.h
@@ -6,7 +6,7 @@
 #define UTIL_H
 
 #include <stdarg.h> /* for va_list */
-#include <stddef.h> /* for size_t etc */
+#include <stddef.h> /* size_t, offsetof, NULL, etc */
 
 #ifndef __GNUC__
 #define __attribute__(x) /* nothing */
@@ -37,14 +37,8 @@ int obfs_snprintf(char *str, size_t size,
 
 /***** Doubly Linked List stuff. *****/
 
-#define OFFSETOF(container_type, element) \
-  (((char*)&((container_type*)0)->element) - ((char*) ((container_type*)0)))
-
-#define UPCAST(container_type, element, ptr) \
-  (container_type*) (                                                   \
-         ((char*)ptr) - OFFSETOF(container_type, element)   \
-                    )
-
+#define DOWNCAST(container_type, element, ptr) \
+  (container_type*)( ((char*)ptr) - offsetof(container_type, element) )
 
 /** A doubly linked list node.
     [algorithms ripped off Wikipedia (Doubly_linked_list) ] */



More information about the tor-commits mailing list