[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[or-cvs] r9592: Move all struct-offset-manipulation macros into util.h, and (in tor/trunk: . src/common src/or)



Author: nickm
Date: 2007-02-16 15:00:43 -0500 (Fri, 16 Feb 2007)
New Revision: 9592

Modified:
   tor/trunk/
   tor/trunk/src/common/compat.c
   tor/trunk/src/common/util.h
   tor/trunk/src/or/config.c
   tor/trunk/src/or/or.h
Log:
 r11824@catbus:  nickm | 2007-02-16 13:16:47 -0500
 Move all struct-offset-manipulation macros into util.h, and use them consistently.  Because there are days when "SUBTYPE_P(handle, subtype, _base)" is just easier to read and write than "(basetp*)(((handle) - STRUCT_OFFSET(subtype, _base))".



Property changes on: tor/trunk
___________________________________________________________________
 svk:merge ticket from /tor/trunk [r11824] on 8246c3cf-6607-4228-993b-4d95d33730f1

Modified: tor/trunk/src/common/compat.c
===================================================================
--- tor/trunk/src/common/compat.c	2007-02-16 18:12:47 UTC (rev 9591)
+++ tor/trunk/src/common/compat.c	2007-02-16 20:00:43 UTC (rev 9592)
@@ -174,8 +174,7 @@
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  tor_mmap_impl_t *h = (tor_mmap_impl_t*)
-    (((char*)handle) - STRUCT_OFFSET(tor_mmap_impl_t, base));
+  tor_mmap_impl_t *h = SUBTYPE_P(handle, tor_mmap_impl_t, base);
   munmap((char*)h->base.data, h->mapping_size);
   tor_free(h);
 }
@@ -244,8 +243,7 @@
 void
 tor_munmap_file(tor_mmap_t *handle)
 {
-  win_mmap_t *h = (win_mmap_t*)
-    (((char*)handle) - STRUCT_OFFSET(win_mmap_t, base));
+  win_mmap_t *h = SUBTYPE_P(handle, win_mmap_t, base);
   if (handle->data)
     /* This is an ugly cast, but without it, "data" in struct tor_mmap_t would
        have to be redefined as non-const. */

Modified: tor/trunk/src/common/util.h
===================================================================
--- tor/trunk/src/common/util.h	2007-02-16 18:12:47 UTC (rev 9591)
+++ tor/trunk/src/common/util.h	2007-02-16 20:00:43 UTC (rev 9592)
@@ -110,6 +110,29 @@
    ((off_t) (((char*)&((tp*)0)->member)-(char*)0))
 #endif
 
+/** Macro: yield a pointer to the field at position <b>off</b> within the
+ * structure <b>st</b>.  Example:
+ * <pre>
+ *   struct a { int foo; int bar; } x;
+ *   off_t bar_offset = STRUCT_OFFSET(struct a, bar);
+ *   int *bar_p = STRUCT_VAR_P(&x, bar_offset);
+ *   *bar_p = 3;
+ * </pre>
+ */
+#define STRUCT_VAR_P(st, off) ((void*) ( ((char*)(st)) + (off) ) )
+
+/** Macro: yield a pointer to an enclosing structure given a pointer to
+ * a substructure at offset <b>off</b>. Example:
+ * <pre>
+ *   struct base { ... };
+ *   struct subtype { int x; struct base b; } x;
+ *   struct base *bp = &x.base;
+ *   struct *sp = SUBTYPE_P(bp, struct subtype, b);
+ * </pre>
+ */
+#define SUBTYPE_P(p, subtype, basemember) \
+  ((void*) ( ((char*)(p)) - STRUCT_OFFSET(subtype, basemember) ))
+
 /* String manipulation */
 
 /** Allowable characters in a hexadecimal string. */

Modified: tor/trunk/src/or/config.c
===================================================================
--- tor/trunk/src/or/config.c	2007-02-16 18:12:47 UTC (rev 9591)
+++ tor/trunk/src/or/config.c	2007-02-16 20:00:43 UTC (rev 9592)
@@ -102,18 +102,6 @@
   const char *initvalue; /**< String (or null) describing initial value. */
 } config_var_t;
 
-/** Macro: yield a pointer to the field at position <b>off</b> within the
- * structure <b>st</b>.  Example:
- * <pre>
- *   struct a { int foo; int bar; } x;
- *   off_t bar_offset = STRUCT_OFFSET(struct a, bar);
- *   int *bar_p = STRUCT_VAR_P(&x, bar_offset);
- *   *bar_p = 3;
- * </pre>
- */
-#define STRUCT_VAR_P(st, off) \
-  ((void*) ( ((char*)st) + off ) )
-
 /** An entry for config_vars: "The option <b>name</b> has type
  * CONFIG_TYPE_<b>conftype</b>, and corresponds to
  * or_options_t.<b>member</b>"

Modified: tor/trunk/src/or/or.h
===================================================================
--- tor/trunk/src/or/or.h	2007-02-16 18:12:47 UTC (rev 9591)
+++ tor/trunk/src/or/or.h	2007-02-16 20:00:43 UTC (rev 9592)
@@ -928,8 +928,7 @@
 /** Cast a connection_t subtype pointer to a connection_t **/
 #define TO_CONN(c) (&(((c)->_base)))
 /** Helper macro: Given a pointer to to._base, of type from*, return &to. */
-#define DOWNCAST(to, ptr) \
-  (to*) (((char*)(ptr)) - STRUCT_OFFSET(to, _base))
+#define DOWNCAST(to, ptr) ((to*)SUBTYPE_P(ptr, to, _base))
 
 /** Convert a connection_t* to an or_connection_t*; assert if the cast is
  * invalid. */
@@ -1515,14 +1514,11 @@
 static INLINE or_circuit_t *TO_OR_CIRCUIT(circuit_t *x)
 {
   tor_assert(x->magic == OR_CIRCUIT_MAGIC);
-  //return (or_circuit_t*) (((char*)x) - STRUCT_OFFSET(or_circuit_t, _base));
   return DOWNCAST(or_circuit_t, x);
 }
 static INLINE origin_circuit_t *TO_ORIGIN_CIRCUIT(circuit_t *x)
 {
   tor_assert(x->magic == ORIGIN_CIRCUIT_MAGIC);
-  //return (origin_circuit_t*)
-  //  (((char*)x) - STRUCT_OFFSET(origin_circuit_t, _base));
   return DOWNCAST(origin_circuit_t, x);
 }