aboutsummaryrefslogtreecommitdiff
path: root/src/common/util.h
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-02-16 20:00:43 +0000
committerNick Mathewson <nickm@torproject.org>2007-02-16 20:00:43 +0000
commitd4aaffc6e7af3577696f55a1ffbcb6a2f98fbddb (patch)
treea5a84c69319e287d9b2013e548a416f0203fc251 /src/common/util.h
parente5d3269b10754b9b46bc19234bbfabd3e631f733 (diff)
downloadtor-d4aaffc6e7af3577696f55a1ffbcb6a2f98fbddb.tar.gz
tor-d4aaffc6e7af3577696f55a1ffbcb6a2f98fbddb.zip
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))". svn:r9592
Diffstat (limited to 'src/common/util.h')
-rw-r--r--src/common/util.h23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/common/util.h b/src/common/util.h
index 9c94925004..d64ffb8e88 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -110,6 +110,29 @@ extern int dmalloc_free(const char *file, const int line, void *pnt,
((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. */